Skip to content

Commit 6261aa2

Browse files
committed
Adjust API templates
1 parent 5d37bc1 commit 6261aa2

File tree

8 files changed

+56
-138
lines changed

8 files changed

+56
-138
lines changed

Templates/Swift/Common/APIClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ public class {{ options.name }}Client {
269269
if response.statusCode == HttpStatusCode.unauthorized.rawValue
270270
&& currentUnauthorizedRetryCount < maxUnauthorizedRetryCount
271271
&& IDKit.isSessionAvailable {
272-
IDKit.apiInducedRefresh { [weak self] error in
273-
guard let error = error else {
272+
IDKit.refreshToken { [weak self] result in
273+
guard case .failure(let error) = result else {
274274
self?.makeRequest(request,
275275
behaviours: requestBehaviour.behaviours,
276276
currentUnauthorizedRetryCount: currentUnauthorizedRetryCount + 1,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{% include "Common/Includes/Header.stencil" %}
2+
3+
import Foundation
4+
5+
public struct API {
6+
/**
7+
The access token to be used for API requests.
8+
9+
This token will be set automatically when using IDKit.
10+
*/
11+
public static var accessToken: String?
12+
}
13+
14+
public extension API {
15+
/**
16+
Determines if an API request should be retried.
17+
18+
It takes into account if the request has failed due to network connection errors or timeouts and if the maximum number of retries has been exceeded.
19+
20+
- parameter currentRetryCount: The current number of retries for a specific request.
21+
- parameter maxRetryCount: The maximum number of retries allowed for a specific request.
22+
- parameter response: The most recent URL response of a specific request or `nil` if not available.
23+
*/
24+
static func shouldRetryRequest(currentRetryCount: Int,
25+
maxRetryCount: Int,
26+
response: URLResponse?) -> Bool {
27+
guard currentRetryCount <= maxRetryCount else { return false }
28+
guard let response = response as? HTTPURLResponse else { return true }
29+
return response.statusCode == HttpStatusCode.requestTimeout.rawValue
30+
}
31+
32+
/**
33+
Returns the number of seconds an API request should be delayed before the next retry is executed.
34+
35+
It calculates the number of seconds based on an exponential backoff algorithm.
36+
37+
- parameter currentRetryCount: The current number of retries for a specific request.
38+
- parameter delayUpperBound: The maximum number of seconds a request should be delayed. Defaults to `64`.
39+
- returns: The request delay in seconds.
40+
*/
41+
static func nextExponentialBackoffRequestDelay(currentRetryCount: Int, delayUpperBound: Int = 64) -> Int {
42+
let nextDelayIteration = NSDecimalNumber(decimal: pow(2, currentRetryCount - 1))
43+
let delay = min(Int(truncating: nextDelayIteration), delayUpperBound)
44+
return delay
45+
}
46+
}

Templates/Swift/Fueling/template.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ templateFiles:
5757
destination: "./../../Generated/Common/AnyCodable.swift"
5858
- path: Common/URLEncoding.swift
5959
destination: "./../../Generated/Common/URLEncoding.swift"
60+
- path: Common/APIContainer.swift
61+
destination: "./../../Generated/Common/API.swift"
6062
- path: Common/Enum.swift
6163
context: enums
6264
destination: "Enums/{{ enumName }}.swift"

Templates/Swift/GeoJSON/template.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

Templates/Swift/POI/template.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ templateFiles:
5757
destination: "./../../Generated/Common/AnyCodable.swift"
5858
- path: Common/URLEncoding.swift
5959
destination: "./../../Generated/Common/URLEncoding.swift"
60+
- path: Common/APIContainer.swift
61+
destination: "./../../Generated/Common/API.swift"
6062
- path: Common/Enum.swift
6163
context: enums
6264
destination: "Enums/{{ enumName }}.swift"

Templates/Swift/Pay/template.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ templateFiles:
5757
destination: "./../../Generated/Common/AnyCodable.swift"
5858
- path: Common/URLEncoding.swift
5959
destination: "./../../Generated/Common/URLEncoding.swift"
60+
- path: Common/APIContainer.swift
61+
destination: "./../../Generated/Common/API.swift"
6062
- path: Common/Enum.swift
6163
context: enums
6264
destination: "Enums/{{ enumName }}.swift"

Templates/Swift/User/template.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ templateFiles:
5757
destination: "./../../Generated/Common/AnyCodable.swift"
5858
- path: Common/URLEncoding.swift
5959
destination: "./../../Generated/Common/URLEncoding.swift"
60+
- path: Common/APIContainer.swift
61+
destination: "./../../Generated/Common/API.swift"
6062
- path: Common/Enum.swift
6163
context: enums
6264
destination: "Enums/{{ enumName }}.swift"

Templates/Swift/Wallet/template.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

0 commit comments

Comments
 (0)