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
enum AppStorageKeys: String { | |
case activeEnvironment | |
} |
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
class TypeSafeUserDefaults { | |
enum IntegerKey: StringLiteralType, RawRepresentable { | |
case myIntegerValue | |
} | |
enum StringKey: StringLiteralType, RawRepresentable { | |
case myStringValue | |
} | |
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
// | |
// OreCalculator.swift | |
// AdventOfCode2019-Day14 | |
// | |
// Created by Dave Poirier on 2019-12-14. | |
// Copyright © 2019 Soft.io. All rights reserved. | |
// | |
import Foundation |
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
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 |
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
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 { |
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
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 | |
} |
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
import Foundation | |
protocol APIEndpoint { | |
func endpoint() -> String | |
} | |
class APIRequest { | |
struct ErrorResponse: Codable { | |
let status: String | |
let code: Int |
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
APILoginRequest(email: email, password: password) | |
.dispatch( | |
onSuccess: { (successResponse) in | |
... | |
}, | |
onFailure: { (errorResponse, error) in | |
... | |
}) |
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
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( |
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
struct APILoginSuccessResponse: Codable { | |
let status: String | |
let accesstoken: String | |
let refreshtoken: String | |
let expiresin: Int | |
} |
NewerOlder