The Inbox: WebClient-Core-ct.127.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The Inbox: WebClient-Core-ct.127.mcz

commits-2
Christoph Thiede uploaded a new version of WebClient-Core to project The Inbox:
http://source.squeak.org/inbox/WebClient-Core-ct.127.mcz

==================== Summary ====================

Name: WebClient-Core-ct.127
Author: ct
Time: 2 October 2020, 5:32:32.7446 pm
UUID: f078e024-2f40-5c4b-b608-e21114236743
Ancestors: WebClient-Core-mt.126

Moves WebClient utility methods to instance side (and forwards them on class-side). This allows to reuse them even when it is necessary to use #httpDo: for passing additional configuration to the WebClient instance, e.g. #timeout:. Also includes minor refactoring of #htmlSubmit:fields:method:encoding:. If the POST encoding is unknown, raise an error instead silently doing nothing.

=============== Diff against WebClient-Core-mt.126 ===============

Item was changed:
  ----- Method: WebClient class>>htmlSubmit:fields: (in category 'utilities') -----
  htmlSubmit: urlString fields: fieldMap
+ "A utility method for html submit operations."
- "A utility method for html submit operations. The fieldMap can be EITHER
- an array of associations OR a Dictionary of key value pairs (the former is
- useful for providing multiple fields and/or specifying the order of fields).
 
+ ^ self httpDo: [:client |
+ client htmlSubmit: urlString fields: fieldMap]
- WebClient
- htmlSubmit: 'http://www.google.com/search'
- fields: {
- 'hl' -> 'en'.
- 'q' -> 'Squeak'
- }
- "
- ^self htmlSubmit: urlString
- fields: fieldMap
- method: 'GET'
  !

Item was changed:
  ----- Method: WebClient class>>htmlSubmit:fields:method: (in category 'utilities') -----
  htmlSubmit: urlString fields: fieldMap method: method
+ "A utility method for html submit operations."
- "A utility method for html submit operations. The fieldMap can be EITHER
- an array of associations OR a Dictionary of key value pairs (the former is
- useful for providing multiple fields and/or specifying the order of fields).
 
+ ^ self httpDo: [:client |
+ client htmlSubmit: urlString fields: fieldMap method: method]!
- WebClient
- htmlSubmit: 'http://www.google.com/search'
- fields: {
- 'hl' -> 'en'.
- 'q' -> 'Squeak'
- } method: 'GET'
- "
- ^self htmlSubmit: urlString
- fields: fieldMap
- method: method
- encoding: 'application/x-www-form-urlencoded'
- !

Item was changed:
  ----- Method: WebClient class>>htmlSubmit:fields:method:encoding: (in category 'utilities') -----
  htmlSubmit: urlString fields: fields method: method encoding: encoding
+ "A utility method for html submit operations."
- "A utility method for html submit operations. The fieldMap can be EITHER
- an array of associations OR a Dictionary of key value pairs (the former is
- useful for providing multiple fields and/or specifying the order of fields).
 
+ ^ self httpDo: [:client |
+ client htmlSubmit: urlString fields: fields method: method encoding: encoding]!
- WebClient
- htmlSubmit: 'http://www.google.com/search'
- fields: {
- 'hl' -> 'en'.
- 'q' -> 'Squeak'
- } method: 'GET'
- encoding: 'application/x-www-form-urlencoded'
- "
-
- method = 'GET' ifTrue:[
- "GET only supports url encoded requests"
- encoding = 'application/x-www-form-urlencoded'
- ifFalse:[^self error: 'Unsupported encoding: ', encoding].
- ^self httpGet: urlString, '?', (WebUtils encodeUrlEncodedForm: fields).
- ].
-
- method = 'POST' ifTrue:[
- "Dispatch on encoding type"
- encoding caseOf: {
- [ 'application/x-www-form-urlencoded'] -> [
- ^self httpPost: urlString
- content: (WebUtils encodeUrlEncodedForm: fields)
- type: encoding.
- ].
- ['multipart/form-data'] -> [
- ^self httpPost: urlString multipartFields: fields
- ].
- } otherwise:[]
- ].
-
- self error: 'Unsupported method: ', method.
- !

