pub async fn make_http_request<B: Serialize>(
client: &Client,
rate_limiter: Arc<RwLock<RateLimiter>>,
method: Method,
url: &str,
headers: Vec<(&str, &str)>,
body: &Option<B>,
retry_config: RetryConfig,
) -> Result<Response, AppError>Expand description
Makes an HTTP request with automatic rate limiting and retry on rate limit errors
This function provides a centralized way to make HTTP requests to the IG Markets API with built-in rate limiting and automatic retry logic.
§Arguments
client- The HTTP client to use for the requestrate_limiter- Shared rate limiter to control request ratemethod- HTTP method (GET, POST, PUT, DELETE, etc.)url- Full URL to requestheaders- Vector of (header_name, header_value) tuplesbody- Optional request body (will be serialized to JSON)retry_config- Retry configuration (max retries and delay)
§Returns
Ok(Response)- Successful HTTP responseErr(AppError)- Error if request fails (excluding rate limit errors which are retried)
§Example
ⓘ
use ig_client::model::http::{make_http_request, RetryConfig};
use reqwest::{Client, Method};
use std::sync::Arc;
use tokio::sync::RwLock;
let client = Client::new();
let rate_limiter = Arc::new(RwLock::new(RateLimiter::new(&config)));
let headers = vec![
("X-IG-API-KEY", "your-api-key"),
("Content-Type", "application/json"),
];
// Infinite retries with 10 second delay (default)
let response = make_http_request(
&client,
rate_limiter.clone(),
Method::GET,
"https://demo-api.ig.com/gateway/deal/markets/EPIC",
headers.clone(),
&None::<()>,
RetryConfig::infinite(),
).await?;
// Maximum 3 retries with default 10 second delay
let response = make_http_request(
&client,
rate_limiter.clone(),
Method::GET,
"https://demo-api.ig.com/gateway/deal/markets/EPIC",
headers.clone(),
&None::<()>,
RetryConfig::with_max_retries(3),
).await?;
// Infinite retries with custom 5 second delay
let response = make_http_request(
&client,
rate_limiter.clone(),
Method::GET,
"https://demo-api.ig.com/gateway/deal/markets/EPIC",
headers.clone(),
&None::<()>,
RetryConfig::with_delay(5),
).await?;
// Maximum 3 retries with custom 5 second delay
let response = make_http_request(
&client,
rate_limiter,
Method::GET,
"https://demo-api.ig.com/gateway/deal/markets/EPIC",
headers,
&None::<()>,
RetryConfig::with_max_retries_and_delay(3, 5),
).await?;