pub struct HttpClient { /* private fields */ }Expand description
HTTP client for making requests to the Shopify API.
The client handles:
- Base URI construction from session shop domain or
api_host - Default headers including User-Agent and access token
- Automatic retry logic for 429 and 500 responses
- Shopify-specific header parsing
§Thread Safety
HttpClient is Send + Sync, making it safe to share across async tasks.
§Example
ⓘ
use shopify_sdk::{HttpClient, HttpRequest, HttpMethod, Session, ShopDomain};
let session = Session::new(
"session-id".to_string(),
ShopDomain::new("my-store").unwrap(),
"access-token".to_string(),
"read_products".parse().unwrap(),
false,
None,
);
let client = HttpClient::new("/admin/api/2024-10", &session, None);
let request = HttpRequest::builder(HttpMethod::Get, "products.json")
.build()
.unwrap();
let response = client.request(request).await?;Implementations§
Source§impl HttpClient
impl HttpClient
Sourcepub fn new(
base_path: impl Into<String>,
session: &Session,
config: Option<&ShopifyConfig>,
) -> Self
pub fn new( base_path: impl Into<String>, session: &Session, config: Option<&ShopifyConfig>, ) -> Self
Creates a new HTTP client for the given session.
§Arguments
base_path- The base path for API requests (e.g., “/admin/api/2024-10”)session- The session providing shop domain and access tokenconfig- Optional configuration forapi_hostanduser_agent_prefix
§Panics
Panics if the underlying reqwest client cannot be created. This should only happen in extremely unusual circumstances (e.g., TLS initialization failure).
§Example
use shopify_sdk::{Session, ShopDomain, AuthScopes};
use shopify_sdk::clients::HttpClient;
let session = Session::new(
"session-id".to_string(),
ShopDomain::new("my-store").unwrap(),
"access-token".to_string(),
AuthScopes::new(),
false,
None,
);
let client = HttpClient::new("/admin/api/2024-10", &session, None);Sourcepub const fn default_headers(&self) -> &HashMap<String, String>
pub const fn default_headers(&self) -> &HashMap<String, String>
Returns the default headers for this client.
Sourcepub async fn request(
&self,
request: HttpRequest,
) -> Result<HttpResponse, HttpError>
pub async fn request( &self, request: HttpRequest, ) -> Result<HttpResponse, HttpError>
Sends an HTTP request to the Shopify API.
This method handles:
- Request validation
- URL construction
- Header merging
- Response parsing
- Retry logic for 429 and 500 responses
- Deprecation warning logging
§Errors
Returns HttpError if:
- Request validation fails (
InvalidRequest) - Network error occurs (
Network) - Non-2xx response received (
Response) - Max retries exceeded (
MaxRetries)
§Example
ⓘ
let request = HttpRequest::builder(HttpMethod::Get, "products.json")
.tries(3) // Enable retries
.build()
.unwrap();
let response = client.request(request).await?;
if response.is_ok() {
println!("Products: {}", response.body);
}Trait Implementations§
Auto Trait Implementations§
impl Freeze for HttpClient
impl !RefUnwindSafe for HttpClient
impl Send for HttpClient
impl Sync for HttpClient
impl Unpin for HttpClient
impl !UnwindSafe for HttpClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more