oaspec/transport
The runtime contract between generated OpenAPI clients and an HTTP runtime.
Generated client code builds a Request and hands it to a Send (or an
AsyncSend) function. Adapters such as oaspec_httpc and oaspec_fetch
provide that function for a real runtime; in tests any function of the
same shape works:
let send = fn(_request) {
Ok(transport.Response(status: 200, headers: [], body: transport.TextBody("[]")))
}
The with_* functions wrap a send function to add a base URL, default
headers or credentials. They work with both Send and AsyncSend.
Types
A value that will be ready later, used by generated async clients.
It runs on every target: JavaScript adapters back it with promises, and other runtimes can bridge it from callbacks.
pub opaque type Async(value)
A function that sends a request and returns the response later.
pub type AsyncSend =
fn(Request) -> Async(Result(Response, TransportError))
The body of a request or a response.
pub type Body {
EmptyBody
TextBody(String)
BytesBody(BitArray)
}
Constructors
-
EmptyBodyNo body.
-
TextBody(String)A UTF-8 text body, such as JSON or form data.
-
BytesBody(BitArray)A binary body.
A credential for one OpenAPI security scheme, passed to with_security.
pub type Credential {
ApiKey(scheme_name: String, value: String)
BearerToken(scheme_name: String, token: String)
BasicAuth(scheme_name: String, value: String)
DigestAuth(scheme_name: String, value: String)
}
Constructors
-
ApiKey(scheme_name: String, value: String)An API key, sent wherever the scheme says (header, query or cookie).
-
BearerToken(scheme_name: String, token: String)A bearer token, sent as
Authorization: Bearer <token>. -
BasicAuth(scheme_name: String, value: String)Basic credentials, already Base64 encoded, sent as
Authorization: Basic <value>. -
DigestAuth(scheme_name: String, value: String)Digest credentials, sent as
Authorization: Digest <value>.
A request built by a generated client.
pub type Request {
Request(
method: http.Method,
base_url: option.Option(String),
path: String,
query: List(#(String, String)),
headers: List(#(String, String)),
body: Body,
security: List(SecurityAlternative),
)
}
Constructors
-
Request( method: http.Method, base_url: option.Option(String), path: String, query: List(#(String, String)), headers: List(#(String, String)), body: Body, security: List(SecurityAlternative), )Arguments
- base_url
-
The base URL from the spec’s
servers, if the client chose one.with_base_urlreplaces it. - path
-
The path with its parameters already filled in, such as
/pets/42. - security
-
The security alternatives the operation accepts.
with_securityreads this to decide which credentials to send.
A set of security requirements that must all be met together.
A request lists the alternatives its operation accepts; meeting any one of them is enough.
pub type SecurityAlternative {
SecurityAlternative(requirements: List(SecurityRequirement))
}
Constructors
-
SecurityAlternative(requirements: List(SecurityRequirement))
One credential an operation needs, as declared by an OpenAPI security
scheme. scheme_name is the key of the scheme under
components.securitySchemes, and with_security matches on it.
pub type SecurityRequirement {
ApiKeyHeader(scheme_name: String, header_name: String)
ApiKeyQuery(scheme_name: String, query_name: String)
ApiKeyCookie(scheme_name: String, cookie_name: String)
HttpAuthorization(scheme_name: String, prefix: String)
}
Constructors
-
ApiKeyHeader(scheme_name: String, header_name: String)An API key sent in the named header.
-
ApiKeyQuery(scheme_name: String, query_name: String)An API key sent in the named query parameter.
-
ApiKeyCookie(scheme_name: String, cookie_name: String)An API key sent in the named cookie.
-
HttpAuthorization(scheme_name: String, prefix: String)An
Authorizationheader with the given scheme prefix, such asBearerorBasic.
A function that sends a request and waits for the response.
pub type Send =
fn(Request) -> Result(Response, TransportError)
A failure to get any response from the server.
Error statuses such as 404 or 500 are responses, not transport errors.
pub type TransportError {
ConnectionFailed(detail: String)
Timeout
InvalidBaseUrl(detail: String)
TlsFailure(detail: String)
Unsupported(detail: String)
}
Constructors
-
ConnectionFailed(detail: String)The connection could not be made or was lost.
-
TimeoutThe request did not finish in time.
-
InvalidBaseUrl(detail: String)The base URL could not be parsed.
-
TlsFailure(detail: String)The TLS handshake failed.
-
Unsupported(detail: String)The runtime cannot send this request, such as a body type it does not handle.
Values
pub fn await(
async async: Async(a),
next next: fn(a) -> Async(b),
) -> Async(b)
Waits for the value and then for the Async that next returns from
it.
pub fn from_callback(
register register: fn(fn(value) -> Nil) -> Nil,
) -> Async(value)
Creates an Async from a function that calls its argument once the
value is ready.
Examples
transport.from_callback(register: fn(done) { done(1) })
// -> an Async that yields 1
pub fn map(
async async: Async(a),
with with_: fn(a) -> b,
) -> Async(b)
Transforms the value once it is ready.
Examples
transport.resolve(value: 1)
|> transport.map(with: fn(x) { x + 1 })
// -> an Async that yields 2
pub fn map_try(
async async: Async(Result(a, error)),
with with_: fn(a) -> Result(b, error),
) -> Async(Result(b, error))
Transforms an Ok value once it is ready. An Error is passed through
and with is not called.
Examples
transport.resolve(value: Ok(1))
|> transport.map_try(with: fn(x) { Ok(x + 1) })
// -> an Async that yields Ok(2)
transport.resolve(value: Error(transport.Timeout))
|> transport.map_try(with: fn(x) { Ok(x + 1) })
// -> an Async that yields Error(transport.Timeout)
pub fn resolve(value value: a) -> Async(a)
Returns an Async that is already ready with the given value.
Examples
transport.resolve(value: Ok(response))
pub fn run(async async: Async(a), done done: fn(a) -> Nil) -> Nil
Calls done with the value once it is ready.
pub fn try_await(
async async: Async(Result(a, error)),
next next: fn(a) -> Async(Result(b, error)),
) -> Async(Result(b, error))
Waits for an Ok value and then for the Async that next returns
from it. An Error is passed through and next is not called.
pub fn with_base_url(
send send: fn(Request) -> a,
base_url base_url: String,
) -> fn(Request) -> a
Wraps a send function so every request goes to base_url, replacing
the base URL the client chose from the spec.
Examples
let send = httpc.send |> transport.with_base_url("https://staging.example.com")
pub fn with_default_headers(
send send: fn(Request) -> a,
headers headers: List(#(String, String)),
) -> fn(Request) -> a
Wraps a send function so each request gets the given headers when it does not already have them.
Header names are compared case-insensitively. A header already on the
request is kept, and when headers names the same header twice the
first entry is used.
Examples
let send =
httpc.send
|> transport.with_default_headers([#("user-agent", "my-app/1.0")])
Panics
Panics when a header name or value contains CR, LF or NUL, since those bytes allow header injection. The check runs when the wrapper is built, not on each request.
pub fn with_security(
send send: fn(Request) -> a,
credentials credentials: List(Credential),
) -> fn(Request) -> a
Wraps a send function so each request carries the credentials its operation asks for.
The first security alternative of the request whose requirements all
have a matching credential (by scheme_name and kind) is applied: API
keys go to the declared header, query parameter or cookie, and the
other credentials go to the Authorization header. When no alternative
can be met the request is sent unchanged, and the server’s rejection
comes back as a normal response.
Examples
let send =
httpc.send
|> transport.with_security([transport.BearerToken("bearerAuth", token)])