
Json.pudu
Pudu57 lines2.6 KB
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 Response111213const ACCEPT_JSON: Str = "application/json"141516export 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}202122export 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}262728export 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}323334export 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}373839export 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}424344export 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}474849fn accepting(client: &Client.Client, method: Http.Method, uri: Str) -> Request.Request {50 Request.Request{..Client.build(client, method, uri), headers: [("accept", ACCEPT_JSON)]}51}525354fn 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