Skip to content

Commit

Permalink
Merge pull request #16 from niklhut/custom-request
Browse files Browse the repository at this point in the history
Add support for custom requests
  • Loading branch information
0xTim authored Sep 28, 2023
2 parents d0a031c + 615fd3e commit 598cda2
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,24 @@ extension ElasticsearchClient {
let url = try buildURL(path: "/\(name)")
return requester.executeRequest(url: url, method: .HEAD, headers: .init(), body: nil).flatMapThrowing { response in
guard response.status == .ok || response.status == .notFound else {
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)", status: response.status.code)
throw ElasticSearchClientError(message: "Invalid response from index exists API - \(response)", status: response.status)
}
return response.status == .ok
}
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}

public func custom(_ path: String, method: HTTPMethod, body: Data) -> EventLoopFuture<Data> {
do {
let url = try buildURL(path: path)
let body = ByteBuffer(data: body)
var headers = HTTPHeaders()
headers.add(name: "content-type", value: "application/json")
return sendRequest(url: url, method: method, headers: headers, body: body).flatMapThrowing { return Data(buffer: $0) }
} catch {
return self.eventLoop.makeFailedFuture(error)
}
}
}
25 changes: 16 additions & 9 deletions Sources/ElasticsearchNIOClient/ElasticsearchClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,19 @@ public struct ElasticsearchClient {
self.jsonDecoder = jsonDecoder
}

func sendRequest<D: Decodable>(url: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<D> {
func sendRequest(url: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<ByteBuffer> {
requester.executeRequest(url: url, method: method, headers: headers, body: body).flatMapThrowing { clientResponse in
self.logger.trace("Response: \(clientResponse)")
if let responseBody = clientResponse.body {
self.logger.trace("Response body: \(String(decoding: responseBody.readableBytesView, as: UTF8.self))")
}
switch clientResponse.status.code {
case 200...299:
guard var body = clientResponse.body else {
guard let body = clientResponse.body else {
self.logger.debug("No body from ElasticSearch response")
throw ElasticSearchClientError(message: "No body from ElasticSearch response", status: clientResponse.status.code)
}
guard let response = try body.readJSONDecodable(D.self, decoder: jsonDecoder, length: body.readableBytes) else {
self.logger.debug("Failed to convert \(D.self)")
throw ElasticSearchClientError(message: "Failed to convert \(D.self)", status: clientResponse.status.code)
throw ElasticSearchClientError(message: "No body from ElasticSearch response", status: clientResponse.status)
}
return response
return body
default:
let requestBody: String
if let body = body {
Expand All @@ -121,8 +117,19 @@ public struct ElasticsearchClient {
responseBody = "Empty"
}
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)")
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch", status: clientResponse.status.code)
throw ElasticSearchClientError(message: "Bad status code from ElasticSearch", status: clientResponse.status)
}
}
}

func sendRequest<D: Decodable>(url: String, method: HTTPMethod, headers: HTTPHeaders, body: ByteBuffer?) -> EventLoopFuture<D> {
sendRequest(url: url, method: method, headers: headers, body: body).flatMapThrowing { body in
var body = body
guard let response = try body.readJSONDecodable(D.self, decoder: jsonDecoder, length: body.readableBytes) else {
self.logger.debug("Failed to convert \(D.self)")
throw ElasticSearchClientError(message: "Failed to convert \(D.self)", status: nil)
}
return response
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import NIOHTTP1

public struct ElasticSearchClientError: Error {
public let message: String
public let status: UInt?
public let status: HTTPResponseStatus?

public init(message: String, status: UInt?) {
public init(message: String, status: HTTPResponseStatus?) {
self.message = message
self.status = status
}
Expand Down
Loading

0 comments on commit 598cda2

Please sign in to comment.