Skip to content

Commit a26be4e

Browse files
authored
Merge pull request #6 from BinaryBirds/dev
using swift-format
2 parents ba79873 + 6cf979c commit a26be4e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+786
-628
lines changed

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
# swift-format commands
4+
5+
format:
6+
@curl -s https://raw.githubusercontent.com/BinaryBirds/swift-format-template/main/config.json >> swift-format.json && \
7+
swift-format --configuration swift-format.json -i -r ./Sources && \
8+
swift-format --configuration swift-format.json -i -r ./Tests && \
9+
rm -f ./swift-format.json
10+
11+
lint:
12+
@curl -s https://raw.githubusercontent.com/BinaryBirds/swift-format-template/main/config.json >> swift-format.json && \
13+
swift-format lint --configuration swift-format.json -r ./Sources && \
14+
swift-format lint --configuration swift-format.json -r ./Tests && \
15+
rm -f ./swift-format.json

Sources/SwiftHttp/Data+Logger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
extension Data {
11-
11+
1212
/// A variable that represents a `String` object representing the `Data` object in utf8 encoding.
1313
var logValue: String {
1414
"\n" + String(data: self, encoding: .utf8)!

Sources/SwiftHttp/HttpClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public protocol HttpClient {
2727
/// - Returns: A HttpResponse object
2828
///
2929
func dataTask(_ req: HttpRequest) async throws -> HttpResponse
30-
30+
3131
///
3232
/// Retreives the contetns of the request (to a file), then returns the location (URL) of the file as a response data object
3333
///
@@ -38,7 +38,7 @@ public protocol HttpClient {
3838
/// - Returns: A HttpResponse object
3939
///
4040
func downloadTask(_ req: HttpRequest) async throws -> HttpResponse
41-
41+
4242
///
4343
/// Performs a HTTP request and uploads the body as binary data to the server
4444
///

Sources/SwiftHttp/HttpCodablePipeline.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88
import Foundation
99

1010
/// A fully Codable HTTP request pipeline, where the req body is encodable and the response is decodable
11-
public struct HttpCodablePipeline<T: Encodable, U: Decodable>: HttpRequestPipeline {
12-
11+
public struct HttpCodablePipeline<T: Encodable, U: Decodable>:
12+
HttpRequestPipeline
13+
{
14+
1315
let url: HttpUrl
1416
let method: HttpMethod
1517
let headers: [HttpHeaderKey: String]
1618
let body: T
1719
let validators: [HttpResponseValidator]
1820
let encoder: HttpRequestEncoder<T>
1921
let decoder: HttpResponseDecoder<U>
20-
22+
2123
///
2224
/// Init a new codable pipeline
2325
///
@@ -46,7 +48,7 @@ public struct HttpCodablePipeline<T: Encodable, U: Decodable>: HttpRequestPipeli
4648
self.encoder = encoder
4749
self.decoder = decoder
4850
}
49-
51+
5052
///
5153
/// Executes the request, encodes the body, validates the response and decodes the data
5254
///
@@ -59,16 +61,16 @@ public struct HttpCodablePipeline<T: Encodable, U: Decodable>: HttpRequestPipeli
5961
public func execute(
6062
_ executor: ((HttpRequest) async throws -> HttpResponse)
6163
) async throws -> U {
62-
let req = HttpRawRequest(url: url,
63-
method: method,
64-
headers: headers.merging(encoder.headers) { $1 },
65-
body: try encoder.encode(body))
66-
64+
let req = HttpRawRequest(
65+
url: url,
66+
method: method,
67+
headers: headers.merging(encoder.headers) { $1 },
68+
body: try encoder.encode(body)
69+
)
70+
6771
let response = try await executor(req)
6872
let validation = HttpResponseValidation(validators + decoder.validators)
6973
try validation.validate(response)
7074
return try decoder.decode(response.data)
7175
}
7276
}
73-
74-

Sources/SwiftHttp/HttpCodablePipelineCollection.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public protocol HttpCodablePipelineCollection {
1616
/// - Returns: The generic request encoder instance
1717
///
1818
func encoder<T: Encodable>() -> HttpRequestEncoder<T>
19-
19+
2020
///
2121
/// The generic decoder object used to decode response data
2222
///
@@ -25,25 +25,25 @@ public protocol HttpCodablePipelineCollection {
2525
func decoder<T: Decodable>() -> HttpResponseDecoder<T>
2626
}
2727

28-
public extension HttpCodablePipelineCollection {
28+
extension HttpCodablePipelineCollection {
2929

3030
///
3131
/// The generic encoder object used to encode body values
3232
///
3333
/// - Returns: The default json encoder
3434
///
35-
func encoder<T: Encodable>() -> HttpRequestEncoder<T> { .json() }
36-
35+
public func encoder<T: Encodable>() -> HttpRequestEncoder<T> { .json() }
36+
3737
///
3838
/// The generic decoder object used to decode response data
3939
///
4040
/// - Returns: The default json decoder
4141
///
42-
func decoder<T: Decodable>() -> HttpResponseDecoder<T> { .json() }
42+
public func decoder<T: Decodable>() -> HttpResponseDecoder<T> { .json() }
4343
}
4444

45-
public extension HttpCodablePipelineCollection {
46-
45+
extension HttpCodablePipelineCollection {
46+
4747
///
4848
/// Executes a raw request pipeline using a data values as a body and returns the response
4949
///
@@ -58,7 +58,7 @@ public extension HttpCodablePipelineCollection {
5858
///
5959
/// - Returns: The HTTP response object
6060
///
61-
func rawRequest(
61+
public func rawRequest(
6262
executor: ((HttpRequest) async throws -> HttpResponse),
6363
url: HttpUrl,
6464
method: HttpMethod,
@@ -75,7 +75,7 @@ public extension HttpCodablePipelineCollection {
7575
)
7676
return try await pipeline.execute(executor)
7777
}
78-
78+
7979
///
8080
/// Executes an encodable request pipeline using an encodable object as a body value and returns the response
8181
///
@@ -90,7 +90,7 @@ public extension HttpCodablePipelineCollection {
9090
///
9191
/// - Returns: The HTTP response object
9292
///
93-
func encodableRequest<T: Encodable>(
93+
public func encodableRequest<T: Encodable>(
9494
executor: ((HttpRequest) async throws -> HttpResponse),
9595
url: HttpUrl,
9696
method: HttpMethod,
@@ -108,7 +108,7 @@ public extension HttpCodablePipelineCollection {
108108
)
109109
return try await pipeline.execute(executor)
110110
}
111-
111+
112112
///
113113
/// Executes a raw request pipeline using a data values as a body and returns the response
114114
///
@@ -123,7 +123,7 @@ public extension HttpCodablePipelineCollection {
123123
///
124124
/// - Returns: The decoded response object
125125
///
126-
func decodableRequest<U: Decodable>(
126+
public func decodableRequest<U: Decodable>(
127127
executor: ((HttpRequest) async throws -> HttpResponse),
128128
url: HttpUrl,
129129
method: HttpMethod,
@@ -141,7 +141,7 @@ public extension HttpCodablePipelineCollection {
141141
)
142142
return try await pipeline.execute(executor)
143143
}
144-
144+
145145
///
146146
/// Executes a codable request pipeline using an encodable body and decodes the response
147147
///
@@ -156,7 +156,7 @@ public extension HttpCodablePipelineCollection {
156156
///
157157
/// - Returns: The decoded response object
158158
///
159-
func codableRequest<T: Encodable, U: Decodable>(
159+
public func codableRequest<T: Encodable, U: Decodable>(
160160
executor: ((HttpRequest) async throws -> HttpResponse),
161161
url: HttpUrl,
162162
method: HttpMethod,

Sources/SwiftHttp/HttpDataDecoder.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
/// A generic HTTP data decoder
1111
public protocol HttpDataDecoder {
12-
12+
1313
///
1414
/// Decodes the response data into a custom decodable object
1515
///
@@ -20,7 +20,8 @@ public protocol HttpDataDecoder {
2020
///
2121
/// - Returns: The decoded object
2222
///
23-
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable
23+
func decode<T>(_ type: T.Type, from data: Data) throws -> T
24+
where T: Decodable
2425
}
2526

2627
/// The JSON decoder is already a valid HTTP data decoder

Sources/SwiftHttp/HttpDataEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99

1010
/// Encodes an encodable object into a HTTP data representation
1111
public protocol HttpDataEncoder {
12-
12+
1313
///
1414
/// Encodes the object value into raw HTTP data
1515
///
@@ -19,7 +19,7 @@ public protocol HttpDataEncoder {
1919
///
2020
/// - Returns: The encoded data value
2121
///
22-
func encode<T>(_ value: T) throws -> Data where T : Encodable
22+
func encode<T>(_ value: T) throws -> Data where T: Encodable
2323
}
2424

2525
/// The JSON encoder is already a valid HTTP data encoder

Sources/SwiftHttp/HttpDecodablePipeline.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import Foundation
99

1010
/// A decodable pipeline workflow, to decode a value from the response
1111
public struct HttpDecodablePipeline<U: Decodable>: HttpRequestPipeline {
12-
12+
1313
let url: HttpUrl
1414
let method: HttpMethod
1515
let headers: [HttpHeaderKey: String]
1616
let body: Data?
1717
let validators: [HttpResponseValidator]
1818
let decoder: HttpResponseDecoder<U>
19-
19+
2020
///
2121
/// Initialize the pipeline
2222
///
@@ -42,7 +42,7 @@ public struct HttpDecodablePipeline<U: Decodable>: HttpRequestPipeline {
4242
self.validators = validators
4343
self.decoder = decoder
4444
}
45-
45+
4646
///
4747
/// Executes the request, encodes the body, validates the response and decodes the data
4848
///
@@ -68,4 +68,3 @@ public struct HttpDecodablePipeline<U: Decodable>: HttpRequestPipeline {
6868
return try decoder.decode(response.data)
6969
}
7070
}
71-

Sources/SwiftHttp/HttpEncodablePipeline.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import Foundation
99

1010
/// An encodable pipeline can be used to send an encodable object as a request body
1111
public struct HttpEncodablePipeline<T: Encodable>: HttpRequestPipeline {
12-
12+
1313
let url: HttpUrl
1414
let method: HttpMethod
1515
let headers: [HttpHeaderKey: String]
1616
let body: T
1717
let validators: [HttpResponseValidator]
1818
let encoder: HttpRequestEncoder<T>
19-
19+
2020
///
2121
/// Initialize the pipeline
2222
///
@@ -42,7 +42,7 @@ public struct HttpEncodablePipeline<T: Encodable>: HttpRequestPipeline {
4242
self.validators = validators
4343
self.encoder = encoder
4444
}
45-
45+
4646
///
4747
/// Executes the request, encodes the body, validates the response and decodes the data
4848
///
@@ -61,7 +61,7 @@ public struct HttpEncodablePipeline<T: Encodable>: HttpRequestPipeline {
6161
headers: headers.merging(encoder.headers) { $1 },
6262
body: try encoder.encode(body)
6363
)
64-
64+
6565
let response = try await executor(req)
6666
let validation = HttpResponseValidation(validators)
6767
try validation.validate(response)

Sources/SwiftHttp/HttpError.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ import Foundation
99

1010
/// A generic error object to transfer HTTP related error messages
1111
public enum HttpError: LocalizedError {
12-
12+
1313
/// The response is not a valid HTTP response
1414
case invalidResponse
15-
15+
1616
/// The response has a unknown status code
1717
case unknownStatusCode
18-
18+
1919
/// The response has an invalid status code
2020
case invalidStatusCode(HttpResponse)
21-
21+
2222
/// The response is missing a header
2323
case missingHeader(HttpResponse)
24-
24+
2525
/// The response has an invalid header value
2626
case invalidHeaderValue(HttpResponse)
27-
27+
2828
/// Upload request does not have data to send
2929
case missingUploadData
30-
30+
3131
/// Unknown error
3232
case unknown(Error)
33-
33+
3434
/// Proper error descriptions for the HttpError values
3535
public var errorDescription: String? {
3636
switch self {

0 commit comments

Comments
 (0)