I hereby claim:
- I am ekscrypto on github.
- I am ekscrypto (https://keybase.io/ekscrypto) on keybase.
- I have a public key whose fingerprint is 1E2E AD78 9E76 08FA 564E EE18 E5CB 8C92 7006 157E
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
struct APILoginRequest: Codable { | |
let email: String | |
let password: String | |
} |
struct APILoginSuccessResponse: Codable { | |
let status: String | |
let accesstoken: String | |
let refreshtoken: String | |
let expiresin: Int | |
} |
extension APILoginRequest: APIEndpoint { | |
func endpoint() -> String { | |
return "/api/login" | |
} | |
func dispatch( | |
onSuccess successHandler: @escaping ((_: APILoginSuccessResponse) -> Void), | |
onFailure failureHandler: @escaping ((_: APIRequest.ErrorResponse?, _: Error) -> Void)) { | |
APIRequest.post( |
APILoginRequest(email: email, password: password) | |
.dispatch( | |
onSuccess: { (successResponse) in | |
... | |
}, | |
onFailure: { (errorResponse, error) in | |
... | |
}) |
import Foundation | |
protocol APIEndpoint { | |
func endpoint() -> String | |
} | |
class APIRequest { | |
struct ErrorResponse: Codable { | |
let status: String | |
let code: Int |
extension APIRequest { | |
public static func post<R: Codable & APIEndpoint, T: Codable, E: Codable>( | |
request: R, | |
onSuccess: @escaping ((_: T) -> Void), | |
onError: @escaping ((_: E?, _: Error) -> Void)) { | |
guard var endpointRequest = self.urlRequest(from: request) else { | |
onError(nil, APIError.invalidEndpoint) | |
return | |
} |
extension APIRequest { | |
public static func processResponse<T: Codable, E: Codable>( | |
_ dataOrNil: Data?, | |
_ urlResponseOrNil: URLResponse?, | |
_ errorOrNil: Error?, | |
onSuccess: ((_: T) -> Void), | |
onError: ((_: E?, _: Error) -> Void)) { | |
if let data = dataOrNil { | |
do { |
extension APIRequest { | |
public static func urlRequest(from request: APIEndpoint) -> URLRequest? { | |
let endpoint = request.endpoint() | |
guard let endpointUrl = URL(string: "\(AppSettings.shared.serverBaseAddress)\(endpoint)") else { | |
return nil | |
} | |
var endpointRequest = URLRequest(url: endpointUrl) | |
endpointRequest.addValue("application/json", forHTTPHeaderField: "Accept") | |
return endpointRequest |
// | |
// OreCalculator.swift | |
// AdventOfCode2019-Day14 | |
// | |
// Created by Dave Poirier on 2019-12-14. | |
// Copyright © 2019 Soft.io. All rights reserved. | |
// | |
import Foundation |