Skip to content

Commit 2f2fbc0

Browse files
algolia-botFluf22
andcommitted
chore: generated code for commit c0bf5709. [skip ci]
algolia/api-clients-automation@c0bf570 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]>
1 parent 126788a commit 2f2fbc0

File tree

13 files changed

+1662
-1107
lines changed

13 files changed

+1662
-1107
lines changed

Sources/Abtesting/AbtestingClient.swift

Lines changed: 86 additions & 58 deletions
Large diffs are not rendered by default.

Sources/Analytics/AnalyticsClient.swift

Lines changed: 166 additions & 116 deletions
Large diffs are not rendered by default.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// RequestOptions.swift
3+
//
4+
//
5+
// Created by Algolia on 11/01/2024.
6+
//
7+
8+
import Foundation
9+
10+
public class RequestOptions {
11+
private(set) var headers: [String: String]
12+
private(set) var queryItems: [URLQueryItem]
13+
private(set) var readTimeout: TimeInterval?
14+
private(set) var writeTimeout: TimeInterval?
15+
private(set) var body: [String: Any?]
16+
17+
public init(
18+
headers: [String: String] = [:],
19+
queryItems: [URLQueryItem] = [],
20+
readTimeout: TimeInterval? = nil,
21+
writeTimeout: TimeInterval? = nil,
22+
body: [String: Any?] = [:]
23+
) {
24+
self.headers = headers
25+
self.queryItems = queryItems
26+
self.readTimeout = readTimeout
27+
self.writeTimeout = writeTimeout
28+
self.body = body
29+
}
30+
31+
public func timeout(for callType: CallType) -> TimeInterval? {
32+
switch callType {
33+
case .read:
34+
return readTimeout
35+
case .write:
36+
return writeTimeout
37+
}
38+
}
39+
}

