package polymarket

  1. Overview
  2. Docs
OCaml client library for the Polymarket prediction market API

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.2.0.tar.gz
md5=4eb4c5d2f63ff081c9713d90be5a51b2
sha512=0e3de0c9b40683e09ab8f9f966a44784ef1b9b482c3eefef84104a7e8042c92f1d79893ee9588b24fa3d0decaed7f365509f4d1c23c66ce8328efb64e721f276

doc/polymarket.http/Polymarket_http/Client/index.html

Module Polymarket_http.ClientSource

Generic HTTP client for Polymarket APIs.

This module provides a reusable HTTP client. Uses cohttp-eio for HTTP requests. Use the Builder module for type-safe request construction.

Client Configuration

Sourcetype t

The client type holding connection configuration

Sourceval create : base_url:string -> sw:Eio.Switch.t -> net:_ Eio.Net.t -> rate_limiter:Polymarket_rate_limiter.Rate_limiter.t -> unit -> t

Create a new client instance.

  • parameter base_url

    The API base URL

  • parameter sw

    The Eio switch for resource management

  • parameter net

    The Eio network interface

  • parameter rate_limiter

    Shared rate limiter for enforcing API limits

Sourceval base_url : t -> string

Get the base URL of the client

HTTP Request Functions

Sourcetype params = (string * string list) list

Query parameters type

Sourceval build_uri : string -> string -> params -> Uri.t

Build a URI from base URL, path, and query parameters

Sourcetype status_code = int

HTTP status code

Sourceval do_get : ?headers:(string * string) list -> t -> Uri.t -> status_code * string

Perform a GET request and return status code and body.

  • parameter headers

    Optional list of HTTP headers to include

Sourceval do_post : ?headers:(string * string) list -> t -> Uri.t -> body:string -> status_code * string

Perform a POST request with JSON body and return status code and body.

  • parameter headers

    Optional list of HTTP headers to include

Sourceval do_delete : ?headers:(string * string) list -> t -> Uri.t -> status_code * string

Perform a DELETE request and return status code and body.

  • parameter headers

    Optional list of HTTP headers to include

Sourceval do_delete_with_body : ?headers:(string * string) list -> t -> Uri.t -> body:string -> status_code * string

Perform a DELETE request with JSON body and return status code and body.

  • parameter headers

    Optional list of HTTP headers to include

Error Handling

Sourcetype http_error = {
  1. status : int;
  2. body : string;
  3. message : string;
}

HTTP error with status code, raw body, and extracted message

Sourcetype parse_error = {
  1. context : string;
  2. message : string;
}

Parse error with context and message

Sourcetype network_error = {
  1. message : string;
}

Network-level error (connection failed, timeout, etc.)

Sourcetype error =
  1. | Http_error of http_error
  2. | Parse_error of parse_error
  3. | Network_error of network_error
    (*

    Structured error type for all API errors

    *)
Sourceval error_to_string : error -> string

Convert error to human-readable string

Sourceval pp_error : Format.formatter -> error -> unit

Pretty printer for errors

Sourcetype error_response = {
  1. error : string;
}

Legacy type alias for backwards compatibility

Sourceval to_error : string -> error

Create a parse error from a message

Sourceval parse_error : status:int -> string -> error

Parse an error response from a JSON body and status code

Response Handling

Sourceval handle_response : status_code -> string -> (string -> ('a, error) result) -> ('a, error) result

Handle HTTP response status and parse body.

  • parameter status

    The HTTP status code

  • parameter body

    The response body

  • parameter parse_fn

    Parser for successful responses

  • returns

    Parsed result or error

JSON Field Checking

These functions log warnings when API responses contain fields not defined in our types. Use with @@deriving yojson_fields to generate field lists.

Sourceval check_extra_fields : expected_fields:string list -> context:string -> Yojson.Safe.t -> unit

Check a JSON value for unexpected fields and log warnings.

  • parameter expected_fields

    List of field names we expect (from yojson_fields)

  • parameter context

    Description for logging (e.g. "Market.t")

Sourceval parse_with_field_check : expected_fields:string list -> context:string -> string -> (Yojson.Safe.t -> 'a) -> ('a, error) result

Parse JSON with extra field checking for single objects.

  • parameter expected_fields

    List of expected field names

  • parameter context

    Description for logging

  • parameter body

    JSON string to parse

  • parameter of_yojson

    Parser function from ppx_yojson_conv

Sourceval parse_list_with_field_check : expected_fields:string list -> context:string -> string -> (Yojson.Safe.t -> 'a) -> ('a, error) result

Parse JSON with extra field checking for lists of objects.

  • parameter expected_fields

    List of expected field names for each item

  • parameter context

    Description for logging

  • parameter body

    JSON string to parse

  • parameter of_yojson

    Parser function from ppx_yojson_conv