shopify-sdk 1.0.0

A Rust SDK for the Shopify API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
//! HTTP client for Shopify API communication.
//!
//! This module provides the [`HttpClient`] type for making authenticated
//! requests to the Shopify API with automatic retry handling.

use std::collections::HashMap;

use crate::auth::Session;
use crate::clients::errors::{HttpError, HttpResponseError, MaxHttpRetriesExceededError};
use crate::clients::http_request::HttpRequest;
use crate::clients::http_response::{ApiDeprecationInfo, HttpResponse};
use crate::config::{DeprecationCallback, ShopifyConfig};

/// Fixed retry wait time in seconds (matching Ruby SDK).
pub const RETRY_WAIT_TIME: u64 = 1;

/// SDK version from Cargo.toml.
pub const SDK_VERSION: &str = env!("CARGO_PKG_VERSION");

/// 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
///
/// ```rust,ignore
/// 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?;
/// ```
pub struct HttpClient {
    /// The internal reqwest HTTP client.
    client: reqwest::Client,
    /// Base URI (e.g., `https://my-store.myshopify.com`).
    base_uri: String,
    /// Base path (e.g., "/admin/api/2024-10").
    base_path: String,
    /// Default headers to include in all requests.
    default_headers: HashMap<String, String>,
    /// Optional callback for deprecation notices.
    deprecation_callback: Option<DeprecationCallback>,
}

impl std::fmt::Debug for HttpClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HttpClient")
            .field("client", &self.client)
            .field("base_uri", &self.base_uri)
            .field("base_path", &self.base_path)
            .field("default_headers", &self.default_headers)
            .field(
                "deprecation_callback",
                &self.deprecation_callback.as_ref().map(|_| "<callback>"),
            )
            .finish()
    }
}

// Verify HttpClient is Send + Sync at compile time
const _: fn() = || {
    const fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<HttpClient>();
};

impl HttpClient {
    /// 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
    ///
    /// ```rust
    /// 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);
    /// ```
    #[must_use]
    pub fn new(
        base_path: impl Into<String>,
        session: &Session,
        config: Option<&ShopifyConfig>,
    ) -> Self {
        let base_path = base_path.into();

        // Determine base URI - use api_host if configured, otherwise session.shop
        let api_host = config.and_then(|c| c.host());
        let default_shop_uri = || format!("https://{}", session.shop.as_ref());
        let base_uri = api_host.map_or_else(default_shop_uri, |host| {
            host.host_name()
                .map_or_else(default_shop_uri, |host_name| format!("https://{host_name}"))
        });

        // Build User-Agent header
        let user_agent_prefix = config
            .and_then(ShopifyConfig::user_agent_prefix)
            .map_or(String::new(), |prefix| format!("{prefix} | "));
        let rust_version = env!("CARGO_PKG_RUST_VERSION");
        let user_agent =
            format!("{user_agent_prefix}Shopify API Library v{SDK_VERSION} | Rust {rust_version}");

        // Build default headers
        let mut default_headers = HashMap::new();
        default_headers.insert("User-Agent".to_string(), user_agent);
        default_headers.insert("Accept".to_string(), "application/json".to_string());

        // Add Host header when using api_host (proxy scenario)
        if api_host.is_some() {
            default_headers.insert("Host".to_string(), session.shop.as_ref().to_string());
        }

        // Add access token header if present
        if !session.access_token.is_empty() {
            default_headers.insert(
                "X-Shopify-Access-Token".to_string(),
                session.access_token.clone(),
            );
        }

        // Create reqwest client
        let client = reqwest::Client::builder()
            .use_rustls_tls()
            .build()
            .expect("Failed to create HTTP client");

        // Get deprecation callback if configured
        let deprecation_callback = config.and_then(|c| c.deprecation_callback().cloned());

        Self {
            client,
            base_uri,
            base_path,
            default_headers,
            deprecation_callback,
        }
    }

    /// Returns the base URI for this client.
    #[must_use]
    pub fn base_uri(&self) -> &str {
        &self.base_uri
    }

    /// Returns the base path for this client.
    #[must_use]
    pub fn base_path(&self) -> &str {
        &self.base_path
    }

    /// Returns the default headers for this client.
    #[must_use]
    pub const fn default_headers(&self) -> &HashMap<String, String> {
        &self.default_headers
    }

