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
//! # Shopify API Rust SDK
//!
//! A Rust SDK for the Shopify API, providing type-safe configuration,
//! authentication handling, and HTTP client functionality for Shopify app development.
//!
//! ## Overview
//!
//! This SDK provides:
//! - Type-safe configuration via [`ShopifyConfig`] and [`ShopifyConfigBuilder`]
//! - Validated newtypes for API credentials and domain values
//! - OAuth scope handling with implied scope support
//! - OAuth 2.0 authorization code flow via [`auth::oauth`]
//! - Token exchange for embedded apps via [`auth::oauth`]
//! - Client credentials for private/organization apps via [`auth::oauth`]
//! - Token refresh for expiring access tokens via [`auth::oauth`]
//! - Session management for authenticated API calls
//! - Async HTTP client with retry logic and rate limit handling
//! - REST API client with convenient methods for Admin API operations
//! - REST resource infrastructure with CRUD operations and dirty tracking
//! - GraphQL API client for modern Admin API operations (recommended)
//! - Storefront API client for headless commerce applications
//! - Webhook registration system for event subscriptions
//!
//! ## Quick Start
//!
//! ```rust
//! use shopify_sdk::{ShopifyConfig, ApiKey, ApiSecretKey, ApiVersion, AuthScopes};
//!
//! // Create configuration using the builder pattern
//! let config = ShopifyConfig::builder()
//! .api_key(ApiKey::new("your-api-key").unwrap())
//! .api_secret_key(ApiSecretKey::new("your-api-secret").unwrap())
//! .scopes("read_products,write_orders".parse().unwrap())
//! .api_version(ApiVersion::latest())
//! .build()
//! .unwrap();
//! ```
//!
//! ## OAuth Authentication
//!
//! For apps that need to authenticate with Shopify stores:
//!
//! ```rust,ignore
//! use shopify_sdk::{ShopifyConfig, ApiKey, ApiSecretKey, ShopDomain, HostUrl};
//! use shopify_sdk::auth::oauth::{begin_auth, validate_auth_callback, AuthQuery};
//!
//! // Step 1: Configure the SDK
//! let config = ShopifyConfig::builder()
//! .api_key(ApiKey::new("your-api-key").unwrap())
//! .api_secret_key(ApiSecretKey::new("your-secret").unwrap())
//! .host(HostUrl::new("https://your-app.com").unwrap())
//! .scopes("read_products".parse().unwrap())
//! .build()
//! .unwrap();
//!
//! // Step 2: Begin authorization
//! let shop = ShopDomain::new("example-shop").unwrap();
//! let result = begin_auth(&config, &shop, "/auth/callback", true, None)?;
//! // Redirect user to result.auth_url
//! // Store result.state in session
//!
//! // Step 3: Handle callback
//! let session = validate_auth_callback(&config, &query, &stored_state).await?;
//! // session is now ready for API calls
//! ```
//!
//! ## Token Exchange (Embedded Apps)
//!
//! For embedded apps using App Bridge session tokens:
//!
//! ```rust,ignore
//! use shopify_sdk::{ShopifyConfig, ApiKey, ApiSecretKey, ShopDomain};
//! use shopify_sdk::auth::oauth::{exchange_online_token, exchange_offline_token};
//!
//! // Configure the SDK (must be embedded)
//! let config = ShopifyConfig::builder()
//! .api_key(ApiKey::new("your-api-key").unwrap())
//! .api_secret_key(ApiSecretKey::new("your-secret").unwrap())
//! .is_embedded(true)
//! .build()
//! .unwrap();
//!
//! let shop = ShopDomain::new("example-shop").unwrap();
//! let session_token = "eyJ..."; // JWT from App Bridge
//!
//! // Exchange for an online access token
//! let session = exchange_online_token(&config, &shop, session_token).await?;
//!
//! // Or exchange for an offline access token
//! let session = exchange_offline_token(&config, &shop, session_token).await?;
//! ```
//!
//! ## Client Credentials (Private/Organization Apps)
//!
//! For private or organization apps without user interaction:
//!
//! ```rust,ignore
//! use shopify_sdk::{ShopifyConfig, ApiKey, ApiSecretKey, ShopDomain};
//! use shopify_sdk::auth::oauth::exchange_client_credentials;
//!
//! // Configure the SDK (must NOT be embedded)
//! let config = ShopifyConfig::builder()
//! .api_key(ApiKey::new("your-api-key").unwrap())
//! .api_secret_key(ApiSecretKey::new("your-secret").unwrap())
//! // is_embedded defaults to false, which is required
//! .build()
//! .unwrap();
//!
//! let shop = ShopDomain::new("example-shop").unwrap();
//!
//! // Exchange client credentials for an offline access token
//! let session = exchange_client_credentials(&config, &shop).await?;
//! println!("Access token: {}", session.access_token);
//! ```
//!
//! ## Token Refresh (Expiring Tokens)
//!
//! For apps using expiring offline access tokens:
//!
//! ```rust,ignore
//! use shopify_sdk::{ShopifyConfig, ApiKey, ApiSecretKey, ShopDomain};
//! use shopify_sdk::auth::oauth::{refresh_access_token, migrate_to_expiring_token};
//!
//! // Refresh an expiring access token
//! if session.expired() {
//! if let Some(refresh_token) = &session.refresh_token {
//! let new_session = refresh_access_token(&config, &shop, refresh_token).await?;
//! println!("New access token: {}", new_session.access_token);
//! }
//! }
//!
//! // Or migrate from non-expiring to expiring tokens (one-time, irreversible)
//! let new_session = migrate_to_expiring_token(&config, &shop, &old_access_token).await?;
//! ```
//!
//! ## Session Management
//!
//! Sessions represent authenticated connections to a Shopify store. They can be
//! either offline (app-level) or online (user-specific):
//!
//! ```rust
//! use shopify_sdk::{Session, ShopDomain, AuthScopes, AssociatedUser};
//!
//! // Create an offline session (no expiration, no user)
//! let offline_session = Session::new(
//! Session::generate_offline_id(&ShopDomain::new("my-store").unwrap()),
//! ShopDomain::new("my-store").unwrap(),
//! "access-token".to_string(),
//! "read_products".parse().unwrap(),
//! false,
//! None,
//! );
//!
//! // Sessions can be serialized for storage
//! let json = serde_json::to_string(&offline_session).unwrap();
//! ```
//!
//! ## Making GraphQL API Requests (Recommended)
//!
//! The [`GraphqlClient`] provides methods for GraphQL Admin API operations:
//!
//! ```rust,ignore
//! use shopify_sdk::{GraphqlClient, Session, ShopDomain, AuthScopes};
//! use serde_json::json;
//!
//! // Create a session
//! let session = Session::new(
//! "session-id".to_string(),
//! ShopDomain::new("my-store").unwrap(),
//! "access-token".to_string(),
//! AuthScopes::new(),
//! false,
//! None,
//! );
//!
//! // Create a GraphQL client (no deprecation warning - this is the recommended API)
//! let client = GraphqlClient::new(&session, None);
//!
//! // Simple query
//! let response = client.query("query { shop { name } }", None, None, None).await?;
//! println!("Shop: {}", response.body["data"]["shop"]["name"]);
//!
//! // Query with variables
//! let response = client.query(
//! "query GetProduct($id: ID!) { product(id: $id) { title } }",
//! Some(json!({ "id": "gid://shopify/Product/123" })),
//! None,
//! None
//! ).await?;
//!
//! // Check for GraphQL errors (returned with HTTP 200)
//! if let Some(errors) = response.body.get("errors") {
//! println!("GraphQL errors: {}", errors);
//! }
//! ```
//!
//! ## Making Storefront API Requests
//!
//! The [`StorefrontClient`] provides methods for Storefront API operations:
//!
//! ```rust,ignore
//! use shopify_sdk::{StorefrontClient, StorefrontToken, ShopDomain};
//! use serde_json::json;
//!
//! let shop = ShopDomain::new("my-store").unwrap();
//!
//! // With public token (client-side safe)
//! let token = StorefrontToken::Public("public-access-token".to_string());
//! let client = StorefrontClient::new(&shop, Some(token), None);
//!
//! // With private token (server-side only)
//! let token = StorefrontToken::Private("private-access-token".to_string());
//! let client = StorefrontClient::new(&shop, Some(token), None);
//!
//! // Tokenless access for basic features
//! let client = StorefrontClient::new(&shop, None, None);
//!
//! // Query products
//! let response = client.query(
//! "query { products(first: 10) { edges { node { title } } } }",
//! None,
//! None,
//! None
//! ).await?;
//!
//! // Access response data
//! let products = &response.body["data"]["products"];
//! ```
//!
//! ## Webhook Registration
//!
//! The [`WebhookRegistry`] provides webhook subscription management with support
//! for HTTP, Amazon EventBridge, and Google Cloud Pub/Sub delivery methods:
//!
//! ```rust
//! use shopify_sdk::{
//! WebhookRegistry, WebhookRegistrationBuilder, WebhookTopic, WebhookDeliveryMethod
//! };
//!
//! // Configure webhooks at startup
//! let mut registry = WebhookRegistry::new();
//!
//! // HTTP delivery
//! registry
//! .add_registration(
//! WebhookRegistrationBuilder::new(
//! WebhookTopic::OrdersCreate,
//! WebhookDeliveryMethod::Http {
//! uri: "https://example.com/api/webhooks/orders".to_string(),
//! },
//! )
//! .build()
//! )
//! // Amazon EventBridge delivery
//! .add_registration(
//! WebhookRegistrationBuilder::new(
//! WebhookTopic::ProductsUpdate,
//! WebhookDeliveryMethod::EventBridge {
//! arn: "arn:aws:events:us-east-1::event-source/aws.partner/shopify.com/123/source".to_string(),
//! },
//! )
//! .build()
//! )
//! // Google Cloud Pub/Sub delivery
//! .add_registration(
//! WebhookRegistrationBuilder::new(
//! WebhookTopic::CustomersCreate,
//! WebhookDeliveryMethod::PubSub {
//! project_id: "my-gcp-project".to_string(),
//! topic_id: "shopify-webhooks".to_string(),
//! },
//! )
//! .filter("vendor:MyApp".to_string())
//! .build()
//! );
//!
//! // Later, register with Shopify when session is available:
//! // let results = registry.register_all(&session, &config).await?;
//! ```
//!
//! ## Making REST API Requests (Deprecated)
//!
//! The [`RestClient`] provides convenient methods for REST API operations:
//!
//! ```rust,ignore
//! use shopify_sdk::{RestClient, Session, ShopDomain, AuthScopes};
//!
//! // Create a session
//! let session = Session::new(
//! "session-id".to_string(),
//! ShopDomain::new("my-store").unwrap(),
//! "access-token".to_string(),
//! AuthScopes::new(),
//! false,
//! None,
//! );
//!
//! // Create a REST client (logs deprecation warning)
//! let client = RestClient::new(&session, None)?;
//!
//! // GET request
//! let response = client.get("products", None).await?;
//!
//! // POST request with body
//! let body = serde_json::json!({"product": {"title": "New Product"}});
//! let response = client.post("products", body, None).await?;
//! ```
//!
//! ## Making Low-Level HTTP Requests
//!
//! For more control, use the low-level [`HttpClient`]:
//!
//! ```rust,ignore
//! use shopify_sdk::{Session, ShopDomain, AuthScopes};
//! use shopify_sdk::clients::{HttpClient, HttpRequest, HttpMethod};
//!
//! // Create a session
//! let session = Session::new(
//! "session-id".to_string(),
//! ShopDomain::new("my-store").unwrap(),
//! "access-token".to_string(),
//! AuthScopes::new(),
//! false,
//! None,
//! );
//!
//! // Create an HTTP client
//! let client = HttpClient::new("/admin/api/2024-10", &session, None);
//!
//! // Build and send a request
//! let request = HttpRequest::builder(HttpMethod::Get, "products.json")
//! .build()
//! .unwrap();
//!
//! let response = client.request(request).await?;
//! ```
//!
//! ## Design Principles
//!
//! - **No global state**: Configuration is instance-based and passed explicitly
//! - **Fail-fast validation**: All newtypes validate on construction
//! - **Thread-safe**: All types are `Send + Sync`
//! - **Async-first**: Designed for use with Tokio async runtime
//! - **Immutable sessions**: Sessions are immutable after creation
// Re-export public types at crate root for convenience
pub use ;
pub use ;
pub use ConfigError;
// Re-export HTTP client types
pub use ;
// Re-export REST client types
pub use ;
// Re-export GraphQL client types
pub use ;
// Re-export Storefront client types
pub use ;
// Re-export OAuth types for convenience
pub use ;
// Re-export REST resource types for convenience
pub use ;
// Re-export webhook types for convenience
pub use ;