// Generated by openapi-moonbit — DO NOT EDIT
// Spec SHA-256: 7e574664c0f434e52021e12f313a3266fd3175fe2ecddde4c0e0930549c72ba2
///|
/// API client parameterized over any `@runtime.Transport` implementation.
pub struct Client[T] {
priv transport : T
priv config : ClientConfig
priv ws_dialer : &@ws.WebSocketDialer?
}
///|
/// Create a new client over `transport`.
pub fn[T] Client::new(
transport : T,
base_url~ : String = "https://release.api.golem.cloud",
auth~ : Auth = Auth::None,
default_headers? : @runtime.Headers,
ws_dialer? : &@ws.WebSocketDialer,
) -> Client[T] {
let config = ClientConfig::new(base_url~, auth~, default_headers?)
{ transport, config, ws_dialer, }
}
///|
/// Create a new client from an explicit `ClientConfig`.
pub fn[T] Client::with_config(
transport : T,
config : ClientConfig,
ws_dialer? : &@ws.WebSocketDialer,
) -> Client[T] {
{ transport, config, ws_dialer, }
}
///|
/// Inspect the active `ClientConfig`.
pub fn[T] Client::config(self : Client[T]) -> ClientConfig {
self.config
}
///|
/// Return a copy of this client with updated authentication credentials.
pub fn[T] Client::with_auth(self : Client[T], auth : Auth) -> Client[T] {
let new_config : ClientConfig = {
base_url: self.config.base_url,
auth,
default_headers: self.config.default_headers.copy(),
}
{ transport: self.transport, config: new_config, ws_dialer: self.ws_dialer, }
}
///|
/// Return a copy of this client targeting a different `base_url`.
pub fn[T] Client::with_base_url(
self : Client[T],
base_url : String,
) -> Client[T] {
let new_config : ClientConfig = {
base_url,
auth: self.config.auth,
default_headers: self.config.default_headers.copy(),
}
{ transport: self.transport, config: new_config, ws_dialer: self.ws_dialer, }
}
///|
fn append_cookie(headers : @runtime.Headers, cookie : String) -> Unit {
if cookie.is_empty() {
return
}
match headers.get("Cookie") {
Option::Some(existing) if !existing.is_empty() =>
headers.set("Cookie", existing + "; " + cookie)
_ => headers.set("Cookie", cookie)
}
}
///|
fn apply_auth_query(auth : Auth, query : String) -> String {
match auth {
Auth::ApiKeyQuery(name~, value~) => {
let pair = @encoding.percent_encode(name) +
"=" +
@encoding.percent_encode(value)
if query == "" {
pair
} else {
query + "&" + pair
}
}
Auth::Combined(items) => {
let mut acc = query
for item in items {
acc = apply_auth_query(item, acc)
}
acc
}
_ => query
}
}
///|
fn apply_auth_headers(auth : Auth, headers : @runtime.Headers) -> Unit {
match auth {
Auth::None | Auth::ApiKeyQuery(..) => ()
Auth::Bearer(token) => headers.set("Authorization", "Bearer " + token)
Auth::Basic(username~, password~) => {
let encoded = @rjson.base64_encode(
@runtime.utf8_encode(username + ":" + password),
)
headers.set("Authorization", "Basic " + encoded)
}
Auth::ApiKeyHeader(name~, value~) => headers.set(name, value)
Auth::ApiKeyCookie(name~, value~) =>
append_cookie(headers, @encoding.format_cookie_scalar(name, value))
Auth::Combined(items) =>
for item in items {
apply_auth_headers(item, headers)
}
}
}
///|
fn[T] Client::prepare_request(
self : Client[T],
http_method~ : String,
path~ : String,
query~ : String = "",
apply_auth~ : Bool = true,
) -> @runtime.Request {
let full_query = if apply_auth {
apply_auth_query(self.config.auth, query)
} else {
query
}
let url = @encoding.join_url(self.config.base_url, path, query=full_query)
let req = @runtime.Request::new(http_method~, url~)
for entry in self.config.default_headers.entries() {
if entry.0.to_lower() == "cookie" {
append_cookie(req.headers(), entry.1)
} else {
req.headers().append(entry.0, entry.1)
}
}
if apply_auth {
apply_auth_headers(self.config.auth, req.headers())
}
req
}
///|
fn[T] Client::expand_op_path(
self : Client[T],
template : String,
params : Array[(String, String)],
pre_encoded~ : Array[(String, String)] = [],
) -> String raise GolemError {
ignore(self)
@encoding.expand_path(template, params, pre_encoded~) catch {
@encoding.MissingPathParam(n) => {
let __err = GolemError::InvalidParam(
name=n,
message="missing path parameter",
)
raise __err
}
@encoding.InvalidValue(msg) => {
let __err = GolemError::InvalidParam(name="path", message=msg)
raise __err
}
}
}
///|
async fn[T : @runtime.Transport] Client::send_request(
self : Client[T],
req : @runtime.Request,
) -> @runtime.Response raise GolemError {
self.transport.send(req) catch {
e => {
let __err = GolemError::Transport(e)
raise __err
}
}
}
///|
fn[T] Client::api_error_from_response(
self : Client[T],
resp : @runtime.Response,
) -> GolemError {
ignore(self)
let body = resp.text()
let parsed : @cjson.Json? = Option::Some(@rjson.parse_json(body)) catch {
_ => Option::None
}
GolemError::Api(status=resp.status(), body~, parsed~)
}
///|
fn[T, R] Client::decode_json_response(
self : Client[T],
resp : @runtime.Response,
decode : (@rjson.JsonReader) -> R raise @cjson.JsonDecodeError,
) -> R raise GolemError {
ignore(self)
let body = resp.text()
let json = @rjson.parse_json(body) catch {
e => {
let __err = GolemError::Decode(
status=resp.status(),
message=e.to_string(),
body~,
)
raise __err
}
}
let reader = @rjson.JsonReader::root(json)
decode(reader) catch {
e => {
let __err = GolemError::Decode(
status=resp.status(),
message=e.to_string(),
body~,
)
raise __err
}
}
}
///|
fn http_url_to_ws(url : String) -> String {
let lower = url.to_lower()
if lower.has_prefix("https://") {
"wss://" + url[8:].to_owned()
} else if lower.has_prefix("http://") {
"ws://" + url[7:].to_owned()
} else {
url
}
}