    /// 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
    ///
    /// ```rust,ignore
    /// 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);
    /// }
    /// ```
    pub async fn request(&self, request: HttpRequest) -> Result<HttpResponse, HttpError> {
        // Validate request first
        request.verify()?;

        // Build full URL
        let url = format!("{}{}/{}", self.base_uri, self.base_path, request.path);

        // Merge headers
        let mut headers = self.default_headers.clone();
        if let Some(body_type) = &request.body_type {
            headers.insert(
                "Content-Type".to_string(),
                body_type.as_content_type().to_string(),
            );
        }
        if let Some(extra) = &request.extra_headers {
            for (key, value) in extra {
                headers.insert(key.clone(), value.clone());
            }
        }

        // Retry loop
        let mut tries: u32 = 0;
        loop {
            tries += 1;

            // Build the reqwest request
            let mut req_builder = match request.http_method {
                crate::clients::http_request::HttpMethod::Get => self.client.get(&url),
                crate::clients::http_request::HttpMethod::Post => self.client.post(&url),
                crate::clients::http_request::HttpMethod::Put => self.client.put(&url),
                crate::clients::http_request::HttpMethod::Delete => self.client.delete(&url),
            };

            // Add headers
            for (key, value) in &headers {
                req_builder = req_builder.header(key, value);
            }

            // Add query params
            if let Some(query) = &request.query {
                req_builder = req_builder.query(query);
            }

            // Add body
            if let Some(body) = &request.body {
                req_builder = req_builder.body(body.to_string());
            }

            // Send request
            let res = req_builder.send().await?;

            // Parse response
            let code = res.status().as_u16();
            let res_headers = Self::parse_response_headers(res.headers());
            let body_text = res.text().await.unwrap_or_default();

            // Parse body as JSON
            let body = if body_text.is_empty() {
                serde_json::json!({})
            } else {
                serde_json::from_str(&body_text).unwrap_or_else(|_| {
                    // For 5xx errors, return raw body as string value
                    if code >= 500 {
                        serde_json::json!({ "raw_body": body_text })
                    } else {
                        serde_json::json!({})
                    }
                })
            };

            let response = HttpResponse::new(code, res_headers, body);

            // Handle deprecation warning if present
            if let Some(reason) = response.deprecation_reason() {
                tracing::warn!(
                    "Deprecated request to Shopify API at {}, received reason: {}",
                    request.path,
                    reason
                );

                // Invoke deprecation callback if configured
                if let Some(callback) = &self.deprecation_callback {
                    let info = ApiDeprecationInfo {
                        reason: reason.to_string(),
                        path: Some(request.path.clone()),
                    };
                    callback(&info);
                }
            }

            // Check if response is OK
            if response.is_ok() {
                return Ok(response);
            }

            // Build error message (matching Ruby SDK format)
            let error_message = Self::serialize_error(&response);

            // Check if we should retry
            let should_retry = code == 429 || code == 500;
            if !should_retry {
                return Err(HttpError::Response(HttpResponseError {
                    code,
                    message: error_message,
                    error_reference: response.request_id().map(String::from),
                }));
            }

            // Check if we've exhausted retries
            if tries >= request.tries {
                if request.tries == 1 {
                    return Err(HttpError::Response(HttpResponseError {
                        code,
                        message: error_message,
                        error_reference: response.request_id().map(String::from),
                    }));
                }
                return Err(HttpError::MaxRetries(MaxHttpRetriesExceededError {
                    code,
                    tries: request.tries,
                    message: error_message,
                    error_reference: response.request_id().map(String::from),
                }));
            }

            // Calculate retry delay
            let delay = Self::calculate_retry_delay(&response, code);
            tokio::time::sleep(delay).await;
        }
    }

    /// Parses response headers into a `HashMap`.
    fn parse_response_headers(
        headers: &reqwest::header::HeaderMap,
    ) -> HashMap<String, Vec<String>> {
        let mut result: HashMap<String, Vec<String>> = HashMap::new();
        for (name, value) in headers {
            let key = name.as_str().to_lowercase();
            let value = value.to_str().unwrap_or_default().to_string();
            result.entry(key).or_default().push(value);
        }
        result
    }

