Skip to content

Commit ba79873

Browse files
committed
using swift-log instead of print
1 parent 34a34d5 commit ba79873

29 files changed

+366
-176
lines changed

Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ let package = Package(
1313
.library(name: "SwiftHttp", targets: ["SwiftHttp"]),
1414
],
1515
dependencies: [
16-
16+
.package(url: "https://github.com/apple/swift-log.git", branch: "main"),
1717
],
1818
targets: [
19-
.target(name: "SwiftHttp", dependencies: []),
19+
.target(name: "SwiftHttp", dependencies: [
20+
.product(name: "Logging", package: "swift-log")
21+
]),
2022
.testTarget(name: "SwiftHttpTests", dependencies: ["SwiftHttp"]),
2123
]
2224
)

README.md

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ struct Todo: Codable {
4040

4141
struct 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
6971
You 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)
8493
let response = try await client.dataTask(req)
8594

8695
/// use the response data
@@ -129,13 +138,15 @@ let token: String = "valid-token"
129138
let 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
/*
141152
curl "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
167180
let validator1 = HttpStatusCodeValidator() // -> (.ok), (.notFound), etc.
@@ -175,7 +188,6 @@ let validator2 = HttpHeaderValidator(.key(.contentType)) { value in
175188

176189
try validator2.validate(response)
177190

178-
179191
// validate using multiple validators
180192
let validation = HttpResponseValidation([validator1, validator2])
181193
try validation.validate(response)
@@ -193,5 +205,3 @@ You can create your own HttpRequestTransformer object to add extra headers to yo
193205
You can create your own HttpResponseTransformer object to validate the response and decode a custom value from the response data.
194206

195207
The codable (encodable, decodable, codable) pipelines are a good example of this approach.
196-
197-

Sources/SwiftHttp/Data+Logger.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Data+Logger.swift
3+
// SwiftHttp
4+
//
5+
// Created by Viasz-Kádi Ferenc on 2023. 04. 14..
6+
//
7+
8+
import Foundation
9+
10+
extension Data {
11+
12+
/// A variable that represents a `String` object representing the `Data` object in utf8 encoding.
13+
var logValue: String {
14+
"\n" + String(data: self, encoding: .utf8)!
15+
}
16+
}

Sources/SwiftHttp/HttpCodablePipeline.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ public struct HttpCodablePipeline<T: Encodable, U: Decodable>: HttpRequestPipeli
2929
/// - Parameter encoder: The request data encoder object
3030
/// - Parameter decoder: The response data decoder object
3131
///
32-
public init(url: HttpUrl,
33-
method: HttpMethod,
34-
headers: [HttpHeaderKey: String] = [:],
35-
body: T,
36-
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()],
37-
encoder: HttpRequestEncoder<T>,
38-
decoder: HttpResponseDecoder<U>) {
32+
public init(
33+
url: HttpUrl,
34+
method: HttpMethod,
35+
headers: [HttpHeaderKey: String] = [:],
36+
body: T,
37+
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()],
38+
encoder: HttpRequestEncoder<T>,
39+
decoder: HttpResponseDecoder<U>
40+
) {
3941
self.url = url
4042
self.method = method
4143
self.headers = headers
@@ -54,7 +56,9 @@ public struct HttpCodablePipeline<T: Encodable, U: Decodable>: HttpRequestPipeli
5456
///
5557
/// - Returns: The decoded response object
5658
///
57-
public func execute(_ executor: ((HttpRequest) async throws -> HttpResponse)) async throws -> U {
59+
public func execute(
60+
_ executor: ((HttpRequest) async throws -> HttpResponse)
61+
) async throws -> U {
5862
let req = HttpRawRequest(url: url,
5963
method: method,
6064
headers: headers.merging(encoder.headers) { $1 },

Sources/SwiftHttp/HttpCodablePipelineCollection.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,17 @@ public extension HttpCodablePipelineCollection {
162162
method: HttpMethod,
163163
headers: [HttpHeaderKey: String] = [:],
164164
body: T,
165-
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()]) async throws -> U {
166-
let pipeline: HttpCodablePipeline<T, U> = .init(
167-
url: url,
168-
method: method,
169-
headers: headers,
170-
body: body,
171-
validators: validators,
172-
encoder: encoder(),
173-
decoder: decoder()
174-
)
175-
return try await pipeline.execute(executor)
176-
}
165+
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()]
166+
) async throws -> U {
167+
let pipeline: HttpCodablePipeline<T, U> = .init(
168+
url: url,
169+
method: method,
170+
headers: headers,
171+
body: body,
172+
validators: validators,
173+
encoder: encoder(),
174+
decoder: decoder()
175+
)
176+
return try await pipeline.execute(executor)
177+
}
177178
}

Sources/SwiftHttp/HttpDecodablePipeline.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ public struct HttpDecodablePipeline<U: Decodable>: HttpRequestPipeline {
2727
/// - Parameter validators: The response validators
2828
/// - Parameter decoder: The decoder used to decode the response data
2929
///
30-
public init(url: HttpUrl,
31-
method: HttpMethod,
32-
headers: [HttpHeaderKey: String] = [:],
33-
body: Data? = nil,
34-
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()],
35-
decoder: HttpResponseDecoder<U>) {
30+
public init(
31+
url: HttpUrl,
32+
method: HttpMethod,
33+
headers: [HttpHeaderKey: String] = [:],
34+
body: Data? = nil,
35+
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()],
36+
decoder: HttpResponseDecoder<U>
37+
) {
3638
self.url = url
3739
self.method = method
3840
self.headers = headers
@@ -50,11 +52,15 @@ public struct HttpDecodablePipeline<U: Decodable>: HttpRequestPipeline {
5052
///
5153
/// - Returns: The decoded response object
5254
///
53-
public func execute(_ executor: ((HttpRequest) async throws -> HttpResponse)) async throws -> U {
54-
let req = HttpRawRequest(url: url,
55-
method: method,
56-
headers: headers,
57-
body: body)
55+
public func execute(
56+
_ executor: ((HttpRequest) async throws -> HttpResponse)
57+
) async throws -> U {
58+
let req = HttpRawRequest(
59+
url: url,
60+
method: method,
61+
headers: headers,
62+
body: body
63+
)
5864

5965
let response = try await executor(req)
6066
let validation = HttpResponseValidation(validators + decoder.validators)

Sources/SwiftHttp/HttpEncodablePipeline.swift

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ public struct HttpEncodablePipeline<T: Encodable>: HttpRequestPipeline {
2727
/// - Parameter validators: The response validators
2828
/// - Parameter encoder: The encoder used to encode the body value
2929
///
30-
public init(url: HttpUrl,
31-
method: HttpMethod,
32-
headers: [HttpHeaderKey: String] = [:],
33-
body: T,
34-
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()],
35-
encoder: HttpRequestEncoder<T>) {
30+
public init(
31+
url: HttpUrl,
32+
method: HttpMethod,
33+
headers: [HttpHeaderKey: String] = [:],
34+
body: T,
35+
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()],
36+
encoder: HttpRequestEncoder<T>
37+
) {
3638
self.url = url
3739
self.method = method
3840
self.headers = headers
@@ -50,11 +52,15 @@ public struct HttpEncodablePipeline<T: Encodable>: HttpRequestPipeline {
5052
///
5153
/// - Returns: The HTTP response object
5254
///
53-
public func execute(_ executor: ((HttpRequest) async throws -> HttpResponse)) async throws -> HttpResponse {
54-
let req = HttpRawRequest(url: url,
55-
method: method,
56-
headers: headers.merging(encoder.headers) { $1 },
57-
body: try encoder.encode(body))
55+
public func execute(
56+
_ executor: ((HttpRequest) async throws -> HttpResponse)
57+
) async throws -> HttpResponse {
58+
let req = HttpRawRequest(
59+
url: url,
60+
method: method,
61+
headers: headers.merging(encoder.headers) { $1 },
62+
body: try encoder.encode(body)
63+
)
5864

5965
let response = try await executor(req)
6066
let validation = HttpResponseValidation(validators)

Sources/SwiftHttp/HttpHeaderValidator.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ public struct HttpHeaderValidator: HttpResponseValidator {
2828
/// - Parameter key The header key to look up the value
2929
/// - Parameter blcok The validation block using the header value
3030
///
31-
public init(_ key: HttpHeaderKey, _ block: @escaping ((String) -> Bool)) {
31+
public init(
32+
_ key: HttpHeaderKey,
33+
_ block: @escaping ((String) -> Bool)
34+
) {
3235
self.key = key
3336
self.block = block
3437
}

Sources/SwiftHttp/HttpRawPipeline.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ public struct HttpRawPipeline: HttpRequestPipeline {
2525
/// - Parameter body: The request body as a data value
2626
/// - Parameter validators: The response validators
2727
///
28-
public init(url: HttpUrl,
29-
method: HttpMethod,
30-
headers: [HttpHeaderKey: String] = [:],
31-
body: Data? = nil,
32-
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()]) {
28+
public init(
29+
url: HttpUrl,
30+
method: HttpMethod,
31+
headers: [HttpHeaderKey: String] = [:],
32+
body: Data? = nil,
33+
validators: [HttpResponseValidator] = [HttpStatusCodeValidator()]
34+
) {
3335
self.url = url
3436
self.method = method
3537
self.headers = headers
@@ -46,8 +48,15 @@ public struct HttpRawPipeline: HttpRequestPipeline {
4648
///
4749
/// - Returns: The HTTP response object
4850
///
49-
public func execute(_ executor: ((HttpRequest) async throws -> HttpResponse)) async throws -> HttpResponse {
50-
let req = HttpRawRequest(url: url, method: method, headers: headers, body: body)
51+
public func execute(
52+
_ executor: ((HttpRequest) async throws -> HttpResponse)
53+
) async throws -> HttpResponse {
54+
let req = HttpRawRequest(
55+
url: url,
56+
method: method,
57+
headers: headers,
58+
body: body
59+
)
5160
let response = try await executor(req)
5261
let validation = HttpResponseValidation(validators)
5362
try validation.validate(response)

Sources/SwiftHttp/HttpRawRequest.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ public struct HttpRawRequest: HttpRequest {
3030
/// - Parameter headers: The request headers
3131
/// - Parameter body: The request body as a data value
3232
///
33-
public init(url: HttpUrl,
34-
method: HttpMethod = .get,
35-
headers: [HttpHeaderKey: String] = [:],
36-
body: Data? = nil) {
33+
public init(
34+
url: HttpUrl,
35+
method: HttpMethod = .get,
36+
headers: [HttpHeaderKey: String] = [:],
37+
body: Data? = nil
38+
) {
3739
self.method = method
3840
self.url = url
3941
self.headers = headers

Sources/SwiftHttp/HttpRawResponse.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public struct HttpRawResponse: HttpResponse {
2929
/// - Parameter headers: The header fields
3030
/// - Parameter data: The body data
3131
///
32-
public init(statusCode: HttpStatusCode, headers: [HttpHeaderKey: String], data: Data) {
32+
public init(
33+
statusCode: HttpStatusCode,
34+
headers: [HttpHeaderKey: String],
35+
data: Data
36+
) {
3337
self.statusCode = statusCode
3438
self.headers = headers
3539
self.data = data
@@ -61,4 +65,5 @@ public struct HttpRawResponse: HttpResponse {
6165
}
6266

6367
extension HttpRawResponse: Codable, Equatable {
68+
6469
}

0 commit comments

Comments
 (0)