Skip to content

Commit 0963ff3

Browse files
authored
Merge pull request #27 from leonidas-o/main
queryItems arg for custom func in ElasticsearchClient
2 parents ed44995 + 7ce088f commit 0963ff3

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Sources/ElasticsearchNIOClient/ElasticsearchClient+Requests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ extension ElasticsearchClient {
276276
}
277277
}
278278

279-
public func custom(_ path: String, method: HTTPMethod, body: Data) -> EventLoopFuture<Data> {
279+
public func custom(_ path: String, queryItems: [URLQueryItem] = [], method: HTTPMethod, body: Data) -> EventLoopFuture<Data> {
280280
do {
281-
let url = try buildURL(path: path)
281+
let url = try buildURL(path: path, queryItems: queryItems)
282282
let body = ByteBuffer(data: body)
283283
var headers = HTTPHeaders()
284284
headers.add(name: "content-type", value: "application/json")

Tests/ElasticsearchNIOClientTests/ElasticsearchNIOClientTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,36 @@ class ElasticSearchIntegrationTests: XCTestCase {
597597
XCTAssertEqual(count["value"] as! Double, 50.5)
598598
}
599599

600+
func testCustomRequestWithQueryItems() throws {
601+
// create index
602+
let mappings: [String: Any] = [
603+
"properties": [
604+
"keyword_field": [
605+
"type": "keyword",
606+
"fields": [
607+
"test": [
608+
"type": "text"
609+
]
610+
]
611+
]
612+
]
613+
]
614+
let settings: [String: Any] = ["number_of_shards": 3]
615+
let createResponse = try client.createIndex(indexName, mappings: mappings, settings: settings).wait()
616+
XCTAssertEqual(createResponse.acknowledged, true)
617+
618+
// get indices in json format
619+
struct ESGetSingleIndexResponse: Decodable {
620+
let index: String
621+
}
622+
let resultData = try client.custom("/_cat/indices", queryItems: [URLQueryItem(name: "format", value: "json")], method: .GET, body: "".data(using: .utf8)!).wait()
623+
let results = try JSONDecoder().decode([ESGetSingleIndexResponse].self, from: resultData)
624+
XCTAssertNotNil(results.map { $0.index }.first { $0 == indexName })
625+
626+
// delete index
627+
let deleteResponse = try client.deleteIndex(self.indexName).wait()
628+
XCTAssertEqual(deleteResponse.acknowledged, true)
629+
}
600630

601631
func testCustomSearchWithDataQuery() throws {
602632
for index in 1...100 {

0 commit comments

Comments
 (0)