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 refreshAuth(_ username: String, _ password: String) -> Request { | |
| return self.login(username, password, onSuccess: { | |
| }, onFailure: { error in | |
| }) | |
| } | |
| func refreshTokenOnAuthFailure(request: Siesta.Request) -> Request { | |
| return request.chained { | |
| guard case .failure(let error) = $0.response, // Did request fail… | |
| error.httpStatusCode == 401 else { // …because of expired token? |
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("**") { | |
| // Retry requests on auth failure | |
| $0.decorateRequests { | |
| self.refreshTokenOnAuthFailure(request: $1) | |
| } | |
| } |
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 Siesta | |
| class ViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| AwesomeAPI.expenses().addObserver(self) | |
| } | |
| override func viewWillAppear(_ animated: Bool) { |
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
| let jsonDecoder = JSONDecoder() | |
| let jsonDateFormatter = DateFormatter() | |
| jsonDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.A" | |
| jsonDecoder.dateDecodingStrategy = .formatted(jsonDateFormatter) |
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) | |
| } |
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
| 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.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
| 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
| @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 { |