Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom requests #16

Merged
merged 5 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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