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
//! UsageCharge resource implementation.
//!
//! This module provides the [`UsageCharge`] resource for managing usage-based
//! charges in Shopify apps. Usage charges are created under a parent
//! `RecurringApplicationCharge` that has a `capped_amount` set.
//!
//! # Nested Resource
//!
//! UsageCharges are nested under RecurringApplicationCharges:
//! - `GET /recurring_application_charges/{charge_id}/usage_charges.json`
//! - `POST /recurring_application_charges/{charge_id}/usage_charges.json`
//! - `GET /recurring_application_charges/{charge_id}/usage_charges/{id}.json`
//!
//! Note: Usage charges cannot be updated or deleted after creation.
//!
//! # Usage-Based Billing
//!
//! To implement usage-based billing:
//!
//! 1. Create a `RecurringApplicationCharge` with a `capped_amount`
//! 2. As the merchant uses resources, create `UsageCharge` records
//! 3. Charges accumulate up to the `capped_amount` per billing period
//!
//! # Example
//!
//! ```rust,ignore
//! use shopify_sdk::rest::{RestResource, ResourceResponse};
//! use shopify_sdk::rest::resources::v2025_10::{UsageCharge, UsageChargeListParams};
//!
//! // Create a usage charge under a recurring charge
//! let usage = UsageCharge {
//!     recurring_application_charge_id: Some(455696195),
//!     description: Some("100 emails sent".to_string()),
//!     price: Some("1.00".to_string()),
//!     ..Default::default()
//! };
//! let saved = usage.save(&client).await?;
//!
//! // List all usage charges for a recurring charge
//! let usages = UsageCharge::all_with_parent(
//!     &client,
//!     "recurring_application_charge_id",
//!     455696195,
//!     None
//! ).await?;
//! ```

use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::clients::RestClient;
use crate::rest::{
    build_path, get_path, ResourceError, ResourceOperation, ResourcePath, ResourceResponse,
    RestResource,
};
use crate::HttpMethod;

use super::common::ChargeCurrency;

/// A usage-based charge under a recurring application charge.
///
/// Usage charges allow apps to bill merchants based on resource consumption.
/// They require a parent `RecurringApplicationCharge` with a `capped_amount`.
///
/// # Nested Resource
///
/// This is a nested resource under `RecurringApplicationCharge`. All operations
/// require the parent `recurring_application_charge_id`.
///
/// Use `UsageCharge::all_with_parent()` to list charges under a specific
/// recurring charge.
///
/// # Fields
///
/// ## Read-Only Fields
/// - `id` - The unique identifier of the usage charge
/// - `currency` - The currency object with the currency code
/// - `created_at` - When the charge was created
/// - `updated_at` - When the charge was last updated
///
/// ## Writable Fields
/// - `recurring_application_charge_id` - The parent charge ID (required)
/// - `description` - Description shown to merchant
/// - `price` - The price of this usage
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct UsageCharge {
    /// The unique identifier of the usage charge.
    /// Read-only field.
    #[serde(skip_serializing)]
    pub id: Option<u64>,

    /// The ID of the parent recurring application charge.
    /// Required for creating new usage charges.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recurring_application_charge_id: Option<u64>,

    /// The description of the usage charge.
    /// Displayed to the merchant on their invoice.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// The price of the usage charge.
    /// Must be a string representing the monetary amount (e.g., "1.00").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub price: Option<String>,

    /// The currency information for the charge.
    /// Read-only field containing the currency code.
    #[serde(skip_serializing)]
    pub currency: Option<ChargeCurrency>,

    /// When the charge was created.
    /// Read-only field.
    #[serde(skip_serializing)]
    pub created_at: Option<DateTime<Utc>>,

    /// When the charge was last updated.
    /// Read-only field.
    #[serde(skip_serializing)]
    pub updated_at: Option<DateTime<Utc>>,
}

impl UsageCharge {
    /// Counts usage charges under a specific recurring application charge.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use for the request
    /// * `recurring_application_charge_id` - The parent charge ID
    /// * `params` - Optional parameters for filtering
    ///
    /// # Returns
    ///
    /// The count of matching usage charges as a `u64`.
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::PathResolutionFailed`] if no count path exists.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let count = UsageCharge::count_with_parent(&client, 455696195, None).await?;
    /// println!("Total usage charges: {}", count);
    /// ```
    pub async fn count_with_parent(
        client: &RestClient,
        recurring_application_charge_id: u64,
        _params: Option<()>,
    ) -> Result<u64, ResourceError> {
        let mut ids: HashMap<&str, String> = HashMap::new();
        ids.insert(
            "recurring_application_charge_id",
            recurring_application_charge_id.to_string(),
        );

        let available_ids: Vec<&str> = ids.keys().copied().collect();
        let path = get_path(Self::PATHS, ResourceOperation::Count, &available_ids).ok_or(
            ResourceError::PathResolutionFailed {
                resource: Self::NAME,
                operation: "count",
            },
        )?;

        let url = build_path(path.template, &ids);
        let response = client.get(&url, None).await?;

        if !response.is_ok() {
            return Err(ResourceError::from_http_response(
                response.code,
                &response.body,
                Self::NAME,
                None,
                response.request_id(),
            ));
        }

        let count = response
            .body
            .get("count")
            .and_then(serde_json::Value::as_u64)
            .ok_or_else(|| {
                ResourceError::Http(crate::clients::HttpError::Response(
                    crate::clients::HttpResponseError {
                        code: response.code,
                        message: "Missing 'count' in response".to_string(),
                        error_reference: response.request_id().map(ToString::to_string),
                    },
                ))
            })?;

        Ok(count)
    }