Sources/Core/Helpers/JSONDataEncoding.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct JSONDataEncoding {
2727
/// - throws: An `Error` if the encoding process encounters an error.
2828
///
2929
/// - returns: The encoded request.
30-
public func encode(_ urlRequest: URLRequest, with parameters: [String: Any]?) -> URLRequest {
30+
public func encode(_ urlRequest: URLRequest, with parameters: [String: Any?]?) -> URLRequest {
3131
var urlRequest = urlRequest
3232

3333
guard let jsonData = parameters?[JSONDataEncoding.jsonDataKey] as? Data, !jsonData.isEmpty
@@ -44,14 +44,25 @@ public struct JSONDataEncoding {
4444
return urlRequest
4545
}
4646

47-
public static func encodingParameters(jsonData: Data?) -> [String: Any]? {
48-
var returnedParams: [String: Any]?
47+
public static func encodingParameters(jsonData: Data?) -> [String: Any?]? {
48+
var returnedParams: [String: Any?]?
4949
if let jsonData = jsonData, !jsonData.isEmpty {
50-
var params: [String: Any] = [:]
50+
var params: [String: Any?] = [:]
5151
params[jsonDataKey] = jsonData
5252
returnedParams = params
5353
}
5454
return returnedParams
5555
}
5656

57+
public static func encodingParameters(jsonData: [String: Any?]?) -> [String: Any?]? {
58+
var returnedParams: [String: Any?]?
59+
let jsonEncodedBody = try? JSONSerialization.data(withJSONObject: jsonData, options: [])
60+
if let jsonEncodedBody = jsonEncodedBody, !jsonEncodedBody.isEmpty {
61+
var params: [String: Any?] = [:]
62+
params[jsonDataKey] = jsonEncodedBody
63+
returnedParams = params
64+
}
65+
return returnedParams
66+
}
67+
5768
}

Sources/Core/Networking/Transporter.swift

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,27 @@ open class Transporter {
2626
open class RequestBuilder<T> {
2727
var credential: URLCredential?
2828
var headers: [String: String]
29-
public let parameters: [String: Any]?
29+
public var parameters: [String: Any?]?
3030
public let method: String
3131
public let path: String
32-
public let queryItems: [URLQueryItem]?
32+
public var queryItems: [URLQueryItem]
3333
public let requestTask: RequestTask = RequestTask()
34-
public let requiresAuthentication: Bool
3534
public let hostIterator: HostIterator
35+
public let timeout: TimeInterval
3636

3737
public let transporter: Transporter
3838

3939
/// Optional block to obtain a reference to the request's progress instance when available.
4040
public var onProgressReady: ((Progress) -> Void)?
4141

4242
required public init(
43-
method: String, path: String, queryItems: [URLQueryItem]?, parameters: [String: Any]?,
44-
headers: [String: String] = [:],
45-
requiresAuthentication: Bool, transporter: Transporter
43+
method: String, path: String, queryItems: [URLQueryItem]?, parameters: [String: Any?]?,
44+
headers: [String: String] = [:], transporter: Transporter, requestOptions: RequestOptions? = nil
4645
) {
4746
self.method = method
4847
self.path = path
49-
self.queryItems = queryItems
50-
self.parameters = parameters
48+
self.queryItems = queryItems ?? []
5149
self.headers = headers
52-
self.requiresAuthentication = requiresAuthentication
5350
self.transporter = transporter
5451

5552
let httpMethod = HTTPMethod(rawValue: method)
@@ -59,6 +56,30 @@ open class RequestBuilder<T> {
5956
}
6057

6158
self.hostIterator = transporter.retryStrategy.retryableHosts(for: httpMethod.toCallType())
59+
60+
self.timeout =
61+
requestOptions?.timeout(for: httpMethod.toCallType())
62+
?? self.transporter.configuration.timeout(for: httpMethod.toCallType())
63+
64+
if let requestOptions = requestOptions {
65+
self.queryItems.append(contentsOf: requestOptions.queryItems)
66+
67+
for (key, value) in requestOptions.headers {
68+
self.headers.updateValue(value, forKey: key)
69+
}
70+
71+
if requestOptions.body == nil && parameters == nil {
72+
self.parameters = nil
73+
return
74+
}
75+
76+
if requestOptions.body != nil {
77+
self.parameters = JSONDataEncoding.encodingParameters(jsonData: requestOptions.body)
78+
return
79+
}
80+
}
81+
82+
self.parameters = parameters
6283
}
6384

6485
open func addHeaders(_ aHeaders: [String: String]) {
@@ -106,11 +127,6 @@ open class RequestBuilder<T> {
106127
}
107128
return self
108129
}
109-
110-
open func addCredential() -> Self {
111-
credential = Transporter.credential
112-
return self
113-
}
114130
}
115131

116132
public protocol RequestBuilderFactory {

Sources/Core/Networking/URLSessionImplementations.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ open class URLSessionRequestBuilder<T>: RequestBuilder<T> {
6060
public var taskDidReceiveChallenge: AlgoliaSearchClientAPIChallengeHandler?
6161

6262
required public init(
63-
method: String, path: String, queryItems: [URLQueryItem]?, parameters: [String: Any]?,
64-
headers: [String: String] = [:],
65-
requiresAuthentication: Bool, transporter: Transporter
63+
method: String, path: String, queryItems: [URLQueryItem]?, parameters: [String: Any?]?,
64+
headers: [String: String] = [:], transporter: Transporter, requestOptions: RequestOptions? = nil
6665
) {
6766
super.init(
6867
method: method, path: path, queryItems: queryItems, parameters: parameters, headers: headers,
69-
requiresAuthentication: requiresAuthentication, transporter: transporter)
68+
transporter: transporter, requestOptions: requestOptions)
7069
}
7170

7271
/**
@@ -291,7 +290,7 @@ open class URLSessionDecodableRequestBuilder<T: Decodable>: URLSessionRequestBui
291290
) throws -> URLRequest {
292291
var superReq = try super.createURLRequest(
293292
urlSession: urlSession, method: method, host: host, encoding: encoding, headers: headers)
294-
superReq.timeoutInterval = self.transporter.configuration.timeout(for: method.toCallType())
293+
superReq.timeoutInterval = self.timeout
295294

296295
guard let url = superReq.url else { throw URLRequest.FormatError.missingURL }
297296
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false) else {
@@ -422,11 +421,11 @@ public enum HTTPMethod: String {
422421
}
423422

424423
public protocol ParameterEncoding {
425-
func encode(_ urlRequest: URLRequest, with parameters: [String: Any]?) throws -> URLRequest
424+
func encode(_ urlRequest: URLRequest, with parameters: [String: Any?]?) throws -> URLRequest
426425
}
427426

428427
private class URLEncoding: ParameterEncoding {
429-
func encode(_ urlRequest: URLRequest, with parameters: [String: Any]?) throws -> URLRequest {
428+
func encode(_ urlRequest: URLRequest, with parameters: [String: Any?]?) throws -> URLRequest {
430429

431430
var urlRequest = urlRequest
432431

0 commit comments

Comments
 (0)