-module(acumen@fetch_certificate). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/acumen/fetch_certificate.gleam"). -export([build/3, response/1]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. ?MODULEDOC( " Fetch an issued certificate from the ACME server.\n" "\n" " After an order reaches the `Valid` status, use the certificate URL\n" " from `order.Valid(certificate_url)` to download the certificate chain\n" " in PEM format.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " import acumen\n" " import acumen/fetch_certificate\n" " import acumen/order\n" "\n" " // After the order is valid...\n" " let assert order.Valid(certificate_url) = completed_order.status\n" "\n" " let assert Ok(#(resp, ctx)) = acumen.execute(\n" " ctx,\n" " build: fetch_certificate.build(certificate_url, _, registered_key),\n" " send: httpc.send,\n" " )\n" "\n" " let assert Ok(pem_chain) = fetch_certificate.response(resp)\n" " ```\n" ). -file("src/acumen/fetch_certificate.gleam", 34). ?DOC(" Builds a POST-as-GET request to fetch an issued certificate.\n"). -spec build(acumen@url:url(), acumen:context(), acumen:registered_key()) -> {ok, gleam@http@request:request(binary())} | {error, acumen:acme_error()}. build(Url, Context, Key) -> _pipe = acumen:build_fetch(Url, Context, Key), gleam@result:map( _pipe, fun(_capture) -> gleam@http@request:set_header( _capture, <<"accept"/utf8>>, <<"application/pem-certificate-chain"/utf8>> ) end ). -file("src/acumen/fetch_certificate.gleam", 50). ?DOC( " Parses the certificate fetch response.\n" "\n" " Returns the certificate chain in PEM format.\n" ). -spec response(gleam@http@response:response(binary())) -> {ok, binary()} | {error, acumen:acme_error()}. response(Resp) -> case erlang:element(2, Resp) of 200 -> {ok, erlang:element(4, Resp)}; _ -> {error, {invalid_response, acumen@internal@utils:unexpected_status_message( erlang:element(2, Resp) )}} end.