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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! REST Resource trait for CRUD operations.
//!
//! This module defines the [`RestResource`] trait, which provides a standardized
//! interface for interacting with Shopify REST API resources. Resources that
//! implement this trait gain `find()`, `all()`, `save()`, `delete()`, and
//! `count()` methods.
//!
//! It also defines the [`ReadOnlyResource`] marker trait for resources that
//! only support read operations (GET requests).
//!
//! # Implementing a Resource
//!
//! To implement a REST resource:
//!
//! 1. Define a struct with serde derives
//! 2. Implement the `RestResource` trait with associated types and constants
//! 3. The trait provides default implementations for CRUD operations
//!
//! # Example
//!
//! ```rust,ignore
//! use shopify_sdk::rest::{RestResource, ResourcePath, ResourceOperation, ResourceResponse, ResourceError};
//! use shopify_sdk::{HttpMethod, RestClient};
//! use serde::{Serialize, Deserialize};
//!
//! #[derive(Debug, Clone, Serialize, Deserialize)]
//! pub struct Product {
//!     pub id: Option<u64>,
//!     pub title: String,
//!     #[serde(skip_serializing_if = "Option::is_none")]
//!     pub vendor: Option<String>,
//! }
//!
//! impl RestResource for Product {
//!     type Id = u64;
//!     type FindParams = ();
//!     type AllParams = ProductListParams;
//!     type CountParams = ();
//!
//!     const NAME: &'static str = "Product";
//!     const PLURAL: &'static str = "products";
//!     const PATHS: &'static [ResourcePath] = &[
//!         ResourcePath::new(HttpMethod::Get, ResourceOperation::Find, &["id"], "products/{id}"),
//!         ResourcePath::new(HttpMethod::Get, ResourceOperation::All, &[], "products"),
//!         ResourcePath::new(HttpMethod::Get, ResourceOperation::Count, &[], "products/count"),
//!         ResourcePath::new(HttpMethod::Post, ResourceOperation::Create, &[], "products"),
//!         ResourcePath::new(HttpMethod::Put, ResourceOperation::Update, &["id"], "products/{id}"),
//!         ResourcePath::new(HttpMethod::Delete, ResourceOperation::Delete, &["id"], "products/{id}"),
//!     ];
//!
//!     fn get_id(&self) -> Option<Self::Id> {
//!         self.id
//!     }
//! }
//!
//! // Usage:
//! let product = Product::find(&client, 123, None).await?;
//! let products = Product::all(&client, None).await?;
//! ```
//!
//! # Read-Only Resources
//!
//! For resources that only support read operations (like Event, Policy, Location),
//! implement the [`ReadOnlyResource`] marker trait in addition to `RestResource`.
//! This provides compile-time documentation of the resource's capabilities.
//!
//! ```rust,ignore
//! // Read-only resources implement both traits
//! impl RestResource for Location { /* ... only GET paths ... */ }
//! impl ReadOnlyResource for Location {}
//! ```

use std::collections::HashMap;
use std::fmt::Display;

use serde::{de::DeserializeOwned, Serialize};
use serde_json::Value;

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

/// A marker trait for REST resources that only support read operations.
///
/// Resources implementing this trait only support GET requests (find, all, count)
/// and do not have Create, Update, or Delete operations. This serves as compile-time
/// documentation of the resource's capabilities and can be used for blanket
/// implementations that restrict certain behaviors.
///
/// # Resources with this trait
///
/// The following Shopify resources are read-only:
/// - `Event` - Store events (read-only audit log)
/// - `Policy` - Store legal policies
/// - `Location` - Store locations
/// - `Currency` - Enabled currencies
/// - `User` - Store staff users
/// - `AccessScope` - App access scopes
///
/// # Example
///
/// ```rust,ignore
/// use shopify_sdk::rest::{RestResource, ReadOnlyResource};
///
/// // Location only supports GET operations
/// impl RestResource for Location {
///     const PATHS: &'static [ResourcePath] = &[
///         ResourcePath::new(HttpMethod::Get, ResourceOperation::Find, &["id"], "locations/{id}"),
///         ResourcePath::new(HttpMethod::Get, ResourceOperation::All, &[], "locations"),
///         ResourcePath::new(HttpMethod::Get, ResourceOperation::Count, &[], "locations/count"),
///         // No Create, Update, or Delete paths
///     ];
///     // ...
/// }
///
/// // Mark as read-only
/// impl ReadOnlyResource for Location {}
/// ```
pub trait ReadOnlyResource: RestResource {}