Item was changed:
  ----- Method: WebClient class>>httpPost:multipartFields: (in category 'utilities') -----
  httpPost: url multipartFields: fieldMap
+ "Make a form submission using multipart/form-data POST. See comment on instance-side implementation."
- "Make a form submission using multipart/form-data POST.
 
+ ^ self httpDo: [:client |
+ client httpPost: url multipartFields: fieldMap]!
- The fieldMap may contain MIMEDocument instances to indicate the presence
- of a file to upload to the server. If the MIMEDocument is present, its
- content type and file name will be used for the upload.
-
- The fieldMap can be EITHER an array of associations OR a Dictionary of
- key value pairs (the former is useful for providing multiple fields and/or
- specifying the order of fields)."
-
- | boundary |
- boundary := WebUtils multipartBoundary.
-
- ^self httpPost: url
- content: (WebUtils encodeMultipartForm: fieldMap boundary: boundary)
- type: 'multipart/form-data; boundary=', boundary!

Item was added:
+ ----- Method: WebClient>>htmlSubmit:fields: (in category 'utilities') -----
+ htmlSubmit: urlString fields: fieldMap
+
+ ^ self
+ htmlSubmit: urlString
+ fields: fieldMap
+ method: 'GET'!

Item was added:
+ ----- Method: WebClient>>htmlSubmit:fields:method: (in category 'utilities') -----
+ htmlSubmit: urlString fields: fieldMap method: method
+
+ ^ self
+ htmlSubmit: urlString
+ fields: fieldMap
+ method: method
+ encoding: 'application/x-www-form-urlencoded'!

Item was added:
+ ----- Method: WebClient>>htmlSubmit:fields:method:encoding: (in category 'utilities') -----
+ htmlSubmit: urlString fields: fields method: method encoding: encoding
+ "A utility method for html submit operations. The fieldMap can be EITHER an array of associations OR a Dictionary of key value pairs (the former is useful for providing multiple fields and/or specifying the order of fields).
+
+ Usage:
+ WebClient
+ htmlSubmit: 'http://www.google.com/search'
+ fields: {'hl' -> 'en'. 'q' -> 'Squeak'}
+ method: 'GET'
+ encoding: 'application/x-www-form-urlencoded'
+ "
+
+ ^ method
+ caseOf: {
+ ['GET'] -> [
+ "GET only supports url encoded requests"
+ encoding = 'application/x-www-form-urlencoded'
+ ifFalse: [^self error: 'Unsupported encoding: ', encoding].
+ self httpGet: urlString, '?', (WebUtils encodeUrlEncodedForm: fields)].
+ ['POST'] -> [
+ encoding caseOf: {
+ ['application/x-www-form-urlencoded'] -> [
+ self
+ httpPost: urlString
+ content: (WebUtils encodeUrlEncodedForm: fields)
+ type: encoding].
+ ['multipart/form-data'] -> [
+ self httpPost: urlString multipartFields: fields] }] }
+ otherwise: [self error: 'Unsupported method: ', method]!

Item was added:
+ ----- Method: WebClient>>httpPost:multipartFields: (in category 'utilities') -----
+ httpPost: url multipartFields: fieldMap
+ "Make a form submission using multipart/form-data POST.
+
+ The fieldMap may contain MIMEDocument instances to indicate the presence of a file to upload to the server. If the MIMEDocument is present, its content type and file name will be used for the upload.
+
+ The fieldMap can be EITHER an array of associations OR a Dictionary of key value pairs (the former is useful for providing multiple fields and/or specifying the order of fields)."
+
+ | boundary |
+ boundary := WebUtils multipartBoundary.
+ ^ self
+ httpPost: url
+ content: (WebUtils encodeMultipartForm: fieldMap boundary: boundary)
+ type: 'multipart/form-data; boundary=', boundary!