ddn.api.net.http.client

Module ddn.api.net.http.client

Defines a small, robust, and idiomatic HTTP client API that supports both synchronous and asynchronous usage.

Types 10

classHttpException : Exception

Base exception type for the HTTP client API.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)Constructs an HTTP exception.

Exception thrown when an HTTP operation times out.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null)Constructs a timeout exception.

Standard HTTP methods.

GET
HEAD
POST
PUT
DELETE
OPTIONS
TRACE
CONNECT
PATCH

A case-insensitive header map with multi-value support.

Header names are normalized to lower case for lookups. Each name can carry several values — required for response headers that legally repeat (e.g. Set-Cookie, which must not be comma-folded per RFC 6265). set replaces all values of a name; add appends one; get returns the first value and getAll returns every value.

Fields
private string[][string] _values
Methods
string opIndex(string name) constMap-like read access.
string opIndex(string name) refMap-like read/write access.
void opIndexAssign(string value, string name)Map-like assignment: `headers["name"] = "value"`.
void opIndexAssign(typeof(null), string name)Map-like delete: `headers["name"] = null`.
const(string[]) * opBinaryRight(string op : "in")(string name) constSupports `"name" in headers`.
int opApply(scope int delegate(const string name, const string value) dg) constForeach iteration over every stored (name, value) pair; a name with several values yields one iteration per value.
void set(string name, string value)Sets (replaces) a header with a single value.
void add(string name, string value)Appends a value to a header, preserving any existing values — use for legally repeatable headers such as `Set-Cookie`.
bool has(string name) constReturns true if the header is present.
string get(string name, string defaultValue = "") constGets the first value of a header.
string[] getAll(string name) constGets all values of a header.
void remove(string name)Removes a header (all its values) if present.
size_t length() @property constReturns the number of stored header names.
const(string[][string]) values() @property constReturns the underlying normalized header map (name to all values).
HttpHeaders dup() constReturns a deep copy of this `HttpHeaders`.

An HTTP request.

Fields
HttpMethod method
string url
HttpHeaders headers
ubyte[] body
Duration operationTimeout
Duration connectTimeout
uint maxRedirects
Methods
HttpRequest withHeader(string name, string value) constReturns a copy of this request with an additional/replaced header.
HttpRequest withBody(const(ubyte)[] data) constReturns a copy of this request with a body.
HttpRequest withOperationTimeout(Duration d) constReturns a copy of this request with an operation timeout.
HttpRequest withConnectTimeout(Duration d) constReturns a copy of this request with a connect timeout.
HttpRequest withMaxRedirects(uint max) constReturns a copy of this request with a redirect limit.
HttpRequest dup() constReturns a deep copy of this request.
Constructors
this(HttpMethod method, string url)Constructs a request with the given method and URL.

An HTTP response.

Fields
ushort statusCode
string reasonPhrase
HttpHeaders headers
ubyte[] body
Methods
bool isSuccess() constReturns true if `statusCode` is in the 2xx range.
string bodyAsString() constReturns the body as a string.
string header(string name, string defaultValue = "") constConvenience accessor for a header on the response.
interfaceHttpClient

Synchronous HTTP client interface.

Methods
HttpResponse request(const HttpRequest request)Performs an HTTP request and returns the full response.
void close()Releases any resources associated with the client.
structHttpFuture(T)

A future representing an asynchronously produced value.

Fields
private SharedState _state
Methods
bool isValid() @property constReturns true if this future refers to a valid asynchronous result.
bool ready()Returns true if the future is already completed.
void wait()Blocks until the future is completed.
bool wait(Duration timeout)Blocks until the future is completed or the timeout elapses.
T get()Blocks until completion, then returns the value or throws the stored error.
Constructors
Nested Templates
SharedState
structHttpPromise(T)

A promise that can be completed with a value or an error.

Fields
private SharedState _state
Methods
HttpPromise!T make() staticCreates a new promise.
HttpFuture!T future()Returns the future associated with this promise.
void succeed(T value)Completes the promise successfully.
void fail(Throwable error)Completes the promise with an error.

Asynchronous HTTP client interface.

Methods
HttpFuture!HttpResponse requestAsync(const HttpRequest request)Starts an HTTP request and returns a future for its response.
void close()Releases any resources associated with the client.

Functions 1

private fnstring asciiLower(string s) @safe pure nothrowLower-cases a header name using ASCII only, returning the original string (no allocation) when it is already lower-case.