/// A REST resource that can be fetched, created, updated, and deleted.
///
/// This trait provides a standardized interface for CRUD operations on
/// Shopify REST API resources. Implementors define the resource's paths,
/// name, and parameter types, and get default implementations for all
/// CRUD methods.
///
/// # Associated Types
///
/// - `Id`: The type of the resource's identifier (usually `u64` or `String`)
/// - `FindParams`: Parameters for `find()` operations (use `()` if none)
/// - `AllParams`: Parameters for `all()` operations (pagination, filters, etc.)
/// - `CountParams`: Parameters for `count()` operations
///
/// # Associated Constants
///
/// - `NAME`: The singular resource name (e.g., "Product")
/// - `PLURAL`: The plural form used in URLs (e.g., "products")
/// - `PATHS`: Available paths for different operations
/// - `PREFIX`: Optional path prefix for nested resources
///
/// # Required Bounds
///
/// Resources must be serializable, deserializable, cloneable, and thread-safe.
#[allow(async_fn_in_trait)]
pub trait RestResource: Serialize + DeserializeOwned + Clone + Send + Sync + Sized {
    /// The type of the resource's identifier.
    type Id: Display + Clone + Send + Sync;

    /// Parameters for `find()` operations.
    ///
    /// Use `()` if no parameters are needed.
    type FindParams: Serialize + Default + Send + Sync;

    /// Parameters for `all()` operations (filtering, pagination, etc.).
    ///
    /// Use `()` if no parameters are needed.
    type AllParams: Serialize + Default + Send + Sync;

    /// Parameters for `count()` operations.
    ///
    /// Use `()` if no parameters are needed.
    type CountParams: Serialize + Default + Send + Sync;

    /// The singular name of the resource (e.g., "Product").
    ///
    /// Used in error messages and as the response body key for single resources.
    const NAME: &'static str;

    /// The plural name used in URL paths (e.g., "products").
    ///
    /// Used as the response body key for collection operations.
    const PLURAL: &'static str;

    /// Available paths for this resource.
    ///
    /// Define paths for each operation the resource supports. The path
    /// selection logic will choose the most specific path that matches
    /// the available IDs.
    const PATHS: &'static [ResourcePath];

    /// Optional path prefix for nested resources.
    ///
    /// Override this for resources that always appear under a parent path.
    const PREFIX: Option<&'static str> = None;

    /// Returns the resource's ID if it exists.
    ///
    /// Returns `None` for new resources that haven't been saved yet.
    fn get_id(&self) -> Option<Self::Id>;

    /// Returns the lowercase key used in JSON request/response bodies.
    #[must_use]
    fn resource_key() -> String {
        Self::NAME.to_lowercase()
    }