    /// Finds a single usage charge by ID under a recurring application charge.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use for the request
    /// * `recurring_application_charge_id` - The parent charge ID
    /// * `id` - The usage charge ID to find
    /// * `params` - Optional parameters for the request
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::NotFound`] if the usage charge doesn't exist.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let usage = UsageCharge::find_with_parent(&client, 455696195, 123, None).await?;
    /// ```
    pub async fn find_with_parent(
        client: &RestClient,
        recurring_application_charge_id: u64,
        id: u64,
        _params: Option<UsageChargeFindParams>,
    ) -> Result<ResourceResponse<Self>, ResourceError> {
        let mut ids: HashMap<&str, String> = HashMap::new();
        ids.insert(
            "recurring_application_charge_id",
            recurring_application_charge_id.to_string(),
        );
        ids.insert("id", id.to_string());

        let available_ids: Vec<&str> = ids.keys().copied().collect();
        let path = get_path(Self::PATHS, ResourceOperation::Find, &available_ids).ok_or(
            ResourceError::PathResolutionFailed {
                resource: Self::NAME,
                operation: "find",
            },
        )?;

        let url = build_path(path.template, &ids);
        let response = client.get(&url, None).await?;

        if !response.is_ok() {
            return Err(ResourceError::from_http_response(
                response.code,
                &response.body,
                Self::NAME,
                Some(&id.to_string()),
                response.request_id(),
            ));
        }

        let key = Self::resource_key();
        ResourceResponse::from_http_response(response, &key)
    }
}

impl RestResource for UsageCharge {
    type Id = u64;
    type FindParams = UsageChargeFindParams;
    type AllParams = UsageChargeListParams;
    type CountParams = ();

    const NAME: &'static str = "UsageCharge";
    const PLURAL: &'static str = "usage_charges";

    /// Paths for the UsageCharge resource.
    ///
    /// All paths require `recurring_application_charge_id` as UsageCharges
    /// are nested under RecurringApplicationCharges.
    ///
    /// Note: No Update or Delete paths - usage charges cannot be modified.
    const PATHS: &'static [ResourcePath] = &[
        ResourcePath::new(
            HttpMethod::Get,
            ResourceOperation::Find,
            &["recurring_application_charge_id", "id"],
            "recurring_application_charges/{recurring_application_charge_id}/usage_charges/{id}",
        ),
        ResourcePath::new(
            HttpMethod::Get,
            ResourceOperation::All,
            &["recurring_application_charge_id"],
            "recurring_application_charges/{recurring_application_charge_id}/usage_charges",
        ),
        ResourcePath::new(
            HttpMethod::Post,
            ResourceOperation::Create,
            &["recurring_application_charge_id"],
            "recurring_application_charges/{recurring_application_charge_id}/usage_charges",
        ),
        // Note: Count path not officially documented but following pattern
        // No Update or Delete paths - usage charges cannot be modified after creation
    ];

    fn get_id(&self) -> Option<Self::Id> {
        self.id
    }
}

/// Parameters for finding a single usage charge.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct UsageChargeFindParams {
    /// Comma-separated list of fields to include in the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fields: Option<String>,
}

