Skip to main content

Crate hpx_transport

Crate hpx_transport 

Source
Expand description

§hpx-transport

Exchange SDK toolkit for cryptocurrency trading applications.

This crate builds on hpx to provide exchange-specific functionality:

  • Authentication: API key, HMAC signing, and custom auth strategies
  • WebSocket: Single-task connection with Connection/Handle/Stream split API
  • SSE (feature sse): Server-Sent Events transport with auto-reconnection, protocol handlers, and SseConnection/SseHandle/SseStream split API
  • Typed Responses: Generic response wrapper with metadata
  • Rate Limiting: Token bucket rate limiter
  • Metrics: OpenTelemetry metrics integration

§Quick Start

use hpx_transport::{
    auth::ApiKeyAuth,
    exchange::{ExchangeClient, RestClient, RestConfig},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config =
        RestConfig::new("https://api.example.com").timeout(std::time::Duration::from_secs(30));

    let auth = ApiKeyAuth::header("X-API-Key", "my-api-key");
    let client = RestClient::new(config, auth)?;

    // Use the client...
    Ok(())
}

§WebSocket Quick Start (Split API)

use hpx_transport::websocket::{Connection, Event, WsConfig, handlers::GenericJsonHandler};

let config = WsConfig::new("wss://api.exchange.com/ws");
let handler = GenericJsonHandler::new();

let connection = Connection::connect_stream(config, handler).await?;
let (handle, mut stream) = connection.split();

handle.subscribe("trades.BTC").await?;
while let Some(event) = stream.next().await {
    if let Event::Message(msg) = event {
        println!("Control/unknown message: {:?}", msg.kind);
    }
}

§Rate Limiting Example

use hpx_transport::rate_limit::RateLimiter;

let limiter = RateLimiter::new();
limiter.add_limit("orders", 10, 1.0)?; // 10 capacity, 1/sec refill

if limiter.try_acquire("orders") {
    println!("Request allowed");
}

Re-exports§

pub use auth::ApiKeyAuth;
pub use auth::Authentication;
pub use auth::BearerAuth;
pub use auth::HmacAuth;
pub use auth::NoAuth;
pub use error::TransportError;
pub use error::TransportResult;
pub use exchange::ExchangeClient;
pub use exchange::RestClient;
pub use exchange::RestConfig;
pub use rate_limit::RateLimitError;
pub use rate_limit::RateLimiter;
pub use typed::ApiError;
pub use typed::TypedResponse;
pub use websocket::Connection;
pub use websocket::ConnectionEpoch;
pub use websocket::ConnectionHandle;
pub use websocket::ConnectionState;
pub use websocket::ConnectionStream;
pub use websocket::Event;
pub use websocket::MessageKind;
pub use websocket::ProtocolHandler;
pub use websocket::RequestId;
pub use websocket::SubscriptionGuard;
pub use websocket::Topic;
pub use websocket::WsClient;
pub use websocket::WsConfig;
pub use websocket::WsMessage;

Modules§

auth
Authentication strategies for exchange APIs.
error
Simplified error handling for the transport layer.
exchange
Exchange client abstraction for REST APIs.
rate_limit
Rate limiting for exchange APIs.
typed
Typed response wrapper for ergonomic API patterns.
websocket
Advanced WebSocket functionality with dual interaction patterns.