@@ -63,14 +63,100 @@ The HttpClient provides the executors to perform data, download or upload tasks.
63
63
64
64
You can create decodable, encodable, codable or raw request when using a codable pipeline collection.
65
65
66
- ## Request pipelines
67
66
67
+ ## Using raw requests & responses
68
68
69
- ## Custom decoder
69
+ You can create raw HTTP requests using the HttpUrl and the HttpRawRequest type.
70
70
71
- If you are using a request pipeline collection you can override the encoder and decoder objects.
71
+ ``` swift
72
+ let url = HttpUrl (scheme : " https" ,
73
+ host : " jsonplaceholder.typicode.com" ,
74
+ port : 80 ,
75
+ path : [" todos" ],
76
+ resource : nil ,
77
+ query : [: ],
78
+ fragment : nil )
79
+
80
+ let req = HttpRawRequest (url : url, method : .get , headers : [: ], body : nil )
81
+
82
+ /// execute the request using the client
83
+ let client = UrlSessionHttpClient (session : .shared , log : true )
84
+ let response = try await client.dataTask (req)
85
+
86
+ /// use the response data
87
+ let todos = try JSONDecoder ().decode ([Todo].self , from : response.data )
88
+ // response.statusCode == .ok
89
+ // response.headers -> response headers as a dictionary
90
+ ```
91
+
92
+ You can use a specific HttpClient to execute a request using a data, download or upload task.
93
+
94
+
95
+ ## Constructing URLs
96
+
97
+ The HttpUrl type allows us to construct various URLs using a base URL.
72
98
73
99
``` swift
74
100
101
+ let baseUrl = HttpUrl (host : " jsonplaceholder.typicode.com" )
75
102
76
- ```
103
+ // https://jsonplaceholder.typicode.com/todos/
104
+ baseUrl.path (" todos" )
105
+
106
+ // https://jsonplaceholder.typicode.com/todos/1/
107
+ baseUrl.path (" todos" , String (1 ))
108
+
109
+ // https://jsonplaceholder.typicode.com/todos/?foo=bar
110
+ baseUrl.path (" todos" ).query (" foo" , " bar" )
111
+
112
+ // https://jsonplaceholder.typicode.com/todos/?foo=baz&bar=1
113
+ baseUrl.path (" todos" ).query ([
114
+ " foo" : " baz" ,
115
+ " bar" : " 1" ,
116
+ ])
117
+ ```
118
+
119
+ You can transform a HttpUrl object into a Foundation URL by using the ` .url ` property.
120
+
121
+ ## Requests
122
+
123
+ You can create a raw request object using a HttpUrl and a HttpMethod, including additional headers and a body data.
124
+
125
+ ``` swift
126
+ let url = HttpUrl (host : " localhost" , path : [" login" ])
127
+
128
+ let token: String = " valid-token"
129
+ let body = try JSONEncoder ().encode ([
130
+ " foo" : " bar" ,
131
+ ])
132
+ let req = HttpRawRequest (url : url,
133
+ method : .post ,
134
+ headers : [
135
+ .key (.authorization ): " Bearer \( token ) " ,
136
+ .custom (" my-header" ): " my-header-value" ,
137
+ ],
138
+ body : body)
139
+
140
+ /*
141
+ curl "https://localhost/login/" \
142
+ -X POST \
143
+ -H 'my-header: my-header-value' \
144
+ -H 'Authorization: Bearer valid-token' \
145
+ -d '{"foo":"bar"}'
146
+ */
147
+ print (req.urlRequest .curlString )
148
+ ```
149
+
150
+ You can transform a HttpRequest into a URLReequest via the ` .urlRequest ` property.
151
+ You can print the cURL representation of a request by using the ` .curlString ` property on a URLRequest object.
152
+
153
+
154
+ ## Pipelines
155
+
156
+ A pipeline allows you to transform a raw request and a raw response using a set of custom actions.
157
+
158
+ You can create your own HttpRequestTransformer object to add extra headers to your request and encode a custom body object to a data value.
159
+
160
+ You can create your own HttpResponseTransformer object to validate the response and decode a custom value from the response data.
161
+
162
+ The codable (encodable, decodable, codable) pipelines are a good example of this approach.
0 commit comments