    /// Finds a single resource by ID.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use for the request
    /// * `id` - The resource ID to find
    /// * `params` - Optional parameters for the request
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::NotFound`] if the resource doesn't exist.
    /// Returns [`ResourceError::PathResolutionFailed`] if no valid path matches.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let product = Product::find(&client, 123, None).await?;
    /// println!("Found: {}", product.title);
    /// ```
    async fn find(
        client: &RestClient,
        id: Self::Id,
        params: Option<Self::FindParams>,
    ) -> Result<ResourceResponse<Self>, ResourceError> {
        // Build the path
        let mut ids: HashMap<&str, String> = HashMap::new();
        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 full_path = Self::build_full_path(&url);

        // Build query params from FindParams
        let query = params
            .map(|p| serialize_to_query(&p))
            .transpose()?
            .filter(|q| !q.is_empty());

        // Make the request
        let response = client.get(&full_path, query).await?;

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

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

    /// Lists all resources matching the given parameters.
    ///
    /// Returns a paginated response. Use `has_next_page()` and `next_page_info()`
    /// to navigate through pages.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use for the request
    /// * `params` - Optional parameters for filtering/pagination
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::PathResolutionFailed`] if no valid path matches.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let response = Product::all(&client, None).await?;
    /// for product in response.iter() {
    ///     println!("Product: {}", product.title);
    /// }
    ///
    /// if response.has_next_page() {
    ///     // Fetch next page...
    /// }
    /// ```
    async fn all(
        client: &RestClient,
        params: Option<Self::AllParams>,
    ) -> Result<ResourceResponse<Vec<Self>>, ResourceError> {
        let path = get_path(Self::PATHS, ResourceOperation::All, &[]).ok_or(
            ResourceError::PathResolutionFailed {
                resource: Self::NAME,
                operation: "all",
            },
        )?;

        let url = path.template;
        let full_path = Self::build_full_path(url);

        // Build query params from AllParams
        let query = params
            .map(|p| serialize_to_query(&p))
            .transpose()?
            .filter(|q| !q.is_empty());

        // Make the request
        let response = client.get(&full_path, query).await?;

        // Check for error status codes
        if !response.is_ok() {
            return Err(ResourceError::from_http_response(
                response.code,
                &response.body,
                Self::NAME,
                None,
                response.request_id(),
            ));
        }

        // Parse the response
        ResourceResponse::from_http_response(response, Self::PLURAL)
    }

    /// Lists resources with a specific parent resource ID.
    ///
    /// For nested resources that require a parent ID (e.g., variants under products).
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use
    /// * `parent_id_name` - The name of the parent ID parameter (e.g., `product_id`)
    /// * `parent_id` - The parent resource ID
    /// * `params` - Optional parameters for filtering/pagination
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::PathResolutionFailed`] if no valid path matches.
    async fn all_with_parent<ParentId: Display + Send>(
        client: &RestClient,
        parent_id_name: &str,
        parent_id: ParentId,
        params: Option<Self::AllParams>,
    ) -> Result<ResourceResponse<Vec<Self>>, ResourceError> {
        let mut ids: HashMap<&str, String> = HashMap::new();
        ids.insert(parent_id_name, parent_id.to_string());

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

        let url = build_path(path.template, &ids);
        let full_path = Self::build_full_path(&url);

        let query = params
            .map(|p| serialize_to_query(&p))
            .transpose()?
            .filter(|q| !q.is_empty());

        let response = client.get(&full_path, query).await?;

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

        ResourceResponse::from_http_response(response, Self::PLURAL)
    }

    /// Saves the resource (create or update).
    ///
    /// For new resources (no ID), sends a POST request to create.
    /// For existing resources (has ID), sends a PUT request to update.
    ///
    /// When updating, only changed fields are sent if dirty tracking is used.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use
    ///
    /// # Returns
    ///
    /// The saved resource with any server-generated fields populated.
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::ValidationFailed`] if validation fails (422).
    /// Returns [`ResourceError::NotFound`] if updating a non-existent resource.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// // Create new
    /// let mut product = Product { id: None, title: "New".to_string(), vendor: None };
    /// let saved = product.save(&client).await?;
    ///
    /// // Update existing
    /// let mut product = Product::find(&client, 123, None).await?.into_inner();
    /// product.title = "Updated".to_string();
    /// let saved = product.save(&client).await?;
    /// ```
    async fn save(&self, client: &RestClient) -> Result<Self, ResourceError> {
        let is_new = self.get_id().is_none();
        let key = Self::resource_key();

        if is_new {
            // Create (POST)
            let path = get_path(Self::PATHS, ResourceOperation::Create, &[]).ok_or(
                ResourceError::PathResolutionFailed {
                    resource: Self::NAME,
                    operation: "create",
                },
            )?;

            let url = path.template;
            let full_path = Self::build_full_path(url);

            // Wrap in resource key - use a map to avoid move issues
            let mut body_map = serde_json::Map::new();
            body_map.insert(
                key.clone(),
                serde_json::to_value(self).map_err(|e| {
                    ResourceError::Http(crate::clients::HttpError::Response(
                        crate::clients::HttpResponseError {
                            code: 400,
                            message: format!("Failed to serialize resource: {e}"),
                            error_reference: None,
                        },
                    ))
                })?,
            );
            let body = Value::Object(body_map);

            let response = client.post(&full_path, body, None).await?;

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

            let result: ResourceResponse<Self> =
                ResourceResponse::from_http_response(response, &key)?;
            Ok(result.into_inner())
        } else {
            // Update (PUT)
            let id = self.get_id().unwrap();

            let mut ids: HashMap<&str, String> = HashMap::new();
            ids.insert("id", id.to_string());

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

            let url = build_path(path.template, &ids);
            let full_path = Self::build_full_path(&url);

            // Wrap in resource key - use a map to avoid move issues
            let mut body_map = serde_json::Map::new();
            body_map.insert(
                key.clone(),
                serde_json::to_value(self).map_err(|e| {
                    ResourceError::Http(crate::clients::HttpError::Response(
                        crate::clients::HttpResponseError {
                            code: 400,
                            message: format!("Failed to serialize resource: {e}"),
                            error_reference: None,
                        },
                    ))
                })?,
            );
            let body = Value::Object(body_map);

            let response = client.put(&full_path, body, 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 result: ResourceResponse<Self> =
                ResourceResponse::from_http_response(response, &key)?;
            Ok(result.into_inner())
        }
    }

    /// Saves the resource with dirty tracking for partial updates.
    ///
    /// For existing resources, only sends changed fields in the PUT request.
    /// This is more efficient than `save()` for large resources.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use
    /// * `changed_fields` - JSON value containing only the changed fields
    ///
    /// # Returns
    ///
    /// The saved resource with server-generated fields populated.
    async fn save_partial(
        &self,
        client: &RestClient,
        changed_fields: Value,
    ) -> Result<Self, ResourceError> {
        let id = self.get_id().ok_or(ResourceError::PathResolutionFailed {
            resource: Self::NAME,
            operation: "update",
        })?;

        let key = Self::resource_key();

        let mut ids: HashMap<&str, String> = HashMap::new();
        ids.insert("id", id.to_string());

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

        let url = build_path(path.template, &ids);
        let full_path = Self::build_full_path(&url);

        // Wrap changed fields in resource key - use a map to avoid move issues
        let mut body_map = serde_json::Map::new();
        body_map.insert(key.clone(), changed_fields);
        let body = Value::Object(body_map);

        let response = client.put(&full_path, body, 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 result: ResourceResponse<Self> = ResourceResponse::from_http_response(response, &key)?;
        Ok(result.into_inner())
    }

    /// Deletes the resource.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::NotFound`] if the resource doesn't exist.
    /// Returns [`ResourceError::PathResolutionFailed`] if no delete path exists.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let product = Product::find(&client, 123, None).await?;
    /// product.delete(&client).await?;
    /// ```
    async fn delete(&self, client: &RestClient) -> Result<(), ResourceError> {
        let id = self.get_id().ok_or(ResourceError::PathResolutionFailed {
            resource: Self::NAME,
            operation: "delete",
        })?;

        let mut ids: HashMap<&str, String> = HashMap::new();
        ids.insert("id", id.to_string());

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

        let url = build_path(path.template, &ids);
        let full_path = Self::build_full_path(&url);

        let response = client.delete(&full_path, 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(),
            ));
        }

        Ok(())
    }

    /// Counts resources matching the given parameters.
    ///
    /// # Arguments
    ///
    /// * `client` - The REST client to use
    /// * `params` - Optional parameters for filtering
    ///
    /// # Returns
    ///
    /// The count of matching resources as a `u64`.
    ///
    /// # Errors
    ///
    /// Returns [`ResourceError::PathResolutionFailed`] if no count path exists.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let count = Product::count(&client, None).await?;
    /// println!("Total products: {}", count);
    /// ```
    async fn count(
        client: &RestClient,
        params: Option<Self::CountParams>,
    ) -> Result<u64, ResourceError> {
        let path = get_path(Self::PATHS, ResourceOperation::Count, &[]).ok_or(
            ResourceError::PathResolutionFailed {
                resource: Self::NAME,
                operation: "count",
            },
        )?;

        let url = path.template;
        let full_path = Self::build_full_path(url);

        let query = params
            .map(|p| serialize_to_query(&p))
            .transpose()?
            .filter(|q| !q.is_empty());

        let response = client.get(&full_path, query).await?;

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

        // Extract count from response
        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)
    }

    /// Builds the full path including any prefix.
    #[must_use]
    fn build_full_path(path: &str) -> String {
        Self::PREFIX.map_or_else(|| path.to_string(), |prefix| format!("{prefix}/{path}"))
    }
}

