Skip to content

Commit 5a10763

Browse files
refactor: Transport and logging improvements (#730)
* Make Commands independent from Foundation networking * Rename OnlineTestCase -> IntegrationTestCase * Refactor HTTPRequest and RetryStrategy in order to propagate intermediate retry errors in the noReachableHosts error * Add test for HTTPRequest * Improve logging * Change ExpressibleByStringLiteral -> ExpressibleByStringInterpolation
1 parent c8aa2bd commit 5a10763

File tree

95 files changed

+1033
-753
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1033
-753
lines changed

Sources/AlgoliaSearchClient/Client/Search/SearchClient+Wait.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public extension Client {
2020
@discardableResult func taskStatus(for taskID: AppTaskID,
2121
requestOptions: RequestOptions? = nil,
2222
completion: @escaping ResultCallback<TaskInfo>) -> Operation & TransportTask {
23-
let command = Command.Advanced.TaskStatus(taskID: taskID, requestOptions: requestOptions)
23+
let command = Command.Advanced.AppTaskStatus(taskID: taskID, requestOptions: requestOptions)
2424
return execute(command, completion: completion)
2525
}
2626

@@ -32,7 +32,7 @@ public extension Client {
3232
*/
3333
@discardableResult func taskStatus(for taskID: AppTaskID,
3434
requestOptions: RequestOptions? = nil) throws -> TaskInfo {
35-
let command = Command.Advanced.TaskStatus(taskID: taskID, requestOptions: requestOptions)
35+
let command = Command.Advanced.AppTaskStatus(taskID: taskID, requestOptions: requestOptions)
3636
return try execute(command)
3737
}
3838

Sources/AlgoliaSearchClient/Client/Search/SearchClient.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ extension SearchClient: TransportContainer {}
7777

7878
extension SearchClient {
7979

80-
func execute<Output: Codable & AppTask>(_ command: AlgoliaCommand, completion: @escaping ResultAppTaskCallback<Output>) -> Operation & TransportTask {
80+
func execute<Command: AlgoliaCommand, Output: Codable & AppTask>(_ command: Command, completion: @escaping ResultAppTaskCallback<Output>) -> Operation & TransportTask {
8181
transport.execute(command, transform: WaitableWrapper.wrap(with: self), completion: completion)
8282
}
8383

84-
func execute<Output: Codable & AppTask>(_ command: AlgoliaCommand) throws -> WaitableWrapper<Output> {
84+
func execute<Command: AlgoliaCommand, Output: Codable & AppTask>(_ command: Command) throws -> WaitableWrapper<Output> {
8585
try transport.execute(command, transform: WaitableWrapper.wrap(with: self))
8686
}
8787

Sources/AlgoliaSearchClient/Command/AlgoliaCommand.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66
//
77

88
import Foundation
9-
#if canImport(FoundationNetworking)
10-
import FoundationNetworking
11-
#endif
129

1310
protocol AlgoliaCommand {
11+
12+
associatedtype Path: PathComponent
1413

14+
var method: HTTPMethod { get }
1515
var callType: CallType { get }
16-
var urlRequest: URLRequest { get }
16+
var path: Path { get }
17+
var body: Data? { get }
1718
var requestOptions: RequestOptions? { get }
1819

1920
}
21+
22+
extension AlgoliaCommand {
23+
24+
var body: Data? {
25+
return nil
26+
}
27+
28+
}

Sources/AlgoliaSearchClient/Command/Command+ABTest.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,73 @@
66
//
77

88
import Foundation
9-
#if canImport(FoundationNetworking)
10-
import FoundationNetworking
11-
#endif
129

1310
extension Command {
1411

1512
enum ABTest {
1613

1714
struct Add: AlgoliaCommand {
1815

16+
let method: HTTPMethod = .post
1917
let callType: CallType = .write
20-
let urlRequest: URLRequest
18+
let path: Path = .ABTestsV2
19+
let body: Data?
2120
let requestOptions: RequestOptions?
2221

2322
init(abTest: AlgoliaSearchClient.ABTest, requestOptions: RequestOptions?) {
2423
self.requestOptions = requestOptions
25-
self.urlRequest = .init(method: .post, path: Path.ABTestsV2, body: abTest.httpBody, requestOptions: self.requestOptions)
24+
self.body = abTest.httpBody
2625
}
2726

2827
}
2928

3029
struct Get: AlgoliaCommand {
3130

31+
let method: HTTPMethod = .get
3232
let callType: CallType = .read
33-
let urlRequest: URLRequest
33+
let path: ABTestRoute
3434
let requestOptions: RequestOptions?
3535

3636
init(abTestID: ABTestID, requestOptions: RequestOptions?) {
3737
self.requestOptions = requestOptions
38-
self.urlRequest = .init(method: .get, path: .ABTestsV2 >>> ABTestRoute.ABTestID(abTestID), requestOptions: self.requestOptions)
38+
self.path = (.ABTestsV2 >>> .ABTestID(abTestID))
3939
}
4040

4141
}
4242

4343
struct Stop: AlgoliaCommand {
4444

45+
let method: HTTPMethod = .post
4546
let callType: CallType = .write
46-
let urlRequest: URLRequest
47+
let path: ABTestCompletion
4748
let requestOptions: RequestOptions?
4849

4950
init(abTestID: ABTestID, requestOptions: RequestOptions?) {
5051
self.requestOptions = requestOptions
51-
self.urlRequest = .init(method: .post, path: .ABTestsV2 >>> .ABTestID(abTestID) >>> ABTestCompletion.stop, requestOptions: self.requestOptions)
52+
self.path = (.ABTestsV2 >>> .ABTestID(abTestID) >>> .stop)
5253
}
5354

5455
}
5556

5657
struct Delete: AlgoliaCommand {
5758

59+
let method: HTTPMethod = .delete
5860
let callType: CallType = .write
59-
let urlRequest: URLRequest
61+
let path: ABTestRoute
6062
let requestOptions: RequestOptions?
6163

6264
init(abTestID: ABTestID, requestOptions: RequestOptions?) {
6365
self.requestOptions = requestOptions
64-
self.urlRequest = .init(method: .delete, path: .ABTestsV2 >>> ABTestRoute.ABTestID(abTestID), requestOptions: self.requestOptions)
66+
self.path = (.ABTestsV2 >>> .ABTestID(abTestID))
6567
}
6668

6769
}
6870

6971
struct List: AlgoliaCommand {
7072

73+
let method: HTTPMethod = .get
7174
let callType: CallType = .read
72-
let urlRequest: URLRequest
75+
let path: Path = .ABTestsV2
7376
let requestOptions: RequestOptions?
7477

7578
init(offset: Int?, limit: Int?, requestOptions: RequestOptions?) {
@@ -78,7 +81,6 @@ extension Command {
7881
.offset: offset.flatMap(String.init),
7982
.limit: limit.flatMap(String.init)
8083
])
81-
self.urlRequest = .init(method: .get, path: Path.ABTestsV2, requestOptions: self.requestOptions)
8284
}
8385

8486
}

Sources/AlgoliaSearchClient/Command/Command+APIKeys.swift

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,93 @@
66
//
77

88
import Foundation
9-
#if canImport(FoundationNetworking)
10-
import FoundationNetworking
11-
#endif
129

1310
extension Command {
1411

1512
enum APIKey {
1613

1714
struct Add: AlgoliaCommand {
1815

16+
let method: HTTPMethod = .post
1917
let callType: CallType = .write
20-
let urlRequest: URLRequest
18+
let path: Path = .keysV1
19+
let body: Data?
2120
let requestOptions: RequestOptions?
2221

2322
init(parameters: APIKeyParameters, requestOptions: RequestOptions?) {
24-
self.urlRequest = URLRequest(method: .post, path: Path.keysV1, body: parameters.httpBody, requestOptions: requestOptions)
23+
self.body = parameters.httpBody
2524
self.requestOptions = requestOptions
2625
}
2726

2827
}
2928

3029
struct Update: AlgoliaCommand {
3130

31+
let method: HTTPMethod = .put
3232
let callType: CallType = .write
33-
let urlRequest: URLRequest
33+
let path: APIKeyCompletion
34+
let body: Data?
3435
let requestOptions: RequestOptions?
3536

3637
init(apiKey: AlgoliaSearchClient.APIKey, parameters: APIKeyParameters, requestOptions: RequestOptions?) {
37-
self.urlRequest = URLRequest(method: .put, path: .keysV1 >>> APIKeyCompletion.apiKey(apiKey), body: parameters.httpBody, requestOptions: requestOptions)
38+
self.path = (.keysV1 >>> .apiKey(apiKey))
39+
self.body = parameters.httpBody
3840
self.requestOptions = requestOptions
3941
}
4042

4143
}
4244

4345
struct Delete: AlgoliaCommand {
4446

47+
let method: HTTPMethod = .delete
4548
let callType: CallType = .write
46-
let urlRequest: URLRequest
49+
let path: APIKeyCompletion
4750
let requestOptions: RequestOptions?
4851

4952
init(apiKey: AlgoliaSearchClient.APIKey, requestOptions: RequestOptions?) {
50-
self.urlRequest = URLRequest(method: .delete, path: .keysV1 >>> APIKeyCompletion.apiKey(apiKey), requestOptions: requestOptions)
53+
self.path = (.keysV1 >>> .apiKey(apiKey))
5154
self.requestOptions = requestOptions
5255
}
5356

5457
}
5558

5659
struct Restore: AlgoliaCommand {
5760

61+
let method: HTTPMethod = .post
5862
let callType: CallType = .write
59-
let urlRequest: URLRequest
63+
let path: APIKeyCompletion
6064
let requestOptions: RequestOptions?
6165

6266
init(apiKey: AlgoliaSearchClient.APIKey, requestOptions: RequestOptions?) {
63-
self.urlRequest = URLRequest(method: .post, path: .keysV1 >>> APIKeyCompletion.restoreAPIKey(apiKey), requestOptions: requestOptions)
67+
self.path = (.keysV1 >>> .restoreAPIKey(apiKey))
6468
self.requestOptions = requestOptions
6569
}
6670

6771
}
6872

6973
struct Get: AlgoliaCommand {
7074

75+
let method: HTTPMethod = .get
7176
let callType: CallType = .read
72-
let urlRequest: URLRequest
77+
let path: APIKeyCompletion
7378
let requestOptions: RequestOptions?
7479

7580
init(apiKey: AlgoliaSearchClient.APIKey, requestOptions: RequestOptions?) {
76-
self.urlRequest = URLRequest(method: .get, path: .keysV1 >>> APIKeyCompletion.apiKey(apiKey), requestOptions: requestOptions)
81+
self.path = (.keysV1 >>> .apiKey(apiKey))
7782
self.requestOptions = requestOptions
7883
}
7984

8085
}
8186

8287
struct List: AlgoliaCommand {
8388

89+
let method: HTTPMethod = .get
8490
let callType: CallType = .read
85-
let urlRequest: URLRequest
91+
let path: Path
8692
let requestOptions: RequestOptions?
8793

8894
init(requestOptions: RequestOptions?) {
89-
self.urlRequest = URLRequest(method: .get, path: Path.keysV1, requestOptions: requestOptions)
95+
self.path = .keysV1
9096
self.requestOptions = requestOptions
9197
}
9298

Sources/AlgoliaSearchClient/Command/Command+Advanced.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,45 @@
66
//
77

88
import Foundation
9-
#if canImport(FoundationNetworking)
10-
import FoundationNetworking
11-
#endif
129

1310
extension Command {
1411

1512
enum Advanced {
1613

1714
struct TaskStatus: AlgoliaCommand {
1815

16+
let method: HTTPMethod = .get
1917
let callType: CallType = .read
20-
let urlRequest: URLRequest
18+
let path: IndexCompletion
2119
let requestOptions: RequestOptions?
2220

2321
init(indexName: IndexName, taskID: TaskID, requestOptions: RequestOptions?) {
2422
self.requestOptions = requestOptions
25-
let path = .indexesV1 >>> .index(indexName) >>> IndexCompletion.task(for: taskID)
26-
urlRequest = .init(method: .get, path: path, requestOptions: self.requestOptions)
23+
self.path = (.indexesV1 >>> .index(indexName) >>> .task(for: taskID))
2724
}
2825

26+
}
27+
28+
struct AppTaskStatus: AlgoliaCommand {
29+
30+
let method: HTTPMethod = .get
31+
let callType: CallType = .read
32+
let path: TaskCompletion
33+
let requestOptions: RequestOptions?
34+
2935
init(taskID: AppTaskID, requestOptions: RequestOptions?) {
3036
self.requestOptions = requestOptions
31-
let path = .task >>> TaskCompletion.task(withID: taskID)
32-
urlRequest = .init(method: .get, path: path, requestOptions: self.requestOptions)
37+
self.path = (.task >>> TaskCompletion.task(withID: taskID))
3338
}
3439

3540
}
3641

42+
3743
struct GetLogs: AlgoliaCommand {
3844

45+
let method: HTTPMethod = .get
3946
let callType: CallType = .read
40-
let urlRequest: URLRequest
47+
let path: Path = .logs
4148
let requestOptions: RequestOptions?
4249

4350
init(indexName: IndexName?, offset: Int?, length: Int?, logType: LogType, requestOptions: RequestOptions?) {
@@ -49,7 +56,6 @@ extension Command {
4956
.merging([.type: logType.rawValue])
5057
)
5158
self.requestOptions = requestOptions
52-
urlRequest = .init(method: .get, path: Path.logs, requestOptions: self.requestOptions)
5359
}
5460

5561
}

Sources/AlgoliaSearchClient/Command/Command+Answers.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,25 @@
66
//
77

88
import Foundation
9-
#if canImport(FoundationNetworking)
10-
import FoundationNetworking
11-
#endif
129

1310
extension Command {
1411

1512
enum Answers {
1613

1714
struct Find: AlgoliaCommand {
1815

16+
let method: HTTPMethod = .post
1917
let callType: CallType = .read
20-
let urlRequest: URLRequest
18+
let path: IndexCompletion
19+
let body: Data?
2120
let requestOptions: RequestOptions?
2221

2322
init(indexName: IndexName,
2423
query: AnswersQuery,
2524
requestOptions: RequestOptions?) {
2625
self.requestOptions = requestOptions
27-
let path: IndexCompletion = .answers >>> .index(indexName) >>> .prediction
28-
urlRequest = .init(method: .post, path: path, body: query.httpBody, requestOptions: self.requestOptions)
26+
self.path = (.answers >>> .index(indexName) >>> .prediction)
27+
self.body = query.httpBody
2928
}
3029

3130
}

0 commit comments

Comments
 (0)