Skip to content

Commit

Permalink
tech(swiftlint): Use SwiftLint to lint code (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjechris authored Nov 23, 2022
1 parent 58b328a commit ecf2743
Show file tree
Hide file tree
Showing 35 changed files with 169 additions and 210 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ jobs:
- name: Checkout repo
uses: actions/checkout@v2

- name: Lint code
uses: swiftlint lint

- name: Run tests
run: swift test --enable-test-discovery
56 changes: 0 additions & 56 deletions .swift-format

This file was deleted.

20 changes: 20 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
included:
- Sources
- Tests

excluded:
- build
- .build

disabled_rules:
- statement_position
- type_name

opt_in_rules:
- closure_end_indentation
- conditional_returns_on_newline
- empty_count
- indentation_width

line_length: 120
warning_threshold: 0
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ let package = Package(
.library(name: "SimpleHTTP", targets: ["SimpleHTTP"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/apple/swift-format", branch: "main")
],
targets: [
.target(name: "SimpleHTTPFoundation", dependencies: []),
Expand Down
16 changes: 8 additions & 8 deletions Sources/SimpleHTTP/Interceptor/CompositeInterceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ import Combine
/// Use an Array of `Interceptor` as a single `Interceptor`
public struct CompositeInterceptor: ExpressibleByArrayLiteral, Sequence {
let interceptors: [Interceptor]

public init(arrayLiteral interceptors: Interceptor...) {
self.interceptors = interceptors
}

public func makeIterator() -> Array<Interceptor>.Iterator {
interceptors.makeIterator()
}
}

extension CompositeInterceptor: Interceptor {
public func adaptRequest<Output>(_ request: Request<Output>) -> Request<Output> {
reduce(request) { request, interceptor in
interceptor.adaptRequest(request)
}
}

public func rescueRequest<Output>(_ request: Request<Output>, error: Error) -> AnyPublisher<Void, Error>? {
let publishers = compactMap { $0.rescueRequest(request, error: error) }

guard !publishers.isEmpty else {
return nil
}

return Publishers.MergeMany(publishers).eraseToAnyPublisher()
}

public func adaptOutput<Output>(_ response: Output, for request: Request<Output>) throws -> Output {
try reduce(response) { response, interceptor in
try interceptor.adaptOutput(response, for: request)
}
}

public func receivedResponse<Output>(_ result: Result<Output, Error>, for request: Request<Output>) {
forEach { interceptor in
interceptor.receivedResponse(result, for: request)
Expand Down
8 changes: 4 additions & 4 deletions Sources/SimpleHTTP/Interceptor/Interceptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public typealias Interceptor = RequestInterceptor & ResponseInterceptor
public protocol RequestInterceptor {
/// Should be called before making the request to provide modifications to `request`
func adaptRequest<Output>(_ request: Request<Output>) -> Request<Output>

/// catch and retry a failed request
/// - Returns: nil if the request should not be retried. Otherwise a publisher that will be executed before
/// retrying the request
Expand All @@ -20,7 +20,7 @@ public protocol ResponseInterceptor {
/// optionally throwing an error instead if needed
/// - Parameter request: the request that was sent to the server
func adaptOutput<Output>(_ output: Output, for request: Request<Output>) throws -> Output

/// Notify of received response for `request`
/// - Parameter request: the request that was sent to the server
func receivedResponse<Output>(_ result: Result<Output, Error>, for request: Request<Output>)
Expand All @@ -30,11 +30,11 @@ extension RequestInterceptor {
func shouldRescueRequest<Output>(_ request: Request<Output>, error: Error) async throws -> Bool {
var cancellable: Set<AnyCancellable> = []
let onCancel = { cancellable.removeAll() }

guard let rescuePublisher = rescueRequest(request, error: error) else {
return false
}

return try await withTaskCancellationHandler(
handler: { onCancel() },
operation: {
Expand Down
2 changes: 1 addition & 1 deletion Sources/SimpleHTTP/MultipartForm/MultipartFormData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public struct MultipartFormData {
let headers = defineBodyPartHeader(name: name, fileName: fileName, mimeType: mimeType)
let stream = InputStream(data: data)
let length = data.count

bodyParts.append(BodyPart(headers: headers, stream: stream, length: length))
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/SimpleHTTP/Query/Dictionary+QueryParam.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

extension Dictionary where Key == String, Value == QueryParam {
/// transform query params into URLQueryItem`
var queryItems: [URLQueryItem] {
var queryItems: [URLQueryItem] {
self.flatMap { key, value -> [URLQueryItem] in
switch value.queryValue {
case .single(let value):
Expand All @@ -14,5 +14,5 @@ extension Dictionary where Key == String, Value == QueryParam {
}
}
}

}
2 changes: 1 addition & 1 deletion Sources/SimpleHTTP/Query/QueryParam.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ extension Array: QueryParam where Element: QueryParam {
return values
}
}

return .collection(values)
}
}
5 changes: 2 additions & 3 deletions Sources/SimpleHTTP/Request/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ public struct Path: Equatable, ExpressibleByStringLiteral, ExpressibleByStringIn
public init(stringLiteral value: StringLiteralType) {
self.init(value: value)
}

public init(stringInterpolation: DefaultStringInterpolation) {
self.init(value: stringInterpolation.description)
}

public static func ==(lhs: Path, rhs: String) -> Bool {
lhs.value == rhs
}
}

10 changes: 4 additions & 6 deletions Sources/SimpleHTTP/Request/Request+URLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ extension Request {
if let decoder = accepting {
return request.settingHeaders([.accept: type(of: decoder).contentType.value])
}

return request
}

private func toURLRequest(encoder: ContentDataEncoder) throws -> URLRequest {
var urlRequest = try URLRequest(url: URL(from: self))

urlRequest.httpMethod = method.rawValue.uppercased()
urlRequest.cachePolicy = cachePolicy
urlRequest.setHeaders(headers)

if let body = body {
switch body {
case .encodable(let body):
Expand All @@ -35,10 +35,8 @@ extension Request {
try urlRequest.multipartBody(multipart)
}
}

return urlRequest
}


}

30 changes: 15 additions & 15 deletions Sources/SimpleHTTP/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@ public struct Request<Output> {
public let query: [String: QueryParam]
public var cachePolicy: URLRequest.CachePolicy = .useProtocolCachePolicy
public var headers: HTTPHeaderFields = [:]

/// Creates a request suitable for a HTTP GET
public static func get(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
self.init(path: path, method: .get, query: query, body: nil)
}

/// Creates a request suitable for a HTTP POST with a `Encodable` body
public static func post(_ path: Path, body: Encodable?, query: [String: QueryParam] = [:])
-> Self {
self.init(path: path, method: .post, query: query, body: body.map(Body.encodable))
}

/// Creates a request suitable for a HTTP POST with a `MultipartFormData` body
@_disfavoredOverload
public static func post(_ path: Path, body: MultipartFormData?, query: [String: QueryParam] = [:])
-> Self {
self.init(path: path, method: .post, query: query, body: body.map(Body.multipart))
}

/// Creates a request suitable for a HTTP PUT with a `Encodable` body
public static func put(_ path: Path, body: Encodable, query: [String: QueryParam] = [:])
-> Self {
self.init(path: path, method: .put, query: query, body: .encodable(body))
}

/// Creates a request suitable for a HTTP PUT with a `MultipartFormData` body
public static func put(_ path: Path, body: MultipartFormData, query: [String: QueryParam] = [:])
-> Self {
Expand All @@ -59,19 +59,19 @@ public struct Request<Output> {
public static func put(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
self.init(path: path, method: .put, query: query, body: nil)
}

/// Creates a request suitable for a HTTP PATCH with a `Encodable` body
public static func patch(_ path: Path, body: Encodable, query: [String: QueryParam] = [:])
-> Self {
self.init(path: path, method: .patch, query: query, body: .encodable(body))
}

/// Creates a request suitable for a HTTP PATCH with a `MultipartFormData` body
public static func patch(_ path: Path, body: MultipartFormData, query: [String: QueryParam] = [:])
-> Self {
self.init(path: path, method: .patch, query: query, body: .multipart(body))
}

/// Creates a request suitable for a HTTP DELETE
public static func delete(_ path: Path, query: [String: QueryParam] = [:]) -> Self {
self.init(path: path, method: .delete, query: query, body: nil)
Expand All @@ -81,7 +81,7 @@ public struct Request<Output> {
public static func delete(_ path: Path, body: Encodable, query: [String: QueryParam] = [:]) -> Self {
self.init(path: path, method: .delete, query: query, body: nil)
}

/// Creates a Request.
///
/// Use this init only if default provided static initializers (`.get`, `.post`, `.put`, `patch`, `.delete`) do not suit your needs.
Expand All @@ -91,22 +91,22 @@ public struct Request<Output> {
self.body = body
self.query = query
}

/// Adds headers to the request
public func headers(_ newHeaders: [HTTPHeader: String]) -> Self {
var request = self

request.headers.merge(newHeaders) { $1 }

return request
}

/// Configures request cache policy
public func cachePolicy(_ policy: URLRequest.CachePolicy) -> Self {
var request = self

request.cachePolicy = policy

return request
}
}
10 changes: 5 additions & 5 deletions Sources/SimpleHTTP/Request/URL+Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ extension URL {
guard var components = URLComponents(string: request.path.value) else {
throw URLComponents.Error.invalid(path: request.path)
}

let queryItems = (components.queryItems ?? []) + request.query.queryItems

components.queryItems = queryItems.isEmpty ? nil : queryItems

guard let url = components.url else {
throw URLComponents.Error.cannotGenerateURL(components: components)
}

self = url
}
}
Expand All @@ -30,7 +30,7 @@ extension URLComponents {
}

extension Dictionary where Key == String, Value == String {
fileprivate var queryItems: [URLQueryItem] {
fileprivate var queryItems: [URLQueryItem] {
map { URLQueryItem(name: $0.key, value: $0.value) }
}
}
2 changes: 1 addition & 1 deletion Sources/SimpleHTTP/Response/DataResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension URLDataResponse {
guard let decoder = errorDecoder, !data.isEmpty else {
throw error
}

throw try decoder(data)
}
}
Expand Down
Loading

0 comments on commit ecf2743

Please sign in to comment.