Skip to content

Commit f98d7c4

Browse files
author
Guilherme Souza
committed
feat: auto retry requests using RetryRequestInterceptor
1 parent bfb6620 commit f98d7c4

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

Sources/Functions/FunctionsClient.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class FunctionsClient: Sendable {
5454
if let logger {
5555
interceptors.append(LoggerInterceptor(logger: logger))
5656
}
57-
57+
interceptors.append(RetryRequestInterceptor.default)
5858
let http = HTTPClient(fetch: fetch, interceptors: interceptors)
5959

6060
self.init(url: url, headers: headers, region: region, http: http)

Sources/Helpers/HTTP/HTTPHeader.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ package struct HTTPHeaders {
2323
}
2424

2525
package mutating func update(_ field: HTTPHeader) {
26-
if let index = fields.firstIndex(where: { $0.name.lowercased() == field.name.lowercased() }) {
26+
if let index = fields.firstIndex(where: { $0.canonicalName == field.canonicalName }) {
2727
fields[index] = field
2828
} else {
2929
fields.append(field)
@@ -35,12 +35,12 @@ package struct HTTPHeaders {
3535
}
3636

3737
package mutating func remove(name: String) {
38-
fields.removeAll { $0.name.lowercased() == name.lowercased() }
38+
fields.removeAll { $0.canonicalName == name.lowercased() }
3939
}
4040

4141
package func value(for name: String) -> String? {
4242
fields
43-
.firstIndex(where: { $0.name.lowercased() == name.lowercased() })
43+
.firstIndex(where: { $0.canonicalName == name.lowercased() })
4444
.map { fields[$0].value }
4545
}
4646

@@ -71,6 +71,11 @@ package struct HTTPHeaders {
7171
return Dictionary(namesAndValues, uniquingKeysWith: { _, last in last })
7272
}
7373

74+
var canonicalDictionary: [String: String] {
75+
let namesAndValues = fields.map { ($0.canonicalName, $0.value) }
76+
return Dictionary(namesAndValues, uniquingKeysWith: { _, last in last })
77+
}
78+
7479
package mutating func merge(with other: HTTPHeaders) {
7580
for field in other.fields {
7681
update(field)
@@ -137,9 +142,13 @@ package struct HTTPHeader: Sendable, Hashable {
137142
package let name: String
138143
package let value: String
139144

145+
package let canonicalName: String
146+
140147
package init(name: String, value: String) {
141148
self.name = name
142149
self.value = value
150+
151+
canonicalName = name.lowercased()
143152
}
144153
}
145154

@@ -152,6 +161,6 @@ extension HTTPHeader: CustomStringConvertible {
152161

153162
extension HTTPHeaders: Equatable {
154163
package static func == (lhs: Self, rhs: Self) -> Bool {
155-
lhs.dictionary == rhs.dictionary
164+
lhs.canonicalDictionary == rhs.canonicalDictionary
156165
}
157166
}

Sources/Helpers/HTTP/RetryRequestInterceptor.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import Foundation
1717
/// of failure, with exponential backoff between retries. You can configure the retry behavior by specifying
1818
/// the retry limit, exponential backoff base, scale, retryable HTTP methods, HTTP status codes, and URL error codes.
1919
package actor RetryRequestInterceptor: HTTPClientInterceptor {
20+
/// A ``RetryRequestInterceptor`` instance with default values.
21+
package static let `default` = RetryRequestInterceptor()
22+
2023
/// The default retry limit for the interceptor.
2124
package static let defaultRetryLimit = 2
2225
/// The default base value for exponential backoff.

Sources/PostgREST/PostgrestBuilder.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class PostgrestBuilder: @unchecked Sendable {
3131
if let logger = configuration.logger {
3232
interceptors.append(LoggerInterceptor(logger: logger))
3333
}
34+
interceptors.append(RetryRequestInterceptor.default)
3435

3536
http = HTTPClient(fetch: configuration.fetch, interceptors: interceptors)
3637

Sources/Storage/StorageApi.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class StorageApi: @unchecked Sendable {
2121
if let logger = configuration.logger {
2222
interceptors.append(LoggerInterceptor(logger: logger))
2323
}
24+
interceptors.append(RetryRequestInterceptor.default)
2425

2526
http = HTTPClient(
2627
fetch: configuration.session.fetch,

0 commit comments

Comments
 (0)