Skip to content

Commit 72dfa0e

Browse files
refactor: URL paths construction (#762)
* replace paths and custom operators with url components concatenation * replace generic parameter types with plain AlgoliaCommand where possible
1 parent 518fbf7 commit 72dfa0e

Some content is hidden

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

46 files changed

+404
-628
lines changed

Sources/AlgoliaSearchClient/Command/AlgoliaCommand.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ import Foundation
99

1010
protocol AlgoliaCommand {
1111

12-
associatedtype Path: PathComponent
13-
1412
var method: HTTPMethod { get }
1513
var callType: CallType { get }
16-
var path: Path { get }
14+
var path: URL { get }
1715
var body: Data? { get }
1816
var requestOptions: RequestOptions? { get }
1917

Sources/AlgoliaSearchClient/Command/Command+ABTest.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension Command {
1515

1616
let method: HTTPMethod = .post
1717
let callType: CallType = .write
18-
let path: Path = .ABTestsV2
18+
let path: URL = .ABTestsV2
1919
let body: Data?
2020
let requestOptions: RequestOptions?
2121

@@ -30,12 +30,14 @@ extension Command {
3030

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

3636
init(abTestID: ABTestID, requestOptions: RequestOptions?) {
3737
self.requestOptions = requestOptions
38-
self.path = (.ABTestsV2 >>> .ABTestID(abTestID))
38+
self.path = URL
39+
.ABTestsV2
40+
.appending(abTestID)
3941
}
4042

4143
}
@@ -44,12 +46,15 @@ extension Command {
4446

4547
let method: HTTPMethod = .post
4648
let callType: CallType = .write
47-
let path: ABTestCompletion
49+
let path: URL
4850
let requestOptions: RequestOptions?
4951

5052
init(abTestID: ABTestID, requestOptions: RequestOptions?) {
5153
self.requestOptions = requestOptions
52-
self.path = (.ABTestsV2 >>> .ABTestID(abTestID) >>> .stop)
54+
self.path = URL
55+
.ABTestsV2
56+
.appending(abTestID)
57+
.appending(.stop)
5358
}
5459

5560
}
@@ -58,12 +63,14 @@ extension Command {
5863

5964
let method: HTTPMethod = .delete
6065
let callType: CallType = .write
61-
let path: ABTestRoute
66+
let path: URL
6267
let requestOptions: RequestOptions?
6368

6469
init(abTestID: ABTestID, requestOptions: RequestOptions?) {
6570
self.requestOptions = requestOptions
66-
self.path = (.ABTestsV2 >>> .ABTestID(abTestID))
71+
self.path = URL
72+
.ABTestsV2
73+
.appending(abTestID)
6774
}
6875

6976
}
@@ -72,7 +79,7 @@ extension Command {
7279

7380
let method: HTTPMethod = .get
7481
let callType: CallType = .read
75-
let path: Path = .ABTestsV2
82+
let path: URL = .ABTestsV2
7683
let requestOptions: RequestOptions?
7784

7885
init(offset: Int?, limit: Int?, requestOptions: RequestOptions?) {

Sources/AlgoliaSearchClient/Command/Command+APIKeys.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension Command {
1515

1616
let method: HTTPMethod = .post
1717
let callType: CallType = .write
18-
let path: Path = .keysV1
18+
let path: URL = .keysV1
1919
let body: Data?
2020
let requestOptions: RequestOptions?
2121

@@ -30,12 +30,14 @@ extension Command {
3030

3131
let method: HTTPMethod = .put
3232
let callType: CallType = .write
33-
let path: APIKeyCompletion
33+
let path: URL
3434
let body: Data?
3535
let requestOptions: RequestOptions?
3636

3737
init(apiKey: AlgoliaSearchClient.APIKey, parameters: APIKeyParameters, requestOptions: RequestOptions?) {
38-
self.path = (.keysV1 >>> .apiKey(apiKey))
38+
self.path = URL
39+
.keysV1
40+
.appending(apiKey)
3941
self.body = parameters.httpBody
4042
self.requestOptions = requestOptions
4143
}
@@ -46,11 +48,13 @@ extension Command {
4648

4749
let method: HTTPMethod = .delete
4850
let callType: CallType = .write
49-
let path: APIKeyCompletion
51+
let path: URL
5052
let requestOptions: RequestOptions?
5153

5254
init(apiKey: AlgoliaSearchClient.APIKey, requestOptions: RequestOptions?) {
53-
self.path = (.keysV1 >>> .apiKey(apiKey))
55+
self.path = URL
56+
.keysV1
57+
.appending(apiKey)
5458
self.requestOptions = requestOptions
5559
}
5660

@@ -60,11 +64,14 @@ extension Command {
6064

6165
let method: HTTPMethod = .post
6266
let callType: CallType = .write
63-
let path: APIKeyCompletion
67+
let path: URL
6468
let requestOptions: RequestOptions?
6569

6670
init(apiKey: AlgoliaSearchClient.APIKey, requestOptions: RequestOptions?) {
67-
self.path = (.keysV1 >>> .restoreAPIKey(apiKey))
71+
self.path = URL
72+
.keysV1
73+
.appending(apiKey)
74+
.appending(.restore)
6875
self.requestOptions = requestOptions
6976
}
7077

@@ -74,11 +81,13 @@ extension Command {
7481

7582
let method: HTTPMethod = .get
7683
let callType: CallType = .read
77-
let path: APIKeyCompletion
84+
let path: URL
7885
let requestOptions: RequestOptions?
7986

8087
init(apiKey: AlgoliaSearchClient.APIKey, requestOptions: RequestOptions?) {
81-
self.path = (.keysV1 >>> .apiKey(apiKey))
88+
self.path = URL
89+
.keysV1
90+
.appending(apiKey)
8291
self.requestOptions = requestOptions
8392
}
8493

@@ -88,11 +97,10 @@ extension Command {
8897

8998
let method: HTTPMethod = .get
9099
let callType: CallType = .read
91-
let path: Path
100+
let path: URL = .keysV1
92101
let requestOptions: RequestOptions?
93102

94103
init(requestOptions: RequestOptions?) {
95-
self.path = .keysV1
96104
self.requestOptions = requestOptions
97105
}
98106

Sources/AlgoliaSearchClient/Command/Command+Advanced.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ extension Command {
1515

1616
let method: HTTPMethod = .get
1717
let callType: CallType = .read
18-
let path: IndexCompletion
18+
let path: URL
1919
let requestOptions: RequestOptions?
2020

2121
init(indexName: IndexName, taskID: TaskID, requestOptions: RequestOptions?) {
2222
self.requestOptions = requestOptions
23-
self.path = (.indexesV1 >>> .index(indexName) >>> .task(for: taskID))
23+
self.path = URL
24+
.indexesV1
25+
.appending(indexName)
26+
.appending(.task)
27+
.appending(taskID)
2428
}
2529

2630
}
@@ -29,12 +33,14 @@ extension Command {
2933

3034
let method: HTTPMethod = .get
3135
let callType: CallType = .read
32-
let path: TaskCompletion
36+
let path: URL
3337
let requestOptions: RequestOptions?
3438

3539
init(taskID: AppTaskID, requestOptions: RequestOptions?) {
3640
self.requestOptions = requestOptions
37-
self.path = (.task >>> TaskCompletion.task(withID: taskID))
41+
self.path = URL
42+
.task
43+
.appending(taskID)
3844
}
3945

4046
}
@@ -43,7 +49,7 @@ extension Command {
4349

4450
let method: HTTPMethod = .get
4551
let callType: CallType = .read
46-
let path: Path = .logs
52+
let path: URL = .logs
4753
let requestOptions: RequestOptions?
4854

4955
init(indexName: IndexName?, offset: Int?, length: Int?, logType: LogType, requestOptions: RequestOptions?) {

Sources/AlgoliaSearchClient/Command/Command+Answers.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ extension Command {
1515

1616
let method: HTTPMethod = .post
1717
let callType: CallType = .read
18-
let path: IndexCompletion
18+
let path: URL
1919
let body: Data?
2020
let requestOptions: RequestOptions?
2121

2222
init(indexName: IndexName,
2323
query: AnswersQuery,
2424
requestOptions: RequestOptions?) {
2525
self.requestOptions = requestOptions
26-
self.path = (.answers >>> .index(indexName) >>> .prediction)
26+
self.path = URL
27+
.answers
28+
.appending(indexName)
29+
.appending(.prediction)
2730
self.body = query.httpBody
2831
}
2932

Sources/AlgoliaSearchClient/Command/Command+Custom.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ extension Command {
1616

1717
let method: HTTPMethod
1818
let callType: CallType
19-
let path: Path
19+
let path: URL
2020
let body: Data?
2121
let requestOptions: RequestOptions?
2222

2323
init(method: HTTPMethod,
2424
callType: CallType,
25-
path: Path,
25+
path: URL,
2626
body: Data?,
2727
requestOptions: RequestOptions?) {
2828
self.method = method
@@ -40,10 +40,10 @@ extension Command {
4040
throw AlgoliaCommandError.invalidHTTPMethod
4141
}
4242
self.method = method
43-
guard let path = (urlRequest.url?.path).flatMap(Path.init) else {
43+
guard let path = urlRequest.url?.path, let pathURL = URL(string: path) else {
4444
throw AlgoliaCommandError.invalidPath
4545
}
46-
self.path = path
46+
self.path = pathURL
4747
self.body = urlRequest.httpBody
4848
self.requestOptions = requestOptions
4949
}

Sources/AlgoliaSearchClient/Command/Command+Dictionaries.swift

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ extension Command {
1515

1616
let method: HTTPMethod = .post
1717
let callType: CallType = .write
18-
let path: DictionaryCompletion
18+
let path: URL
1919
let body: Data?
2020
let requestOptions: RequestOptions?
2121

2222
init<D: CustomDictionary>(dictionary: D.Type,
2323
requests: [DictionaryRequest<D.Entry>],
2424
clearExistingDictionaryEntries: Bool,
2525
requestOptions: RequestOptions?) where D.Entry: Encodable {
26-
self.path = (.dictionaries >>> .dictionaryName(D.name) >>> .batch)
26+
self.path = URL
27+
.dictionaries
28+
.appending(D.name)
29+
.appending(.batch)
2730
self.body = Payload(requests: requests, clearExistingDictionaryEntries: clearExistingDictionaryEntries).httpBody
2831
self.requestOptions = requestOptions
2932
}
@@ -40,23 +43,29 @@ extension Command {
4043

4144
let method: HTTPMethod = .post
4245
let callType: CallType = .read
43-
let path: DictionaryCompletion
46+
let path: URL
4447
let body: Data?
4548
let requestOptions: RequestOptions?
4649

4750
init(dictionaryName: DictionaryName,
4851
query: DictionaryQuery,
4952
requestOptions: RequestOptions?) {
5053
self.requestOptions = requestOptions
51-
self.path = (.dictionaries >>> .dictionaryName(dictionaryName) >>> .search)
54+
self.path = URL
55+
.dictionaries
56+
.appending(dictionaryName)
57+
.appending(.search)
5258
self.body = query.httpBody
5359
}
5460

5561
init<D: CustomDictionary>(dictionary: D.Type,
5662
query: DictionaryQuery,
5763
requestOptions: RequestOptions?) {
5864
self.requestOptions = requestOptions
59-
self.path = (.dictionaries >>> .dictionaryName(D.name) >>> .search)
65+
self.path = URL
66+
.dictionaries
67+
.appending(D.name)
68+
.appending(.search)
6069
self.body = query.httpBody
6170
}
6271

@@ -66,12 +75,15 @@ extension Command {
6675

6776
let method: HTTPMethod = .get
6877
let callType: CallType = .read
69-
let path: DictionaryCompletion
78+
let path: URL
7079
let requestOptions: RequestOptions?
7180

7281
init(requestOptions: RequestOptions?) {
7382
self.requestOptions = requestOptions
74-
self.path = (.dictionaries >>> .common >>> .settings)
83+
self.path = URL
84+
.dictionaries
85+
.appending(.asterisk)
86+
.appending(.settings)
7587
}
7688

7789
}
@@ -80,14 +92,17 @@ extension Command {
8092

8193
let method: HTTPMethod = .put
8294
let callType: CallType = .write
83-
let path: DictionaryCompletion
95+
let path: URL
8496
let body: Data?
8597
let requestOptions: RequestOptions?
8698

8799
init(settings: DictionarySettings,
88100
requestOptions: RequestOptions?) {
89101
self.requestOptions = requestOptions
90-
self.path = (.dictionaries >>> .common >>> .settings)
102+
self.path = URL
103+
.dictionaries
104+
.appending(.asterisk)
105+
.appending(.settings)
91106
self.body = settings.httpBody
92107
}
93108

@@ -97,12 +112,15 @@ extension Command {
97112

98113
let method: HTTPMethod = .get
99114
let callType: CallType = .read
100-
let path: DictionaryCompletion
115+
let path: URL
101116
let requestOptions: RequestOptions?
102117

103118
init(requestOptions: RequestOptions?) {
104119
self.requestOptions = requestOptions
105-
self.path = (.dictionaries >>> .common >>> .languages)
120+
self.path = URL
121+
.dictionaries
122+
.appending(.asterisk)
123+
.appending(.languages)
106124
}
107125

108126
}

0 commit comments

Comments
 (0)