@@ -40,14 +40,16 @@ struct Todo: Codable {
40
40
41
41
struct TodoApi : HttpCodablePipelineCollection {
42
42
43
- let client: HttpClient = UrlSessionHttpClient (log : true )
43
+ let client: HttpClient = UrlSessionHttpClient (logLevel : . info )
44
44
let apiBaseUrl = HttpUrl (host : " jsonplaceholder.typicode.com" )
45
45
46
46
47
47
func list () async throws -> [Todo] {
48
- try await decodableRequest (executor : client.dataTask ,
49
- url : apiBaseUrl.path (" todos" ),
50
- method : .get )
48
+ try await decodableRequest (
49
+ executor : client.dataTask ,
50
+ url : apiBaseUrl.path (" todos" ),
51
+ method : .get
52
+ )
51
53
}
52
54
}
53
55
@@ -69,18 +71,25 @@ You can create decodable, encodable, codable or raw request when using a codable
69
71
You can create raw HTTP requests using the HttpUrl and the HttpRawRequest type.
70
72
71
73
``` 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 )
74
+ let url = HttpUrl (
75
+ scheme : " https" ,
76
+ host : " jsonplaceholder.typicode.com" ,
77
+ port : 80 ,
78
+ path : [" todos" ],
79
+ resource : nil ,
80
+ query : [: ],
81
+ fragment : nil
82
+ )
83
+
84
+ let req = HttpRawRequest (
85
+ url : url,
86
+ method : .get ,
87
+ headers : [: ],
88
+ body : nil
89
+ )
81
90
82
91
/// execute the request using the client
83
- let client = UrlSessionHttpClient (session : .shared , log : true )
92
+ let client = UrlSessionHttpClient (session : .shared , logLevel : . info )
84
93
let response = try await client.dataTask (req)
85
94
86
95
/// use the response data
@@ -129,13 +138,15 @@ let token: String = "valid-token"
129
138
let body = try JSONEncoder ().encode ([
130
139
" foo" : " bar" ,
131
140
])
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)
141
+ let req = HttpRawRequest (
142
+ url : url,
143
+ method : .post ,
144
+ headers : [
145
+ .key (.authorization ): " Bearer \( token ) " ,
146
+ .custom (" my-header" ): " my-header-value" ,
147
+ ],
148
+ body : body
149
+ )
139
150
140
151
/*
141
152
curl "https://localhost/login/" \
@@ -157,11 +168,13 @@ You can validate a response by using a HttpResponseValidator object.
157
168
158
169
``` swift
159
170
// mock response
160
- let response = HttpRawResponse (statusCode : .ok ,
161
- headers : [
162
- .key (.contentType ): " application/json" ,
163
- ],
164
- data : .init ())
171
+ let response = HttpRawResponse (
172
+ statusCode : .ok ,
173
+ headers : [
174
+ .key (.contentType ): " application/json" ,
175
+ ],
176
+ data : .init ()
177
+ )
165
178
166
179
// check if the status code is between 200 and 299
167
180
let validator1 = HttpStatusCodeValidator () // -> (.ok), (.notFound), etc.
@@ -175,7 +188,6 @@ let validator2 = HttpHeaderValidator(.key(.contentType)) { value in
175
188
176
189
try validator2.validate (response)
177
190
178
-
179
191
// validate using multiple validators
180
192
let validation = HttpResponseValidation ([validator1, validator2])
181
193
try validation.validate (response)
@@ -193,5 +205,3 @@ You can create your own HttpRequestTransformer object to add extra headers to yo
193
205
You can create your own HttpResponseTransformer object to validate the response and decode a custom value from the response data.
194
206
195
207
The codable (encodable, decodable, codable) pipelines are a good example of this approach.
196
-
197
-
0 commit comments