@@ -40,14 +40,16 @@ struct Todo: Codable {
4040
4141struct TodoApi : HttpCodablePipelineCollection {
4242
43- let client: HttpClient = UrlSessionHttpClient (log : true )
43+ let client: HttpClient = UrlSessionHttpClient (logLevel : . info )
4444 let apiBaseUrl = HttpUrl (host : " jsonplaceholder.typicode.com" )
4545
4646
4747 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+ )
5153 }
5254}
5355
@@ -69,18 +71,25 @@ You can create decodable, encodable, codable or raw request when using a codable
6971You can create raw HTTP requests using the HttpUrl and the HttpRawRequest type.
7072
7173``` 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+ )
8190
8291/// execute the request using the client
83- let client = UrlSessionHttpClient (session : .shared , log : true )
92+ let client = UrlSessionHttpClient (session : .shared , logLevel : . info )
8493let response = try await client.dataTask (req)
8594
8695/// use the response data
@@ -129,13 +138,15 @@ let token: String = "valid-token"
129138let body = try JSONEncoder ().encode ([
130139 " foo" : " bar" ,
131140])
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+ )
139150
140151/*
141152curl "https://localhost/login/" \
@@ -157,11 +168,13 @@ You can validate a response by using a HttpResponseValidator object.
157168
158169``` swift
159170// 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+ )
165178
166179// check if the status code is between 200 and 299
167180let validator1 = HttpStatusCodeValidator () // -> (.ok), (.notFound), etc.
@@ -175,7 +188,6 @@ let validator2 = HttpHeaderValidator(.key(.contentType)) { value in
175188
176189try validator2.validate (response)
177190
178-
179191// validate using multiple validators
180192let validation = HttpResponseValidation ([validator1, validator2])
181193try validation.validate (response)
@@ -193,5 +205,3 @@ You can create your own HttpRequestTransformer object to add extra headers to yo
193205You can create your own HttpResponseTransformer object to validate the response and decode a custom value from the response data.
194206
195207The codable (encodable, decodable, codable) pipelines are a good example of this approach.
196-
197-
0 commit comments