/// Parameters for listing usage charges.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct UsageChargeListParams {
    /// Maximum number of results to return (default: 50, max: 250).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,

    /// Return charges after this ID.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub since_id: Option<u64>,

    /// Comma-separated list of fields to include in the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fields: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rest::{get_path, ResourceOperation};

    #[test]
    fn test_usage_charge_serialization() {
        let charge = UsageCharge {
            id: Some(12345),
            recurring_application_charge_id: Some(455696195),
            description: Some("100 emails sent".to_string()),
            price: Some("1.00".to_string()),
            currency: Some(ChargeCurrency::new("USD")),
            created_at: Some(
                DateTime::parse_from_rfc3339("2024-01-15T10:30:00Z")
                    .unwrap()
                    .with_timezone(&Utc),
            ),
            updated_at: Some(
                DateTime::parse_from_rfc3339("2024-01-15T10:35:00Z")
                    .unwrap()
                    .with_timezone(&Utc),
            ),
        };

        let json = serde_json::to_string(&charge).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&json).unwrap();

        // Writable fields should be present
        assert_eq!(parsed["recurring_application_charge_id"], 455696195);
        assert_eq!(parsed["description"], "100 emails sent");
        assert_eq!(parsed["price"], "1.00");

        // Read-only fields should be omitted
        assert!(parsed.get("id").is_none());
        assert!(parsed.get("currency").is_none());
        assert!(parsed.get("created_at").is_none());
        assert!(parsed.get("updated_at").is_none());
    }

    #[test]
    fn test_usage_charge_deserialization() {
        let json = r#"{
            "id": 1034618207,
            "recurring_application_charge_id": 455696195,
            "description": "Super Mega Plan 1000 emails",
            "price": "1.00",
            "currency": {
                "currency": "USD"
            },
            "created_at": "2024-01-15T10:30:00Z",
            "updated_at": "2024-01-15T10:35:00Z"
        }"#;

        let charge: UsageCharge = serde_json::from_str(json).unwrap();

        assert_eq!(charge.id, Some(1034618207));
        assert_eq!(charge.recurring_application_charge_id, Some(455696195));
        assert_eq!(
            charge.description,
            Some("Super Mega Plan 1000 emails".to_string())
        );
        assert_eq!(charge.price, Some("1.00".to_string()));
        assert_eq!(charge.currency.as_ref().unwrap().code(), Some("USD"));
        assert!(charge.created_at.is_some());
        assert!(charge.updated_at.is_some());
    }

    #[test]
    fn test_usage_charge_nested_paths() {
        // All paths should require recurring_application_charge_id

        // Find requires both recurring_application_charge_id and id
        let find_path = get_path(
            UsageCharge::PATHS,
            ResourceOperation::Find,
            &["recurring_application_charge_id", "id"],
        );
        assert!(find_path.is_some());
        assert_eq!(
            find_path.unwrap().template,
            "recurring_application_charges/{recurring_application_charge_id}/usage_charges/{id}"
        );

        // Find with only id should fail (no standalone path)
        let find_without_parent = get_path(UsageCharge::PATHS, ResourceOperation::Find, &["id"]);
        assert!(find_without_parent.is_none());

        // All requires recurring_application_charge_id
        let all_path = get_path(
            UsageCharge::PATHS,
            ResourceOperation::All,
            &["recurring_application_charge_id"],
        );
        assert!(all_path.is_some());
        assert_eq!(
            all_path.unwrap().template,
            "recurring_application_charges/{recurring_application_charge_id}/usage_charges"
        );

        // All without parent should fail
        let all_without_parent = get_path(UsageCharge::PATHS, ResourceOperation::All, &[]);
        assert!(all_without_parent.is_none());

        // Create requires recurring_application_charge_id
        let create_path = get_path(
            UsageCharge::PATHS,
            ResourceOperation::Create,
            &["recurring_application_charge_id"],
        );
        assert!(create_path.is_some());
        assert_eq!(
            create_path.unwrap().template,
            "recurring_application_charges/{recurring_application_charge_id}/usage_charges"
        );
        assert_eq!(create_path.unwrap().http_method, HttpMethod::Post);

        // No Update path
        let update_path = get_path(
            UsageCharge::PATHS,
            ResourceOperation::Update,
            &["recurring_application_charge_id", "id"],
        );
        assert!(update_path.is_none());

        // No Delete path
        let delete_path = get_path(
            UsageCharge::PATHS,
            ResourceOperation::Delete,
            &["recurring_application_charge_id", "id"],
        );
        assert!(delete_path.is_none());
    }

    #[test]
    fn test_usage_charge_list_params() {
        let params = UsageChargeListParams {
            limit: Some(50),
            since_id: Some(100),
            fields: Some("id,description,price".to_string()),
        };

        let json = serde_json::to_value(&params).unwrap();
        assert_eq!(json["limit"], 50);
        assert_eq!(json["since_id"], 100);
        assert_eq!(json["fields"], "id,description,price");

        // Empty params should serialize to empty object
        let empty_params = UsageChargeListParams::default();
        let empty_json = serde_json::to_value(&empty_params).unwrap();
        assert_eq!(empty_json, serde_json::json!({}));
    }

    #[test]
    fn test_usage_charge_constants() {
        assert_eq!(UsageCharge::NAME, "UsageCharge");
        assert_eq!(UsageCharge::PLURAL, "usage_charges");
    }

    #[test]
    fn test_usage_charge_get_id() {
        let charge_with_id = UsageCharge {
            id: Some(12345),
            ..Default::default()
        };
        assert_eq!(charge_with_id.get_id(), Some(12345));

        let charge_without_id = UsageCharge::default();
        assert_eq!(charge_without_id.get_id(), None);
    }

    #[test]
    fn test_usage_charge_with_currency_nested_object() {
        // Test that currency deserializes correctly as a nested object
        let json = r#"{
            "id": 123,
            "recurring_application_charge_id": 456,
            "description": "Test charge",
            "price": "5.00",
            "currency": {
                "currency": "EUR"
            }
        }"#;

        let charge: UsageCharge = serde_json::from_str(json).unwrap();
        assert!(charge.currency.is_some());
        let currency = charge.currency.unwrap();
        assert_eq!(currency.code(), Some("EUR"));
    }
}