dream_opensearch/client
OpenSearch HTTP client
This module provides a simple HTTP client for interacting with OpenSearch (or Elasticsearch) clusters. It handles the low-level HTTP communication needed for document operations, searches, and index management.
Quick Start
Create a client and index a document 
import dream_opensearch/client.{type Client}
import dream_opensearch/document
pub fn create_client() -> Client {
client.new("http://localhost:9200")
}
pub fn index_alice(
opensearch: Client,
index: String,
) -> Result(String, String) {
document.index(opensearch, index, "123", "{\"name\":\"Alice\"}")
}
Base URL
The base URL should point to your OpenSearch cluster. It can include a user name and password, which the client sends as HTTP Basic authentication on every request:
http://localhost:9200https://search.example.com:9200https://user:pass@search.example.com:9200
Percent-encode @, : and / in the user name or password (%40,
%3A, %2F). The client decodes them before it builds the
authorization header.
Types
OpenSearch client configuration
Represents a connection to an OpenSearch cluster. The client stores the base URL and handles HTTP communication with the cluster.
Fields
base_url: The base URL of the OpenSearch cluster (without trailing slash), as given tonew. It keeps any user name and password; do not print or log it.
pub type Client {
Client(base_url: String)
}
Constructors
-
Client(base_url: String)A client for the cluster at
base_url. Build it withnew, which removes a trailing slash.
Values
pub fn new(base_url: String) -> Client
Create a new OpenSearch client
Creates a client configured to connect to the specified OpenSearch cluster.
The base URL should include the protocol and port (e.g., http://localhost:9200).
Any trailing slash in the URL is automatically removed. The URL is not
checked here: send_request parses it on each request.
A user name and password in the URL (https://user:pass@host:9200) make
every request send HTTP Basic authentication. See send_request.
Parameters
base_url: The base URL of the OpenSearch cluster
Returns
A new Client configured for the specified cluster.
See Also
send_request: sends a request to the cluster of this client.
Example
Create a client 
import dream_opensearch/client.{type Client}
pub fn create_local_client() -> Client {
client.new("http://localhost:9200")
}
pub fn create_client_with_credentials() -> Client {
client.new("https://user:pass@search.example.com:9200")
}
pub fn send_request(
client: Client,
method: http.Method,
path: String,
body: String,
) -> Result(String, String)
Send HTTP request to OpenSearch
Sends a raw HTTP request to the OpenSearch cluster. The
dream_opensearch/document module uses it for every operation. Most
applications should use that module instead.
The request automatically includes the Content-Type: application/json header.
When the base URL has a user name, the request also has the header
authorization: Basic <base64(user:password)>. The user name and password
are percent-decoded first, and a URL with no password sends an empty
password. The user name and password are not sent in the request line or
the host header. A base URL with no user name sends no authorization
header.
Parameters
client: The OpenSearch clientmethod: The HTTP method (GET, POST, PUT, DELETE, etc.)path: The API path (e.g., “/users/_doc/123” or “/users/_search”), sent as it is. This function does not percent-encode it: encode an index name or a document ID with characters outsideA-Z a-z 0-9 - . _ ~yourself. Thedream_opensearch/documentfunctions encode them for you.body: The request body as a JSON string
Returns
Ok(String): The response body as a string (status < 400)Error(String): See Errors.
Errors
- The response status is 400 or more: the response body, unchanged.
- The connection fails:
Connection failed: <reason>, for exampleConnection failed: Connection refused, orConnection failed: DNS resolution failed (NXDOMAIN)for a host name that does not resolve. No request is sent. - The server closes the connection with no answer:
Remote closed connection. For GET, HEAD, PUT and DELETE, the HTTP client sends the request once more on a new connection before it returns this error. It sends a POST one time only. - The server sends no answer for 30 seconds:
Timed out after 30000ms. The request was sent, and the cluster can still finish it. - The response body is not valid UTF-8, with any status:
The OpenSearch response body is not valid UTF-8. - The URL cannot be parsed:
Invalid OpenSearch URL: the URL cannot be parsed. No request is sent. - The URL has no host:
Invalid OpenSearch URL: the URL has no host. No request is sent. - The user name or password has invalid percent-encoding:
Invalid credentials in the OpenSearch URL: the user name or password has invalid percent-encoding. No request is sent.
No error message contains the user name or password.
See Also
new: creates the client.dream_opensearch/document: the operations built on this function.
Example
Get a document 
import dream_opensearch/client.{type Client}
import gleam/http
pub fn get_user(opensearch: Client, index: String) -> Result(String, String) {
client.send_request(opensearch, http.Get, "/" <> index <> "/_doc/123", "")
}