This file contains hidden or 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
fileprivate init() { | |
... | |
let jsonDecoder = JSONDecoder() | |
// –––––– Mapping from specific paths to models –––––– | |
service.configureTransformer("/status") { | |
try jsonDecoder.decode([String: String].self, from: $0.content) |
This file contains hidden or 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
func resourceChanged(_ resource: Resource, event: ResourceEvent) { | |
if let status: [String: String] = resource.typedContent() { | |
print("\(status)") | |
} | |
} |
This file contains hidden or 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
if let text: String = resource.typedContent() { | |
print(text) | |
} |
This file contains hidden or 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
private var authToken: String? { | |
didSet { | |
service.invalidateConfiguration() | |
guard let token = authToken else { return } | |
let jwt = try? JWTDecode.decode(jwt: token) | |
tokenExpiryDate = jwt?.expiresAt | |
} | |
} |
This file contains hidden or 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
@discardableResult func login(_ email: String, _ password: String, onSuccess: @escaping () -> Void, onFailure: @escaping (String) -> Void) -> Request { | |
let request = service.resource("/login") | |
.request(.post, json: ["email": email, "password": password]) | |
.onSuccess { entity in | |
guard let json: [String: String] = entity.typedContent() else { | |
onFailure("JSON parsing error") | |
return | |
} | |
guard let token = json["jwt"] else { |
This file contains hidden or 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
private var refreshTimer: Timer? | |
public private(set) var tokenExpiryDate: Date? { | |
didSet { | |
guard let tokenExpiryDate = tokenExpiryDate else { return } | |
let timeToExpire = tokenExpiryDate.timeIntervalSinceNow | |
// try to refresh JWT token before the expiration time | |
let timeToRefresh = Date(timeIntervalSinceNow: timeToExpire * 0.9) |
This file contains hidden or 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
service.configure("**") { | |
if let authToken = self.authToken { | |
$0.headers["Authorization"] = "Bearer \(authToken)" | |
} | |
} |
This file contains hidden or 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 | |
struct Expense: Decodable { | |
let amount: Float | |
let createdAt: Date | |
let description: String | |
let type: String | |
enum CodingKeys: String, CodingKey { | |
case amount |
This file contains hidden or 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
service.configureTransformer("/login", requestMethods: [.post]) { | |
try jsonDecoder.decode([String: String].self, from: $0.content) | |
} |
This file contains hidden or 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
service.configureTransformer("/expenses") { | |
try jsonDecoder.decode([Expense].self, from: $0.content) | |
} |