-module(acumen@finalize_order). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/acumen/finalize_order.gleam"). -export([build/4, response/2]). -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( " Finalize an ACME order with a Certificate Signing Request.\n" "\n" " After all authorizations are valid (order status is `Ready`), submit a CSR\n" " to finalize the order and trigger certificate issuance.\n" "\n" " ## Example\n" "\n" " ```gleam\n" " import acumen\n" " import acumen/finalize_order\n" " import acumen/order\n" "\n" " // Generate a CSR from the order\n" " let assert Ok(csr) = order.to_ec_csr(ready_order, cert_key)\n" "\n" " let assert Ok(#(resp, ctx)) = acumen.execute(\n" " ctx,\n" " build: finalize_order.build(ready_order.finalize_url, _, registered_key, csr:),\n" " send: httpc.send,\n" " )\n" "\n" " let assert Ok(finalized) = finalize_order.response(resp, ready_order.url)\n" " ```\n" ). -file("src/acumen/finalize_order.gleam", 39). ?DOC( " Builds the HTTP request to finalize an order with a CSR.\n" "\n" " The `csr` must be in DER format; the library handles base64url encoding.\n" ). -spec build( acumen@url:url(), acumen:context(), acumen:registered_key(), bitstring() ) -> {ok, gleam@http@request:request(binary())} | {error, acumen:acme_error()}. build(Url, Context, Key, Csr) -> Csr_encoded = begin _pipe = Csr, _pipe@1 = gleam@bit_array:base64_url_encode(_pipe, false), gleam@json:string(_pipe@1) end, _pipe@2 = gleam@json:object([{<<"csr"/utf8>>, Csr_encoded}]), _pipe@3 = gleam@json:to_string(_pipe@2), _pipe@4 = acumen@internal@jws:sign_with_kid( erlang:element(2, Key), erlang:element(3, Key), _pipe@3, erlang:element(3, Context), Url ), _pipe@5 = gleam@result:map_error( _pipe@4, fun(Field@0) -> {jws_error, Field@0} end ), gleam@result:'try'( _pipe@5, fun(_capture) -> acumen:build_post_request(Url, _capture) end ). -file("src/acumen/finalize_order.gleam", 68). ?DOC( " Parses the finalize response.\n" "\n" " Returns the order, typically in `Processing` or `Valid` status.\n" " Pass the `order_url` directly since the finalize response does not\n" " include a Location header.\n" ). -spec response(gleam@http@response:response(binary()), acumen@url:url()) -> {ok, acumen@order:order()} | {error, acumen:acme_error()}. response(Resp, Order_url) -> case erlang:element(2, Resp) of 200 -> acumen@order:parse_order_response(Resp, Order_url); _ -> {error, {invalid_response, acumen@internal@utils:unexpected_status_message( erlang:element(2, Resp) )}} end.