Skip to content

Commit 598cda2

Browse files
authored
Merge pull request #16 from niklhut/custom-request
Add support for custom requests
2 parents d0a031c + 615fd3e commit 598cda2

File tree

4 files changed

+174
-110
lines changed

4 files changed

+174
-110
lines changed

Sources/ElasticsearchNIOClient/ElasticsearchClient+Requests.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,24 @@ extension ElasticsearchClient {
267267
let url = try buildURL(path: "/\(name)")
268268
return requester.executeRequest(url: url, method: .HEAD, headers: .init(), body: nil).flatMapThrowing { response in
269269
guard response.status == .ok || response.status == .notFound else {
270-
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)", status: response.status.code)
270+
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)", status: response.status)
271271
}
272272
return response.status == .ok
273273
}
274274
} catch {
275275
return self.eventLoop.makeFailedFuture(error)
276276
}
277277
}
278+
279+
public func custom(_ path: String, method: HTTPMethod, body: Data) -> EventLoopFuture<Data> {
280+
do {
281+
let url = try buildURL(path: path)
282+
let body = ByteBuffer(data: body)
283+
var headers = HTTPHeaders()
284+
headers.add(name: "content-type", value: "application/json")
285+
return sendRequest(url: url, method: method, headers: headers, body: body).flatMapThrowing { return Data(buffer: $0) }
286+
} catch {
287+
return self.eventLoop.makeFailedFuture(error)
288+
}
289+
}
278290
}

Sources/ElasticsearchNIOClient/ElasticsearchClient.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,19 @@ public struct ElasticsearchClient {
9090
self.jsonDecoder = jsonDecoder
9191
}
9292

93-
func sendRequest<D: Decodable>(url: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<D> {
93+
func sendRequest(url: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<ByteBuffer> {
9494
requester.executeRequest(url: url, method: method, headers: headers, body: body).flatMapThrowing { clientResponse in
9595
self.logger.trace("Response: \(clientResponse)")
9696
if let responseBody = clientResponse.body {
9797
self.logger.trace("Response body: \(String(decoding: responseBody.readableBytesView, as: UTF8.self))")
9898
}
9999
switch clientResponse.status.code {
100100
case 200...299:
101-
guard var body = clientResponse.body else {
101+
guard let body = clientResponse.body else {
102102
self.logger.debug("No body from ElasticSearch response")
103-
throw ElasticSearchClientError(message: "No body from ElasticSearch response", status: clientResponse.status.code)
104-
}
105-
guard let response = try body.readJSONDecodable(D.self, decoder: jsonDecoder, length: body.readableBytes) else {
106-
self.logger.debug("Failed to convert \(D.self)")
107-
throw ElasticSearchClientError(message: "Failed to convert \(D.self)", status: clientResponse.status.code)
103+
throw ElasticSearchClientError(message: "No body from ElasticSearch response", status: clientResponse.status)
108104
}
109-
return response
105+
return body
110106
default:
111107
let requestBody: String
112108
if let body = body {
@@ -121,8 +117,19 @@ public struct ElasticsearchClient {
121117
responseBody = "Empty"
122118
}
123119
self.logger.trace("Got response status \(clientResponse.status) from ElasticSearch with response \(clientResponse) when trying \(method) request to \(url). Request body was \(requestBody) and response body was \(responseBody)")
124-
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch", status: clientResponse.status.code)
120+
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch", status: clientResponse.status)
121+
}
122+
}
123+
}
124+
125+
func sendRequest<D: Decodable>(url: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<D> {
126+
sendRequest(url: url, method: method, headers: headers, body: body).flatMapThrowing { body in
127+
var body = body
128+
guard let response = try body.readJSONDecodable(D.self, decoder: jsonDecoder, length: body.readableBytes) else {
129+
self.logger.debug("Failed to convert \(D.self)")
130+
throw ElasticSearchClientError(message: "Failed to convert \(D.self)", status: nil)
125131
}
132+
return response
126133
}
127134
}
128135
}

Sources/ElasticsearchNIOClient/Models/ElasticsearchClientError.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import NIOHTTP1
2+
13
public struct ElasticSearchClientError: Error {
24
public let message: String
3-
public let status: UInt?
5+
public let status: HTTPResponseStatus?
46

5-
public init(message: String, status: UInt?) {
7+
public init(message: String, status: HTTPResponseStatus?) {
68
self.message = message
79
self.status = status
810
}

0 commit comments

Comments
 (0)