Skip to content

Commit ecf2743

Browse files
authored
tech(swiftlint): Use SwiftLint to lint code (pjechris#19)
1 parent 58b328a commit ecf2743

35 files changed

+169
-210
lines changed

.github/workflows/test.yml

+3
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ jobs:
1616
- name: Checkout repo
1717
uses: actions/checkout@v2
1818

19+
- name: Lint code
20+
uses: swiftlint lint
21+
1922
- name: Run tests
2023
run: swift test --enable-test-discovery

.swift-format

-56
This file was deleted.

.swiftlint.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
included:
2+
- Sources
3+
- Tests
4+
5+
excluded:
6+
- build
7+
- .build
8+
9+
disabled_rules:
10+
- statement_position
11+
- type_name
12+
13+
opt_in_rules:
14+
- closure_end_indentation
15+
- conditional_returns_on_newline
16+
- empty_count
17+
- indentation_width
18+
19+
line_length: 120
20+
warning_threshold: 0

Package.swift

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ let package = Package(
1111
.library(name: "SimpleHTTP", targets: ["SimpleHTTP"])
1212
],
1313
dependencies: [
14-
// Dependencies declare other packages that this package depends on.
15-
// .package(url: /* package url */, from: "1.0.0"),
16-
.package(url: "https://github.com/apple/swift-format", branch: "main")
1714
],
1815
targets: [
1916
.target(name: "SimpleHTTPFoundation", dependencies: []),

Sources/SimpleHTTP/Interceptor/CompositeInterceptor.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@ import Combine
44
/// Use an Array of `Interceptor` as a single `Interceptor`
55
public struct CompositeInterceptor: ExpressibleByArrayLiteral, Sequence {
66
let interceptors: [Interceptor]
7-
7+
88
public init(arrayLiteral interceptors: Interceptor...) {
99
self.interceptors = interceptors
1010
}
11-
11+
1212
public func makeIterator() -> Array<Interceptor>.Iterator {
1313
interceptors.makeIterator()
1414
}
1515
}
16-
16+
1717
extension CompositeInterceptor: Interceptor {
1818
public func adaptRequest<Output>(_ request: Request<Output>) -> Request<Output> {
1919
reduce(request) { request, interceptor in
2020
interceptor.adaptRequest(request)
2121
}
2222
}
23-
23+
2424
public func rescueRequest<Output>(_ request: Request<Output>, error: Error) -> AnyPublisher<Void, Error>? {
2525
let publishers = compactMap { $0.rescueRequest(request, error: error) }
26-
26+
2727
guard !publishers.isEmpty else {
2828
return nil
2929
}
30-
30+
3131
return Publishers.MergeMany(publishers).eraseToAnyPublisher()
3232
}
33-
33+
3434
public func adaptOutput<Output>(_ response: Output, for request: Request<Output>) throws -> Output {
3535
try reduce(response) { response, interceptor in
3636
try interceptor.adaptOutput(response, for: request)
3737
}
3838
}
39-
39+
4040
public func receivedResponse<Output>(_ result: Result<Output, Error>, for request: Request<Output>) {
4141
forEach { interceptor in
4242
interceptor.receivedResponse(result, for: request)

Sources/SimpleHTTP/Interceptor/Interceptor.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public typealias Interceptor = RequestInterceptor & ResponseInterceptor
77
public protocol RequestInterceptor {
88
/// Should be called before making the request to provide modifications to `request`
99
func adaptRequest<Output>(_ request: Request<Output>) -> Request<Output>
10-
10+
1111
/// catch and retry a failed request
1212
/// - Returns: nil if the request should not be retried. Otherwise a publisher that will be executed before
1313
/// retrying the request
@@ -20,7 +20,7 @@ public protocol ResponseInterceptor {
2020
/// optionally throwing an error instead if needed
2121
/// - Parameter request: the request that was sent to the server
2222
func adaptOutput<Output>(_ output: Output, for request: Request<Output>) throws -> Output
23-
23+
2424
/// Notify of received response for `request`
2525
/// - Parameter request: the request that was sent to the server
2626
func receivedResponse<Output>(_ result: Result<Output, Error>, for request: Request<Output>)
@@ -30,11 +30,11 @@ extension RequestInterceptor {
3030
func shouldRescueRequest<Output>(_ request: Request<Output>, error: Error) async throws -> Bool {
3131
var cancellable: Set<AnyCancellable> = []
3232
let onCancel = { cancellable.removeAll() }
33-
33+
3434
guard let rescuePublisher = rescueRequest(request, error: error) else {
3535
return false
3636
}
37-
37+
3838
return try await withTaskCancellationHandler(
3939
handler: { onCancel() },
4040
operation: {

Sources/SimpleHTTP/MultipartForm/MultipartFormData.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public struct MultipartFormData {
152152
let headers = defineBodyPartHeader(name: name, fileName: fileName, mimeType: mimeType)
153153
let stream = InputStream(data: data)
154154
let length = data.count
155-
155+
156156
bodyParts.append(BodyPart(headers: headers, stream: stream, length: length))
157157
}
158158

Sources/SimpleHTTP/Query/Dictionary+QueryParam.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22

33
extension Dictionary where Key == String, Value == QueryParam {
44
/// transform query params into URLQueryItem`
5-
var queryItems: [URLQueryItem] {
5+
var queryItems: [URLQueryItem] {
66
self.flatMap { key, value -> [URLQueryItem] in
77
switch value.queryValue {
88
case .single(let value):
@@ -14,5 +14,5 @@ extension Dictionary where Key == String, Value == QueryParam {
1414
}
1515
}
1616
}
17-
17+
1818
}

Sources/SimpleHTTP/Query/QueryParam.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ extension Array: QueryParam where Element: QueryParam {
5050
return values
5151
}
5252
}
53-
53+
5454
return .collection(values)
5555
}
5656
}

Sources/SimpleHTTP/Request/Path.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ public struct Path: Equatable, ExpressibleByStringLiteral, ExpressibleByStringIn
3232
public init(stringLiteral value: StringLiteralType) {
3333
self.init(value: value)
3434
}
35-
35+
3636
public init(stringInterpolation: DefaultStringInterpolation) {
3737
self.init(value: stringInterpolation.description)
3838
}
39-
39+
4040
public static func ==(lhs: Path, rhs: String) -> Bool {
4141
lhs.value == rhs
4242
}
4343
}
44-

Sources/SimpleHTTP/Request/Request+URLRequest.swift

+4-6
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ extension Request {
1616
if let decoder = accepting {
1717
return request.settingHeaders([.accept: type(of: decoder).contentType.value])
1818
}
19-
19+
2020
return request
2121
}
2222

2323
private func toURLRequest(encoder: ContentDataEncoder) throws -> URLRequest {
2424
var urlRequest = try URLRequest(url: URL(from: self))
25-
25+
2626
urlRequest.httpMethod = method.rawValue.uppercased()
2727
urlRequest.cachePolicy = cachePolicy
2828
urlRequest.setHeaders(headers)
29-
29+
3030
if let body = body {
3131
switch body {
3232
case .encodable(let body):
@@ -35,10 +35,8 @@ extension Request {
3535
try urlRequest.multipartBody(multipart)
3636
}
3737
}
38-
38+
3939
return urlRequest
4040
}
4141

42-
4342
}
44-

Sources/SimpleHTTP/Request/Request.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ public struct Request<Output> {
2424
public let query: [String: QueryParam]
2525
public var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
2626
public var headers: HTTPHeaderFields = [:]
27-
27+
2828
/// Creates a request suitable for a HTTP GET
2929
public static func get(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
3030
self.init(path: path, method: .get, query: query, body: nil)
3131
}
32-
32+
3333
/// Creates a request suitable for a HTTP POST with a `Encodable` body
3434
public static func post(_ path: Path, body: Encodable?, query: [String: QueryParam] = [:])
3535
-> Self {
3636
self.init(path: path, method: .post, query: query, body: body.map(Body.encodable))
3737
}
38-
38+
3939
/// Creates a request suitable for a HTTP POST with a `MultipartFormData` body
4040
@_disfavoredOverload
4141
public static func post(_ path: Path, body: MultipartFormData?, query: [String: QueryParam] = [:])
4242
-> Self {
4343
self.init(path: path, method: .post, query: query, body: body.map(Body.multipart))
4444
}
45-
45+
4646
/// Creates a request suitable for a HTTP PUT with a `Encodable` body
4747
public static func put(_ path: Path, body: Encodable, query: [String: QueryParam] = [:])
4848
-> Self {
4949
self.init(path: path, method: .put, query: query, body: .encodable(body))
5050
}
51-
51+
5252
/// Creates a request suitable for a HTTP PUT with a `MultipartFormData` body
5353
public static func put(_ path: Path, body: MultipartFormData, query: [String: QueryParam] = [:])
5454
-> Self {
@@ -59,19 +59,19 @@ public struct Request<Output> {
5959
public static func put(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
6060
self.init(path: path, method: .put, query: query, body: nil)
6161
}
62-
62+
6363
/// Creates a request suitable for a HTTP PATCH with a `Encodable` body
6464
public static func patch(_ path: Path, body: Encodable, query: [String: QueryParam] = [:])
6565
-> Self {
6666
self.init(path: path, method: .patch, query: query, body: .encodable(body))
6767
}
68-
68+
6969
/// Creates a request suitable for a HTTP PATCH with a `MultipartFormData` body
7070
public static func patch(_ path: Path, body: MultipartFormData, query: [String: QueryParam] = [:])
7171
-> Self {
7272
self.init(path: path, method: .patch, query: query, body: .multipart(body))
7373
}
74-
74+
7575
/// Creates a request suitable for a HTTP DELETE
7676
public static func delete(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
7777
self.init(path: path, method: .delete, query: query, body: nil)
@@ -81,7 +81,7 @@ public struct Request<Output> {
8181
public static func delete(_ path: Path, body: Encodable, query: [String: QueryParam] = [:]) -> Self {
8282
self.init(path: path, method: .delete, query: query, body: nil)
8383
}
84-
84+
8585
/// Creates a Request.
8686
///
8787
/// Use this init only if default provided static initializers (`.get`, `.post`, `.put`, `patch`, `.delete`) do not suit your needs.
@@ -91,22 +91,22 @@ public struct Request<Output> {
9191
self.body = body
9292
self.query = query
9393
}
94-
94+
9595
/// Adds headers to the request
9696
public func headers(_ newHeaders: [HTTPHeader: String]) -> Self {
9797
var request = self
98-
98+
9999
request.headers.merge(newHeaders) { $1 }
100-
100+
101101
return request
102102
}
103-
103+
104104
/// Configures request cache policy
105105
public func cachePolicy(_ policy: URLRequest.CachePolicy) -> Self {
106106
var request = self
107-
107+
108108
request.cachePolicy = policy
109-
109+
110110
return request
111111
}
112112
}

Sources/SimpleHTTP/Request/URL+Request.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ extension URL {
99
guard var components = URLComponents(string: request.path.value) else {
1010
throw URLComponents.Error.invalid(path: request.path)
1111
}
12-
12+
1313
let queryItems = (components.queryItems ?? []) + request.query.queryItems
14-
14+
1515
components.queryItems = queryItems.isEmpty ? nil : queryItems
16-
16+
1717
guard let url = components.url else {
1818
throw URLComponents.Error.cannotGenerateURL(components: components)
1919
}
20-
20+
2121
self = url
2222
}
2323
}
@@ -30,7 +30,7 @@ extension URLComponents {
3030
}
3131

3232
extension Dictionary where Key == String, Value == String {
33-
fileprivate var queryItems: [URLQueryItem] {
33+
fileprivate var queryItems: [URLQueryItem] {
3434
map { URLQueryItem(name: $0.key, value: $0.value) }
3535
}
3636
}

Sources/SimpleHTTP/Response/DataResponse.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension URLDataResponse {
1414
guard let decoder = errorDecoder, !data.isEmpty else {
1515
throw error
1616
}
17-
17+
1818
throw try decoder(data)
1919
}
2020
}

0 commit comments

Comments
 (0)