Packages
mercury_client
1.0.0
A complete, production-grade Elixir client for the Mercury Banking API (accounts, transactions, recipients, invoices, payments, treasury, webhooks, and more), with typed errors, retry with backoff, and lazy auto-paginating streams.
Current section
Files
Jump to
Current section
Files
lib/mercury/multipart.ex
defmodule Mercury.Multipart do
@moduledoc false
# Hand-rolled multipart/form-data encoder (single file field) used for the
# attachment-upload endpoints. Kept dependency-free rather than reaching
# for an extra multipart-building library.
@spec encode(field_name :: String.t(), filename :: String.t(), content :: binary()) ::
{binary(), String.t()}
def encode(field_name, filename, content) when is_binary(content) do
boundary = "mercury-elixir-" <> random_boundary()
body =
IO.iodata_to_binary([
"--",
boundary,
"\r\n",
"Content-Disposition: form-data; name=\"",
field_name,
"\"; filename=\"",
escape(filename),
"\"\r\n",
"Content-Type: application/octet-stream\r\n",
"\r\n",
content,
"\r\n",
"--",
boundary,
"--\r\n"
])
{body, "multipart/form-data; boundary=" <> boundary}
end
defp random_boundary do
16
|> :crypto.strong_rand_bytes()
|> Base.encode16(case: :lower)
end
defp escape(name), do: String.replace(name, "\"", "\\\"")
end