-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from KarimEbrahemAbdelaziz/feature/add-support…
…-to-all-methods Feature/add support to all methods
- Loading branch information
Showing
11 changed files
with
236 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// RequestParameters.swift | ||
// | ||
// | ||
// Created by KarimEbrahem on 7/19/20. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
public enum RequestParameters { | ||
case body([String: String]?) | ||
case url([String: String]?) | ||
|
||
var parameters: [String: String]? { | ||
switch self { | ||
case .body(let parameters), .url(let parameters): | ||
return parameters | ||
} | ||
} | ||
|
||
var encoder: ParameterEncoder { | ||
switch self { | ||
case .body: | ||
return JSONParameterEncoder.default | ||
case .url: | ||
return URLEncodedFormParameterEncoder.default | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// | ||
// ZenError.swift | ||
// | ||
// | ||
// Created by KarimEbrahem on 7/19/20. | ||
// | ||
|
||
import Foundation | ||
|
||
public enum ZenError: Error { | ||
case RequestBuilderFailed | ||
case DecodingResponseFailed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// ZenRequest.swift | ||
// | ||
// Created by Karim Ebrahem on 4/7/20. | ||
// | ||
|
||
import Foundation | ||
import Alamofire | ||
|
||
public typealias Service<Response> = (_ completionHandler: @escaping (Result<Response, Error>) -> Void) -> Void | ||
|
||
@propertyWrapper | ||
public class ZenRequest<T> where T: Codable { | ||
|
||
private var url: URL | ||
private var path: String? | ||
private var method: HTTPMethod? | ||
private var headers: [String: Any]? | ||
private var parameters: RequestParameters? | ||
private var request: URLRequest? | ||
|
||
public var projectedValue: ZenRequest<T> { return self } | ||
|
||
public init(_ url: String) { | ||
self.url = URL(string: url)! | ||
} | ||
|
||
@discardableResult | ||
func set(method: HTTPMethod) -> Self { | ||
self.method = method | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func set(path: String) -> Self { | ||
self.path = path | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func set(headers: [String: Any]?) -> Self { | ||
self.headers = headers | ||
return self | ||
} | ||
|
||
@discardableResult | ||
public func set(parameters: RequestParameters?) -> Self { | ||
self.parameters = parameters | ||
return self | ||
} | ||
|
||
public func build() throws { | ||
do { | ||
var urlRequest = URLRequest(url: url.appendingPathComponent(path ?? ""), | ||
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, | ||
timeoutInterval: 25) | ||
urlRequest.httpMethod = method?.rawValue | ||
|
||
headers?.forEach { | ||
urlRequest.addValue($0.value as! String, forHTTPHeaderField: $0.key) | ||
} | ||
|
||
if let params = parameters { | ||
urlRequest = try buildRequestParams(urlRequest, params: params) | ||
} | ||
|
||
self.request = urlRequest | ||
} catch { | ||
throw ZenError.RequestBuilderFailed | ||
} | ||
} | ||
|
||
fileprivate func buildRequestParams(_ urlRequest: URLRequest, params: RequestParameters) throws -> URLRequest { | ||
var urlRequest = urlRequest | ||
urlRequest = try params.encoder.encode(params.parameters, into: urlRequest) | ||
return urlRequest | ||
} | ||
|
||
public var wrappedValue: Service<T> { | ||
get { | ||
return { completion in | ||
guard let request = self.request else { return } | ||
|
||
AF.request(request) | ||
.responseData { (response: AFDataResponse<Data>) in | ||
|
||
switch response.result { | ||
case .success(let data): | ||
do { | ||
let responseData = try JSONDecoder().decode(T.self, from: data) | ||
completion(.success(responseData)) | ||
} catch { | ||
completion(.failure(ZenError.DecodingResponseFailed)) | ||
} | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// | ||
// Todo.swift | ||
// | ||
// | ||
// Created by KarimEbrahem on 7/19/20. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Todo: Codable { | ||
var userId: Int | ||
var id: Int | ||
var title: String | ||
var completed: Bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// APIClient.swift | ||
// | ||
// | ||
// Created by KarimEbrahem on 7/19/20. | ||
// | ||
|
||
import Foundation | ||
import Zen | ||
|
||
class APIClient { | ||
@ZenRequest<Todo>("https://jsonplaceholder.typicode.com/todos/") | ||
static var fetchTodo: Service<Todo> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters