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
//! Blog resource implementation.
//!
//! This module provides the Blog resource, which represents a blog
//! in a Shopify store. Blogs are containers for articles.
//!
//! # Example
//!
//! ```rust,ignore
//! use shopify_sdk::rest::{RestResource, ResourceResponse};
//! use shopify_sdk::rest::resources::v2025_10::{Blog, BlogListParams};
//! use shopify_sdk::rest::resources::v2025_10::common::BlogCommentable;
//!
//! // Find a single blog
//! let blog = Blog::find(&client, 123, None).await?;
//! println!("Blog: {}", blog.title.as_deref().unwrap_or(""));
//!
//! // List blogs
//! let params = BlogListParams {
//!     limit: Some(50),
//!     ..Default::default()
//! };
//! let blogs = Blog::all(&client, Some(params)).await?;
//!
//! // Create a new blog with comment moderation
//! let mut blog = Blog {
//!     title: Some("News".to_string()),
//!     commentable: Some(BlogCommentable::Moderate),
//!     ..Default::default()
//! };
//! let saved = blog.save(&client).await?;
//!
//! // Count blogs
//! let count = Blog::count(&client, None).await?;
//! println!("Total blogs: {}", count);
//! ```

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

use crate::rest::{ResourceOperation, ResourcePath, RestResource};
use crate::HttpMethod;

use super::common::BlogCommentable;

/// A blog in a Shopify store.
///
/// Blogs are containers for articles. A store can have multiple blogs,
/// each with its own set of articles. Blogs support comment settings
/// and can integrate with Feedburner.
///
/// # Fields
///
/// ## Read-Only Fields
/// - `id` - The unique identifier of the blog
/// - `created_at` - When the blog was created
/// - `updated_at` - When the blog was last updated
/// - `admin_graphql_api_id` - The GraphQL API ID
///
/// ## Writable Fields
/// - `title` - The title of the blog
/// - `handle` - The URL-friendly handle (auto-generated from title if not set)
/// - `commentable` - Comment settings (no, moderate, yes)
/// - `template_suffix` - The suffix of the Liquid template used for the blog
/// - `feedburner` - The Feedburner URL (optional)
/// - `feedburner_location` - The Feedburner location (optional)
/// - `tags` - Comma-separated tags for the blog
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct Blog {
    /// The unique identifier of the blog.
    /// Read-only field.
    #[serde(skip_serializing)]
    pub id: Option<u64>,

    /// The title of the blog.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// The URL-friendly handle of the blog.
    /// Auto-generated from the title if not specified.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<String>,

    /// The comment moderation setting for the blog.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub commentable: Option<BlogCommentable>,

    /// The suffix of the Liquid template used for the blog.
    /// For example, if the value is "custom", the blog uses the
    /// `blog.custom.liquid` template.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub template_suffix: Option<String>,

    /// The Feedburner URL for the blog.
    /// Used to redirect RSS feeds through Feedburner.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feedburner: Option<String>,

    /// The Feedburner location.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub feedburner_location: Option<String>,

    /// Comma-separated tags for the blog.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tags: Option<String>,

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

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

    /// The admin GraphQL API ID for this blog.
    /// Read-only field.
    #[serde(skip_serializing)]
    pub admin_graphql_api_id: Option<String>,
}

impl RestResource for Blog {
    type Id = u64;
    type FindParams = BlogFindParams;
    type AllParams = BlogListParams;
    type CountParams = BlogCountParams;

    const NAME: &'static str = "Blog";
    const PLURAL: &'static str = "blogs";

    const PATHS: &'static [ResourcePath] = &[
        ResourcePath::new(
            HttpMethod::Get,
            ResourceOperation::Find,
            &["id"],
            "blogs/{id}",
        ),
        ResourcePath::new(HttpMethod::Get, ResourceOperation::All, &[], "blogs"),
        ResourcePath::new(
            HttpMethod::Get,
            ResourceOperation::Count,
            &[],
            "blogs/count",
        ),
        ResourcePath::new(HttpMethod::Post, ResourceOperation::Create, &[], "blogs"),
        ResourcePath::new(
            HttpMethod::Put,
            ResourceOperation::Update,
            &["id"],
            "blogs/{id}",
        ),
        ResourcePath::new(
            HttpMethod::Delete,
            ResourceOperation::Delete,
            &["id"],
            "blogs/{id}",
        ),
    ];

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

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

/// Parameters for listing blogs.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct BlogListParams {
    /// Filter by blog handle.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<String>,

    /// Show blogs created after this date.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at_min: Option<DateTime<Utc>>,

    /// Show blogs created before this date.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub created_at_max: Option<DateTime<Utc>>,

    /// Show blogs updated after this date.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at_min: Option<DateTime<Utc>>,

    /// Show blogs updated before this date.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub updated_at_max: Option<DateTime<Utc>>,

    /// Maximum number of results to return (default: 50, max: 250).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,

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

    /// Cursor for pagination.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_info: Option<String>,

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

/// Parameters for counting blogs.
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
pub struct BlogCountParams {
    // No specific count params for blogs
}

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

    #[test]
    fn test_blog_struct_serialization() {
        let blog = Blog {
            id: Some(12345),
            title: Some("Company News".to_string()),
            handle: Some("news".to_string()),
            commentable: Some(BlogCommentable::Moderate),
            template_suffix: Some("custom".to_string()),
            feedburner: Some("https://feeds.feedburner.com/example".to_string()),
            feedburner_location: Some("example".to_string()),
            tags: Some("news, updates".to_string()),
            created_at: Some(
                DateTime::parse_from_rfc3339("2024-01-10T08:00:00Z")
                    .unwrap()
                    .with_timezone(&Utc),
            ),
            updated_at: Some(
                DateTime::parse_from_rfc3339("2024-06-20T15:45:00Z")
                    .unwrap()
                    .with_timezone(&Utc),
            ),
            admin_graphql_api_id: Some("gid://shopify/OnlineStoreBlog/12345".to_string()),
        };

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

        // Writable fields should be present
        assert_eq!(parsed["title"], "Company News");
        assert_eq!(parsed["handle"], "news");
        assert_eq!(parsed["commentable"], "moderate");
        assert_eq!(parsed["template_suffix"], "custom");
        assert_eq!(parsed["feedburner"], "https://feeds.feedburner.com/example");
        assert_eq!(parsed["feedburner_location"], "example");
        assert_eq!(parsed["tags"], "news, updates");

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

    #[test]
    fn test_blog_deserialization_from_api_response() {
        let json = r#"{
            "id": 241253187,
            "handle": "apple-blog",
            "title": "Apple Blog",
            "updated_at": "2024-06-20T15:45:00Z",
            "commentable": "no",
            "feedburner": null,
            "feedburner_location": null,
            "created_at": "2024-01-10T08:00:00Z",
            "template_suffix": null,
            "tags": "apple, tech",
            "admin_graphql_api_id": "gid://shopify/OnlineStoreBlog/241253187"
        }"#;

        let blog: Blog = serde_json::from_str(json).unwrap();

        assert_eq!(blog.id, Some(241253187));
        assert_eq!(blog.handle, Some("apple-blog".to_string()));
        assert_eq!(blog.title, Some("Apple Blog".to_string()));
        assert_eq!(blog.commentable, Some(BlogCommentable::No));
        assert!(blog.feedburner.is_none());
        assert!(blog.feedburner_location.is_none());
        assert!(blog.template_suffix.is_none());
        assert_eq!(blog.tags, Some("apple, tech".to_string()));
        assert!(blog.created_at.is_some());
        assert!(blog.updated_at.is_some());
        assert_eq!(
            blog.admin_graphql_api_id,
            Some("gid://shopify/OnlineStoreBlog/241253187".to_string())
        );
    }

    #[test]
    fn test_blog_commentable_enum_handling() {
        // Test all BlogCommentable variants in blog context
        let variants = [
            (BlogCommentable::No, "no"),
            (BlogCommentable::Moderate, "moderate"),
            (BlogCommentable::Yes, "yes"),
        ];

        for (commentable, expected_str) in variants {
            let blog = Blog {
                title: Some("Test Blog".to_string()),
                commentable: Some(commentable),
                ..Default::default()
            };

            let json = serde_json::to_value(&blog).unwrap();
            assert_eq!(json["commentable"], expected_str);
        }
    }

    #[test]
    fn test_blog_list_params_serialization() {
        let params = BlogListParams {
            handle: Some("news".to_string()),
            limit: Some(50),
            since_id: Some(100),
            ..Default::default()
        };

        let json = serde_json::to_value(&params).unwrap();

        assert_eq!(json["handle"], "news");
        assert_eq!(json["limit"], 50);
        assert_eq!(json["since_id"], 100);

        // Fields not set should be omitted
        assert!(json.get("created_at_min").is_none());
        assert!(json.get("page_info").is_none());
    }

    #[test]
    fn test_blog_path_constants_are_correct() {
        // Test Find path
        let find_path = get_path(Blog::PATHS, ResourceOperation::Find, &["id"]);
        assert!(find_path.is_some());
        assert_eq!(find_path.unwrap().template, "blogs/{id}");
        assert_eq!(find_path.unwrap().http_method, HttpMethod::Get);

        // Test All path
        let all_path = get_path(Blog::PATHS, ResourceOperation::All, &[]);
        assert!(all_path.is_some());
        assert_eq!(all_path.unwrap().template, "blogs");
        assert_eq!(all_path.unwrap().http_method, HttpMethod::Get);

        // Test Count path
        let count_path = get_path(Blog::PATHS, ResourceOperation::Count, &[]);
        assert!(count_path.is_some());
        assert_eq!(count_path.unwrap().template, "blogs/count");
        assert_eq!(count_path.unwrap().http_method, HttpMethod::Get);

        // Test Create path
        let create_path = get_path(Blog::PATHS, ResourceOperation::Create, &[]);
        assert!(create_path.is_some());
        assert_eq!(create_path.unwrap().template, "blogs");
        assert_eq!(create_path.unwrap().http_method, HttpMethod::Post);

        // Test Update path
        let update_path = get_path(Blog::PATHS, ResourceOperation::Update, &["id"]);
        assert!(update_path.is_some());
        assert_eq!(update_path.unwrap().template, "blogs/{id}");
        assert_eq!(update_path.unwrap().http_method, HttpMethod::Put);

        // Test Delete path
        let delete_path = get_path(Blog::PATHS, ResourceOperation::Delete, &["id"]);
        assert!(delete_path.is_some());
        assert_eq!(delete_path.unwrap().template, "blogs/{id}");
        assert_eq!(delete_path.unwrap().http_method, HttpMethod::Delete);

        // Verify constants
        assert_eq!(Blog::NAME, "Blog");
        assert_eq!(Blog::PLURAL, "blogs");
    }

    #[test]
    fn test_blog_get_id_returns_correct_value() {
        // Blog with ID
        let blog_with_id = Blog {
            id: Some(123456789),
            title: Some("Test Blog".to_string()),
            ..Default::default()
        };
        assert_eq!(blog_with_id.get_id(), Some(123456789));

        // Blog without ID (new blog)
        let blog_without_id = Blog {
            id: None,
            title: Some("New Blog".to_string()),
            ..Default::default()
        };
        assert_eq!(blog_without_id.get_id(), None);
    }

    #[test]
    fn test_blog_tags_field_handling() {
        // Tags as comma-separated string
        let blog = Blog {
            title: Some("Tech Blog".to_string()),
            tags: Some("tech, programming, rust, web".to_string()),
            ..Default::default()
        };

        let json = serde_json::to_value(&blog).unwrap();
        assert_eq!(json["tags"], "tech, programming, rust, web");

        // Deserialize tags back
        let deserialized: Blog = serde_json::from_value(json).unwrap();
        assert_eq!(
            deserialized.tags,
            Some("tech, programming, rust, web".to_string())
        );
    }
}