Skip to main content

HttpClient

Struct HttpClient 

Source
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

Source

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 token
  • config - Optional configuration for api_host and user_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);
Source

pub fn base_uri(&self) -> &str

Returns the base URI for this client.

Source

pub fn base_path(&self) -> &str

Returns the base path for this client.

Source

pub const fn default_headers(&self) -> &HashMap<String, String>

Returns the default headers for this client.

Source

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§

Source§

impl Debug for HttpClient

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more