Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-httpclient

Named HTTP clients for Pudu: a client factory, delegating handlers, pooled keep-alive connections, handler lifetimes, logging, and resilience

0.1.1Apache-2.01

InstallClose

Json.pudu

Pudu57 lines2.6 KB

GitHub ↗
1/** @Client.Json.Module — requests and responses whose bodies are JSON */2module PuduLangHttpClient.Client.Json34import Std.Http as Http5import Std.Json as Json6import PuduLangHttpClient.Client as Client7import PuduLangHttpClient.Content as Content8import PuduLangHttpClient as HttpClient9import PuduLangHttpClient.Request as Request10import PuduLangHttpClient.Response as Response1112/// The media type JSON requests ask for.13const ACCEPT_JSON: Str = "application/json"1415/// The body of a successful `GET` as a JSON document.16export fn getJson(client: &Client.Client, uri: Str) -> HttpClient.Outcome[Json.Json] {17  let response = Response.ensureSuccess(Client.send(client, accepting(client, Http.Get, uri)) ?) ?18  Response.json(&response)19}2021/// The body of a successful `GET` as a value that reads itself from JSON.22export fn getFromJson[T: Json.Decode](client: &Client.Client, uri: Str) -> HttpClient.Outcome[T] {23  let response = Response.ensureSuccess(Client.send(client, accepting(client, Http.Get, uri)) ?) ?24  Response.readAs(&response)25}2627/// The body of a successful `DELETE` as a value that reads itself from JSON.28export fn deleteFromJson[T: Json.Decode](client: &Client.Client, uri: Str) -> HttpClient.Outcome[T] {29  let response = Response.ensureSuccess(Client.send(client, accepting(client, Http.Delete, uri)) ?) ?30  Response.readAs(&response)31}3233/// The response to a `POST` carrying a value written as JSON, whatever its status.34export fn postAsJson[T: Json.Encode](client: &Client.Client, uri: Str, value: &T) -> HttpClient.Outcome[Response.Response] {35  Client.send(client, carrying(client, Http.Post, uri, Content.jsonOf(value)))36}3738/// The response to a `PUT` carrying a value written as JSON, whatever its status.39export fn putAsJson[T: Json.Encode](client: &Client.Client, uri: Str, value: &T) -> HttpClient.Outcome[Response.Response] {40  Client.send(client, carrying(client, Http.Put, uri, Content.jsonOf(value)))41}4243/// The response to a `PATCH` carrying a value written as JSON, whatever its status.44export fn patchAsJson[T: Json.Encode](client: &Client.Client, uri: Str, value: &T) -> HttpClient.Outcome[Response.Response] {45  Client.send(client, carrying(client, Http.Patch, uri, Content.jsonOf(value)))46}4748/// A request asking for JSON.49fn accepting(client: &Client.Client, method: Http.Method, uri: Str) -> Request.Request {50  Request.Request{..Client.build(client, method, uri), headers: [("accept", ACCEPT_JSON)]}51}5253/// A request asking for JSON and carrying a body.54fn carrying(client: &Client.Client, method: Http.Method, uri: Str, content: Content.Content) -> Request.Request {55  Request.Request{..accepting(client, method, uri), content: Some(content)}56}57