Skip to main content

Crate nym_http_api_client_macro

Crate nym_http_api_client_macro 

Source
Expand description

Proc-macros for configuring HTTP clients globally via the inventory crate.

This crate provides macros that allow any crate in the workspace to contribute configuration modifications to reqwest::ClientBuilder instances through a compile-time registry pattern.

§Overview

The macros work by:

  1. Collecting configuration functions from across all crates at compile time
  2. Sorting them by priority (lower numbers run first)
  3. Applying them sequentially to build HTTP clients with consistent settings

§Examples

§Basic Usage with client_defaults!

use nym_http_api_client_macro::client_defaults;

// Register default configurations with priority
client_defaults!(
    priority = 10;  // Optional, defaults to 0
    timeout = std::time::Duration::from_secs(30),
    gzip = true,
    user_agent = "Nym/1.0"
);

§Using client_cfg! for one-off configurations

use nym_http_api_client_macro::client_cfg;

let configure = client_cfg!(
    timeout = std::time::Duration::from_secs(60),
    default_headers {
        "X-Custom-Header" => "value",
        "Authorization" => "auth_token"
    }
);

let builder = reqwest::ClientBuilder::new();
let configured = configure(builder);

§DSL Reference

The macro DSL supports several patterns:

  • key = value - Calls builder.key(value)
  • key(arg1, arg2) - Calls builder.key(arg1, arg2)
  • flag - Calls builder.flag() with no arguments
  • default_headers { "name" => "value", ... } - Sets default headers

§Priority System

Configurations are applied in priority order (lower numbers first):

  • Negative priorities: Early configuration (e.g., -100 for base settings)
  • Zero (default): Standard configuration
  • Positive priorities: Late configuration (e.g., 100 for overrides)

Macros§

client_cfg
Creates a closure that configures a ReqwestClientBuilder.
client_defaults
Registers global default configurations for HTTP clients.