Skip to content

Commit e281978

Browse files
fix(Browse): change browse with cursor method GET -> POST (#671)
* fix(browse) Use POST method for browse with cursor call instead of GET to avoid issues with cursor URL encoding * Set browse method by default with the empty query, not the empty cursor
1 parent bbc6e89 commit e281978

File tree

7 files changed

+19
-15
lines changed

7 files changed

+19
-15
lines changed

AlgoliaSearchClient.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |spec|
22
spec.name = "AlgoliaSearchClient"
33
spec.module_name = 'AlgoliaSearchClient'
4-
spec.version = "8.1.1"
4+
spec.version = "8.1.2-beta.1"
55
spec.summary = "Algolia Search API Client written in Swift."
66
spec.homepage = "https://github.com/algolia/algoliasearch-client-swift"
77
spec.license = { :type => "MIT", :file => "LICENSE" }

Sources/AlgoliaSearchClient/Command/Command+Search.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ extension Command {
3939
urlRequest = .init(method: .post, path: path, body: query.httpBody, requestOptions: requestOptions)
4040
}
4141

42-
init(indexName: IndexName, cursor: Cursor? = nil, requestOptions: RequestOptions?) {
43-
self.requestOptions = requestOptions.updateOrCreate(cursor.flatMap { [.cursor: $0.rawValue.addingPercentEncoding(withAllowedCharacters: .uriAllowed)] } ?? [:])
42+
init(indexName: IndexName, cursor: Cursor, requestOptions: RequestOptions?) {
43+
self.requestOptions = requestOptions
44+
let body = CursorWrapper(cursor)
4445
let path = .indexesV1 >>> .index(indexName) >>> IndexCompletion.browse
45-
urlRequest = .init(method: .get, path: path, requestOptions: self.requestOptions)
46+
urlRequest = .init(method: .post, path: path, body: body.httpBody, requestOptions: self.requestOptions)
4647
}
4748

4849
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// This is generated file. Don't modify it manually.
2-
public extension Version { static let current: Version = .init(major: 8, minor: 1, patch: 1, prereleaseIdentifier: nil) }
2+
public extension Version { static let current: Version = .init(major: 8, minor: 1, patch: 2, prereleaseIdentifier: "beta.1") }

Sources/AlgoliaSearchClient/Index/Index+Search.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public extension Index {
107107
- Parameter completion: Result completion
108108
- Returns: SearchResponse object
109109
*/
110-
@discardableResult func browse(query: Query,
110+
@discardableResult func browse(query: Query = .init(),
111111
requestOptions: RequestOptions? = nil,
112112
completion: @escaping ResultCallback<SearchResponse>) -> Operation & TransportTask {
113113
let command = Command.Search.Browse(indexName: name, query: query, requestOptions: requestOptions)
@@ -122,7 +122,7 @@ public extension Index {
122122
- Parameter completion: Result completion
123123
- Returns: Launched asynchronous operation
124124
*/
125-
@discardableResult func browse(query: Query,
125+
@discardableResult func browse(query: Query = .init(),
126126
requestOptions: RequestOptions? = nil) throws -> SearchResponse {
127127
let command = Command.Search.Browse(indexName: name, query: query, requestOptions: requestOptions)
128128
return try execute(command)
@@ -137,7 +137,7 @@ public extension Index {
137137
- Parameter completion: Result completion
138138
- Returns: Launched asynchronous operation
139139
*/
140-
@discardableResult func browse(cursor: Cursor? = nil,
140+
@discardableResult func browse(cursor: Cursor,
141141
requestOptions: RequestOptions? = nil,
142142
completion: @escaping ResultCallback<SearchResponse>) -> Operation & TransportTask {
143143
let command = Command.Search.Browse(indexName: name, cursor: cursor, requestOptions: requestOptions)
@@ -152,7 +152,7 @@ public extension Index {
152152
- Parameter requestOptions: Configure request locally with RequestOptions.
153153
- Returns: SearchResponse object
154154
*/
155-
@discardableResult func browse(cursor: Cursor? = nil,
155+
@discardableResult func browse(cursor: Cursor,
156156
requestOptions: RequestOptions? = nil) throws -> SearchResponse {
157157
let command = Command.Search.Browse(indexName: name, cursor: cursor, requestOptions: requestOptions)
158158
return try execute(command)

Sources/AlgoliaSearchClient/Models/Data structures/FieldWrapper.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ struct EditsKey: Key { static let value = "edits" }
3737
struct ObjectIDKey: Key { static let value = "objectID" }
3838
struct RemoveKey: Key { static let value = "remove" }
3939
struct ClusterKey: Key { static let value = "cluster" }
40+
struct CursorKey: Key { static let value = "cursor" }
4041

4142
typealias ParamsWrapper<Wrapped: Codable> = FieldWrapper<ParamsKey, Wrapped>
4243
typealias RequestsWrapper<Wrapped: Codable> = FieldWrapper<RequestsKey, Wrapped>
4344
typealias EventsWrapper<Wrapped: Codable> = FieldWrapper<EventsKey, Wrapped>
4445
typealias EditsWrapper = FieldWrapper<EditsKey, [Rule.Edit]>
4546
typealias ObjectIDWrapper = FieldWrapper<ObjectIDKey, ObjectID>
4647
typealias ClusterWrapper<Wrapped: Codable> = FieldWrapper<ClusterKey, Wrapped>
48+
typealias CursorWrapper = FieldWrapper<CursorKey, Cursor>

Tests/AlgoliaSearchClientTests/Integration/BrowseIntegrationTests.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ class BrowseIntegrationTests: OnlineTestCase {
3535
let objectID: String
3636
let name: String
3737
}
38-
39-
let records: [Record] = (0...50).map { _ in
40-
.init(objectID: .random(length: 5), name: String.random(length: .random(in: 1...20)))
38+
39+
let records: [Record] = (0...10000).map { _ in
40+
.init(objectID: .random(length: 5), name: String.random(length: .random(in: 1...20)))
4141
}
4242

43+
try index.setSettings(Settings().set(\.attributesForFaceting, to: ["metric", "color"]))
4344
try index.saveObjects(records).wait()
4445

4546
let responses = try index.browseObjects()

Tests/AlgoliaSearchClientTests/Unit/Command/SearchCommandTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ class SearchCommandTests: XCTestCase, AlgoliaCommandTest {
3737
let command = Command.Search.Browse(indexName: test.indexName, cursor: test.cursor, requestOptions: test.requestOptions)
3838
check(command: command,
3939
callType: .read,
40-
method: .get,
40+
method: .post,
4141
urlPath: "/1/indexes/testIndex/browse",
42-
queryItems: [.init(name: "testParameter", value: "testParameterValue"), .init(name: "cursor", value: "AgA%2BBgg4MTUyNTQ0Mg==")],
43-
body: nil,
42+
queryItems: [.init(name: "testParameter", value: "testParameterValue")],
43+
body: CursorWrapper(test.cursor).httpBody,
4444
requestOptions: test.requestOptions)
4545
}
4646

0 commit comments

Comments
 (0)