/// Serializes a params struct to a query parameter map.
fn serialize_to_query<T: Serialize>(params: &T) -> Result<HashMap<String, String>, ResourceError> {
    let value = serde_json::to_value(params).map_err(|e| {
        ResourceError::Http(crate::clients::HttpError::Response(
            crate::clients::HttpResponseError {
                code: 400,
                message: format!("Failed to serialize params: {e}"),
                error_reference: None,
            },
        ))
    })?;

    let mut query = HashMap::new();

    if let Value::Object(map) = value {
        for (key, val) in map {
            match val {
                Value::Null => {} // Skip null values
                Value::String(s) => {
                    query.insert(key, s);
                }
                Value::Number(n) => {
                    query.insert(key, n.to_string());
                }
                Value::Bool(b) => {
                    query.insert(key, b.to_string());
                }
                Value::Array(arr) => {
                    // Convert arrays to comma-separated values
                    let values: Vec<String> = arr
                        .iter()
                        .filter_map(|v| match v {
                            Value::String(s) => Some(s.clone()),
                            Value::Number(n) => Some(n.to_string()),
                            _ => None,
                        })
                        .collect();
                    if !values.is_empty() {
                        query.insert(key, values.join(","));
                    }
                }
                Value::Object(_) => {
                    // For complex objects, serialize as JSON string
                    query.insert(key, val.to_string());
                }
            }
        }
    }

    Ok(query)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rest::{ResourceOperation, ResourcePath};
    use crate::HttpMethod;
    use serde::{Deserialize, Serialize};

    // Test resource implementation
    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct MockProduct {
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<u64>,
        title: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        vendor: Option<String>,
    }

    #[derive(Debug, Clone, Serialize, Deserialize, Default)]
    struct MockProductParams {
        #[serde(skip_serializing_if = "Option::is_none")]
        limit: Option<u32>,
        #[serde(skip_serializing_if = "Option::is_none")]
        page_info: Option<String>,
    }

    impl RestResource for MockProduct {
        type Id = u64;
        type FindParams = ();
        type AllParams = MockProductParams;
        type CountParams = ();

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

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

    // Nested resource for testing
    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct MockVariant {
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<u64>,
        #[serde(skip_serializing_if = "Option::is_none")]
        product_id: Option<u64>,
        title: String,
    }

    impl RestResource for MockVariant {
        type Id = u64;
        type FindParams = ();
        type AllParams = ();
        type CountParams = ();

        const NAME: &'static str = "Variant";
        const PLURAL: &'static str = "variants";
        const PATHS: &'static [ResourcePath] = &[
            // Nested paths (more specific)
            ResourcePath::new(
                HttpMethod::Get,
                ResourceOperation::Find,
                &["product_id", "id"],
                "products/{product_id}/variants/{id}",
            ),
            ResourcePath::new(
                HttpMethod::Get,
                ResourceOperation::All,
                &["product_id"],
                "products/{product_id}/variants",
            ),
            // Standalone paths (less specific)
            ResourcePath::new(
                HttpMethod::Get,
                ResourceOperation::Find,
                &["id"],
                "variants/{id}",
            ),
        ];

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

    // Mock read-only resource for testing ReadOnlyResource trait
    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct MockLocation {
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<u64>,
        name: String,
        active: bool,
    }

    impl RestResource for MockLocation {
        type Id = u64;
        type FindParams = ();
        type AllParams = ();
        type CountParams = ();

        const NAME: &'static str = "Location";
        const PLURAL: &'static str = "locations";
        const PATHS: &'static [ResourcePath] = &[
            ResourcePath::new(
                HttpMethod::Get,
                ResourceOperation::Find,
                &["id"],
                "locations/{id}",
            ),
            ResourcePath::new(HttpMethod::Get, ResourceOperation::All, &[], "locations"),
            ResourcePath::new(
                HttpMethod::Get,
                ResourceOperation::Count,
                &[],
                "locations/count",
            ),
            // No Create, Update, or Delete paths - read-only resource
        ];

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

    // Implement ReadOnlyResource for MockLocation
    impl ReadOnlyResource for MockLocation {}

    #[test]
    fn test_resource_defines_name_and_paths() {
        assert_eq!(MockProduct::NAME, "Product");
        assert_eq!(MockProduct::PLURAL, "products");
        assert!(!MockProduct::PATHS.is_empty());
    }

    #[test]
    fn test_get_id_returns_none_for_new_resource() {
        let product = MockProduct {
            id: None,
            title: "New".to_string(),
            vendor: None,
        };
        assert!(product.get_id().is_none());
    }

    #[test]
    fn test_get_id_returns_some_for_existing_resource() {
        let product = MockProduct {
            id: Some(123),
            title: "Existing".to_string(),
            vendor: None,
        };
        assert_eq!(product.get_id(), Some(123));
    }

    #[test]
    fn test_build_full_path_without_prefix() {
        let path = MockProduct::build_full_path("products/123");
        assert_eq!(path, "products/123");
    }

    #[test]
    fn test_serialize_to_query_handles_basic_types() {
        #[derive(Serialize)]
        struct Params {
            limit: u32,
            title: String,
            active: bool,
        }

        let params = Params {
            limit: 50,
            title: "Test".to_string(),
            active: true,
        };

        let query = serialize_to_query(&params).unwrap();
        assert_eq!(query.get("limit"), Some(&"50".to_string()));
        assert_eq!(query.get("title"), Some(&"Test".to_string()));
        assert_eq!(query.get("active"), Some(&"true".to_string()));
    }

    #[test]
    fn test_serialize_to_query_skips_none() {
        #[derive(Serialize)]
        struct Params {
            #[serde(skip_serializing_if = "Option::is_none")]
            limit: Option<u32>,
            #[serde(skip_serializing_if = "Option::is_none")]
            page_info: Option<String>,
        }

        let params = Params {
            limit: Some(50),
            page_info: None,
        };

        let query = serialize_to_query(&params).unwrap();
        assert_eq!(query.get("limit"), Some(&"50".to_string()));
        assert!(!query.contains_key("page_info"));
    }

    #[test]
    fn test_serialize_to_query_handles_arrays() {
        #[derive(Serialize)]
        struct Params {
            ids: Vec<u64>,
        }

        let params = Params { ids: vec![1, 2, 3] };

        let query = serialize_to_query(&params).unwrap();
        assert_eq!(query.get("ids"), Some(&"1,2,3".to_string()));
    }

    #[test]
    fn test_nested_resource_path_selection() {
        // With product_id available, should select nested path for All
        let path = get_path(MockVariant::PATHS, ResourceOperation::All, &["product_id"]);
        assert!(path.is_some());
        assert_eq!(path.unwrap().template, "products/{product_id}/variants");

        // With both product_id and id, should select nested Find path
        let path = get_path(
            MockVariant::PATHS,
            ResourceOperation::Find,
            &["product_id", "id"],
        );
        assert!(path.is_some());
        assert_eq!(
            path.unwrap().template,
            "products/{product_id}/variants/{id}"
        );

        // With only id, should select standalone Find path
        let path = get_path(MockVariant::PATHS, ResourceOperation::Find, &["id"]);
        assert!(path.is_some());
        assert_eq!(path.unwrap().template, "variants/{id}");
    }

    #[test]
    fn test_resource_trait_bounds() {
        fn assert_trait_bounds<T: RestResource>() {}
        assert_trait_bounds::<MockProduct>();
        assert_trait_bounds::<MockVariant>();
    }

    #[test]
    fn test_resource_key_lowercase() {
        assert_eq!(MockProduct::resource_key(), "product");
        assert_eq!(MockVariant::resource_key(), "variant");
    }

    #[test]
    fn test_read_only_resource_marker_trait_compiles() {
        // Test that ReadOnlyResource trait compiles correctly as a marker trait
        fn assert_read_only<T: ReadOnlyResource>() {}
        assert_read_only::<MockLocation>();
    }

    #[test]
    fn test_read_only_resource_trait_bounds_with_rest_resource() {
        // Test that ReadOnlyResource requires RestResource as a supertrait
        fn assert_both_traits<T: ReadOnlyResource + RestResource>() {}
        assert_both_traits::<MockLocation>();
    }

    #[test]
    fn test_read_only_resource_has_only_get_paths() {
        // Verify that read-only resources only have GET operations defined
        let paths = MockLocation::PATHS;

        // Should have Find, All, Count paths
        assert!(get_path(paths, ResourceOperation::Find, &["id"]).is_some());
        assert!(get_path(paths, ResourceOperation::All, &[]).is_some());
        assert!(get_path(paths, ResourceOperation::Count, &[]).is_some());

        // Should NOT have Create, Update, Delete paths
        assert!(get_path(paths, ResourceOperation::Create, &[]).is_none());
        assert!(get_path(paths, ResourceOperation::Update, &["id"]).is_none());
        assert!(get_path(paths, ResourceOperation::Delete, &["id"]).is_none());
    }

    #[test]
    fn test_read_only_resource_implements_rest_resource() {
        // Verify that implementing ReadOnlyResource still allows RestResource methods
        let location = MockLocation {
            id: Some(123),
            name: "Main Warehouse".to_string(),
            active: true,
        };

        assert_eq!(location.get_id(), Some(123));
        assert_eq!(MockLocation::NAME, "Location");
        assert_eq!(MockLocation::PLURAL, "locations");
        assert_eq!(MockLocation::resource_key(), "location");
    }
}