    /// Calculates the retry delay based on response and status code.
    fn calculate_retry_delay(response: &HttpResponse, status: u16) -> std::time::Duration {
        // For 429: use Retry-After if present, otherwise fixed delay
        // For 500: always use fixed delay (ignore Retry-After)
        if status == 429 {
            if let Some(retry_after) = response.retry_request_after {
                return std::time::Duration::from_secs_f64(retry_after);
            }
        }
        std::time::Duration::from_secs(RETRY_WAIT_TIME)
    }

    /// Serializes error response to JSON format (matching Ruby SDK).
    fn serialize_error(response: &HttpResponse) -> String {
        let mut error_body = serde_json::Map::new();

        if let Some(errors) = response.body.get("errors") {
            error_body.insert("errors".to_string(), errors.clone());
        }
        if let Some(error) = response.body.get("error") {
            error_body.insert("error".to_string(), error.clone());
        }
        if response.body.get("error").is_some() {
            if let Some(desc) = response.body.get("error_description") {
                error_body.insert("error_description".to_string(), desc.clone());
            }
        }

        if let Some(request_id) = response.request_id() {
            error_body.insert(
                "error_reference".to_string(),
                serde_json::json!(format!(
                    "If you report this error, please include this id: {request_id}."
                )),
            );
        }

        serde_json::to_string(&error_body).unwrap_or_else(|_| "{}".to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::auth::AuthScopes;
    use crate::config::{ApiKey, ApiSecretKey, ShopDomain};

    fn create_test_session() -> Session {
        Session::new(
            "test-session".to_string(),
            ShopDomain::new("test-shop").unwrap(),
            "test-access-token".to_string(),
            AuthScopes::new(),
            false,
            None,
        )
    }

    #[test]
    fn test_client_construction_with_session() {
        let session = create_test_session();
        let client = HttpClient::new("/admin/api/2024-10", &session, None);

        assert_eq!(client.base_uri(), "https://test-shop.myshopify.com");
        assert_eq!(client.base_path(), "/admin/api/2024-10");
    }

    #[test]
    fn test_user_agent_header_format() {
        let session = create_test_session();
        let client = HttpClient::new("/admin/api/2024-10", &session, None);

        let user_agent = client.default_headers().get("User-Agent").unwrap();
        assert!(user_agent.contains("Shopify API Library v"));
        assert!(user_agent.contains("Rust"));
    }

    #[test]
    fn test_access_token_header_injection() {
        let session = create_test_session();
        let client = HttpClient::new("/admin/api/2024-10", &session, None);

        assert_eq!(
            client.default_headers().get("X-Shopify-Access-Token"),
            Some(&"test-access-token".to_string())
        );
    }

    #[test]
    fn test_no_access_token_header_when_empty() {
        let session = Session::new(
            "test-session".to_string(),
            ShopDomain::new("test-shop").unwrap(),
            String::new(), // Empty access token
            AuthScopes::new(),
            false,
            None,
        );
        let client = HttpClient::new("/admin/api/2024-10", &session, None);

        assert!(client
            .default_headers()
            .get("X-Shopify-Access-Token")
            .is_none());
    }

    #[test]
    fn test_accept_header_is_json() {
        let session = create_test_session();
        let client = HttpClient::new("/admin/api/2024-10", &session, None);

        assert_eq!(
            client.default_headers().get("Accept"),
            Some(&"application/json".to_string())
        );
    }

    #[test]
    fn test_client_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<HttpClient>();
    }

    #[test]
    fn test_base_uri_with_shop_domain() {
        let session = create_test_session();
        let client = HttpClient::new("/admin/api/2024-10", &session, None);

        assert_eq!(client.base_uri(), "https://test-shop.myshopify.com");
    }

    #[test]
    fn test_user_agent_with_prefix() {
        let session = create_test_session();
        let config = ShopifyConfig::builder()
            .api_key(ApiKey::new("test-key").unwrap())
            .api_secret_key(ApiSecretKey::new("test-secret").unwrap())
            .user_agent_prefix("MyApp/1.0")
            .build()
            .unwrap();

        let client = HttpClient::new("/admin/api/2024-10", &session, Some(&config));

        let user_agent = client.default_headers().get("User-Agent").unwrap();
        assert!(user_agent.starts_with("MyApp/1.0 | "));
        assert!(user_agent.contains("Shopify API Library"));
    }
}