Skip to content

Commit

Permalink
General refactor (#215)
Browse files Browse the repository at this point in the history
* Clean up handling fake requests

* Clean up method calls

* Simplify method name

* Run SwiftFormat

* Clean up and simplify logic
  • Loading branch information
3lvis authored Oct 1, 2017
1 parent a65c07d commit 2a2a41c
Show file tree
Hide file tree
Showing 18 changed files with 226 additions and 282 deletions.
2 changes: 1 addition & 1 deletion Sources/FormDataPart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum FormDataPartType {
return "image/png"
case .jpg:
return "image/jpeg"
case .custom(let value):
case let .custom(value):
return value
}
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension String {
var components = self.components(separatedBy: "/")
guard let lastComponent = components.popLast(),
let endcodedLastComponent = lastComponent.addingPercentEncoding(withAllowedCharacters: .urlQueryParametersAllowed) else {
return nil
return nil
}

return (components + [endcodedLastComponent]).joined(separator: "/")
Expand All @@ -69,27 +69,27 @@ extension FileManager {
}

extension URLRequest {
init(url: URL, requestType: Networking.RequestType, path: String, parameterType: Networking.ParameterType?, responseType: Networking.ResponseType, boundary: String, authorizationHeaderValue: String?, token: String?, authorizationHeaderKey: String, headerFields: [String: String]?) {
init(url: URL, requestType: Networking.RequestType, path _: String, parameterType: Networking.ParameterType?, responseType: Networking.ResponseType, boundary: String, authorizationHeaderValue: String?, token: String?, authorizationHeaderKey: String, headerFields: [String: String]?) {
self = URLRequest(url: url)
self.httpMethod = requestType.rawValue
httpMethod = requestType.rawValue

if let parameterType = parameterType, let contentType = parameterType.contentType(boundary) {
self.addValue(contentType, forHTTPHeaderField: "Content-Type")
addValue(contentType, forHTTPHeaderField: "Content-Type")
}

if let accept = responseType.accept {
self.addValue(accept, forHTTPHeaderField: "Accept")
addValue(accept, forHTTPHeaderField: "Accept")
}

if let authorizationHeader = authorizationHeaderValue {
self.setValue(authorizationHeader, forHTTPHeaderField: authorizationHeaderKey)
setValue(authorizationHeader, forHTTPHeaderField: authorizationHeaderKey)
} else if let token = token {
self.setValue("Bearer \(token)", forHTTPHeaderField: authorizationHeaderKey)
setValue("Bearer \(token)", forHTTPHeaderField: authorizationHeaderKey)
}

if let headerFields = headerFields {
for (key, value) in headerFields {
self.setValue(value, forHTTPHeaderField: key)
setValue(value, forHTTPHeaderField: key)
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions Sources/JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,20 @@ enum JSON: Equatable {
case array(Data, [[String: Any]])

var dictionary: [String: Any] {
get {
switch self {
case .dictionary(_, let body):
return body
default:
return [String: Any]()
}
switch self {
case let .dictionary(_, body):
return body
default:
return [String: Any]()
}
}

var array: [[String: Any]] {
get {
switch self {
case .array(_, let body):
return body
default:
return [[String: Any]]()
}
switch self {
case let .array(_, body):
return body
default:
return [[String: Any]]()
}
}

Expand Down
42 changes: 14 additions & 28 deletions Sources/Networking+HTTPRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ public extension Networking {
/// - Returns: The request identifier.
@discardableResult
public func get(_ path: String, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String {
let parameterType = parameters != nil ? ParameterType.formURLEncoded : ParameterType.none
let parameterType: ParameterType = parameters != nil ? .formURLEncoded : .none

return handleRequest(.get, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, parts: nil, responseType: .json) { result in
completion(result as! JSONResult)
}
return handleJSONRequest(.get, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, completion: completion)
}

/// Registers a fake GET request for the specified path. After registering this, every GET request to the path, will return the registered response.
Expand All @@ -42,7 +40,7 @@ public extension Networking {
///
/// - Parameter path: The path for the cancelled GET request
public func cancelGET(_ path: String) {
let url = try! self.composedURL(with: path)
let url = try! composedURL(with: path)
cancelRequest(.data, requestType: .get, url: url)
}
}
Expand All @@ -59,9 +57,7 @@ public extension Networking {
/// - Returns: The request identifier.
@discardableResult
public func put(_ path: String, parameterType: ParameterType = .json, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String {
return handleRequest(.put, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, parts: nil, responseType: .json) { result in
completion(result as! JSONResult)
}
return handleJSONRequest(.put, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, completion: completion)
}

/// Registers a fake PUT request for the specified path. After registering this, every PUT request to the path, will return the registered response.
Expand All @@ -88,7 +84,7 @@ public extension Networking {
///
/// - Parameter path: The path for the cancelled PUT request.
public func cancelPUT(_ path: String) {
let url = try! self.composedURL(with: path)
let url = try! composedURL(with: path)
cancelRequest(.data, requestType: .put, url: url)
}
}
Expand All @@ -105,9 +101,7 @@ public extension Networking {
/// - Returns: The request identifier.
@discardableResult
public func post(_ path: String, parameterType: ParameterType = .json, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String {
return handleRequest(.post, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, parts: nil, responseType: .json) { result in
completion(result as! JSONResult)
}
return handleJSONRequest(.post, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, completion: completion)
}

/// POST request to the specified path, using the provided parameters.
Expand All @@ -120,9 +114,7 @@ public extension Networking {
/// - Returns: The request identifier.
@discardableResult
public func post(_ path: String, parameters: Any? = nil, parts: [FormDataPart], completion: @escaping (_ result: JSONResult) -> Void) -> String {
return handleRequest(.post, path: path, cacheName: nil, parameterType: .multipartFormData, parameters: parameters, parts: parts, responseType: .json) { result in
completion(result as! JSONResult)
}
return handleJSONRequest(.post, path: path, parameterType: .multipartFormData, parameters: parameters, parts: parts, responseType: .json, completion: completion)
}

/// Registers a fake POST request for the specified path. After registering this, every POST request to the path, will return the registered response.
Expand All @@ -149,7 +141,7 @@ public extension Networking {
///
/// - Parameter path: The path for the cancelled POST request.
public func cancelPOST(_ path: String) {
let url = try! self.composedURL(with: path)
let url = try! composedURL(with: path)
cancelRequest(.data, requestType: .post, url: url)
}
}
Expand All @@ -165,10 +157,8 @@ public extension Networking {
/// - Returns: The request identifier.
@discardableResult
public func delete(_ path: String, parameters: Any? = nil, completion: @escaping (_ result: JSONResult) -> Void) -> String {
let parameterType = parameters != nil ? ParameterType.formURLEncoded : ParameterType.none
return handleRequest(.delete, path: path, cacheName: nil, parameterType: parameterType, parameters: parameters, parts: nil, responseType: .json) { result in
completion(result as! JSONResult)
}
let parameterType: ParameterType = parameters != nil ? .formURLEncoded : .none
return handleJSONRequest(.delete, path: path, parameterType: parameterType, parameters: parameters, responseType: .json, completion: completion)
}

/// Registers a fake DELETE request for the specified path. After registering this, every DELETE request to the path, will return the registered response.
Expand All @@ -195,7 +185,7 @@ public extension Networking {
///
/// - Parameter path: The path for the cancelled DELETE request.
public func cancelDELETE(_ path: String) {
let url = try! self.composedURL(with: path)
let url = try! composedURL(with: path)
cancelRequest(.data, requestType: .delete, url: url)
}
}
Expand Down Expand Up @@ -223,16 +213,14 @@ public extension Networking {
/// - Returns: The request identifier.
@discardableResult
public func downloadImage(_ path: String, cacheName: String? = nil, completion: @escaping (_ result: ImageResult) -> Void) -> String {
return handleRequest(.get, path: path, cacheName: cacheName, parameterType: nil, parameters: nil, parts: nil, responseType: .image) { result in
completion(result as! ImageResult)
}
return handleImageRequest(.get, path: path, cacheName: cacheName, responseType: .image, completion: completion)
}

/// Cancels the image download request for the specified path. This causes the request to complete with error code URLError.cancelled.
///
/// - Parameter path: The path for the cancelled image download request.
public func cancelImageDownload(_ path: String) {
let url = try! self.composedURL(with: path)
let url = try! composedURL(with: path)
cancelRequest(.data, requestType: .get, url: url)
}

Expand All @@ -254,9 +242,7 @@ public extension Networking {
/// - completion: A closure that gets called when the download request is completed, it contains a `data` object and an `NSError`.
@discardableResult
public func downloadData(_ path: String, cacheName: String? = nil, completion: @escaping (_ result: DataResult) -> Void) -> String {
return handleRequest(.get, path: path, cacheName: cacheName, parameterType: nil, parameters: nil, parts: nil, responseType: .data) { result in
completion(result as! DataResult)
}
return handleDataRequest(.get, path: path, cacheName: cacheName, responseType: .data, completion: completion)
}

/// Retrieves data from the cache or from the filesystem.
Expand Down
Loading

0 comments on commit 2a2a41c

Please sign in to comment.