Polymarket.Http (Polymarket v0.1.1)

Copy Markdown View Source

Shared Req-based HTTP plumbing for the Polymarket API clients (Polymarket.Gamma, Polymarket.Clob).

Each client passes itself as the owner so that, in tests, requests route through that client's own Req.Test stub (see req_options/1).

Summary

Types

An HTTP header as a {name, value} pair.

Functions

Issue a GET request and return the decoded JSON body on a 200 response.

Issue a POST request with a raw body, returning the decoded JSON body on a 2xx response and {:error, {status, body}} otherwise.

Types

header()

@type header() :: {String.t(), String.t()}

An HTTP header as a {name, value} pair.

Functions

get(url, params, owner, headers \\ [])

@spec get(String.t(), keyword(), module(), [header()]) ::
  {:ok, term()} | {:error, :failed_to_get}

Issue a GET request and return the decoded JSON body on a 200 response.

params are sent as query parameters; array-valued params are expanded into repeated keys (clob_token_ids: ["1", "2"] -> ?clob_token_ids=1&clob_token_ids=2) since Req rejects list values. headers are extra request headers (e.g. the POLY_* authentication headers). owner is the calling client module, used to scope the Req.Test stub during tests.

post(url, body, owner, headers \\ [])

@spec post(String.t(), binary(), module(), [header()]) ::
  {:ok, term()} | {:error, {non_neg_integer(), term()}}

Issue a POST request with a raw body, returning the decoded JSON body on a 2xx response and {:error, {status, body}} otherwise.

body is sent verbatim — callers that authenticate the request (L2 HMAC) must sign exactly these bytes, so the body is never re-encoded here. A content-type: application/json header is added for non-empty bodies. headers are extra request headers and owner scopes the Req.Test stub during tests.

Unlike get/4, a non-2xx surfaces the status and decoded body so callers can report the server's error (e.g. an order rejection) rather than a bare atom. A transport failure (no HTTP response) is returned as {:error, {0, message}} rather than raised, so callers' {:ok, _} | {:error, _} contracts hold.