Skip to main content

RestResource

Trait RestResource 

Source
pub trait RestResource:
    Serialize
    + DeserializeOwned
    + Clone
    + Send
    + Sync
    + Sized {
    type Id: Display + Clone + Send + Sync;
    type FindParams: Serialize + Default + Send + Sync;
    type AllParams: Serialize + Default + Send + Sync;
    type CountParams: Serialize + Default + Send + Sync;

    const NAME: &'static str;
    const PLURAL: &'static str;
    const PATHS: &'static [ResourcePath];
    const PREFIX: Option<&'static str> = None;

    // Required method
    fn get_id(&self) -> Option<Self::Id>;

    // Provided methods
    fn resource_key() -> String { ... }
    async fn find(
        client: &RestClient,
        id: Self::Id,
        params: Option<Self::FindParams>,
    ) -> Result<ResourceResponse<Self>, ResourceError> { ... }
    async fn all(
        client: &RestClient,
        params: Option<Self::AllParams>,
    ) -> Result<ResourceResponse<Vec<Self>>, ResourceError> { ... }
    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> { ... }
    async fn save(&self, client: &RestClient) -> Result<Self, ResourceError> { ... }
    async fn save_partial(
        &self,
        client: &RestClient,
        changed_fields: Value,
    ) -> Result<Self, ResourceError> { ... }
    async fn delete(&self, client: &RestClient) -> Result<(), ResourceError> { ... }
    async fn count(
        client: &RestClient,
        params: Option<Self::CountParams>,
    ) -> Result<u64, ResourceError> { ... }
    fn build_full_path(path: &str) -> String { ... }
}
Expand description

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.

Required Associated Constants§

Source

const NAME: &'static str

The singular name of the resource (e.g., “Product”).

Used in error messages and as the response body key for single resources.

Source

const PLURAL: &'static str

The plural name used in URL paths (e.g., “products”).

Used as the response body key for collection operations.

Source

const PATHS: &'static [ResourcePath]

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.

Provided Associated Constants§

Source

const PREFIX: Option<&'static str> = None

Optional path prefix for nested resources.

Override this for resources that always appear under a parent path.

Required Associated Types§

Source

type Id: Display + Clone + Send + Sync

The type of the resource’s identifier.

Source

type FindParams: Serialize + Default + Send + Sync

Parameters for find() operations.

Use () if no parameters are needed.

Source

type AllParams: Serialize + Default + Send + Sync

Parameters for all() operations (filtering, pagination, etc.).

Use () if no parameters are needed.

Source

type CountParams: Serialize + Default + Send + Sync

Parameters for count() operations.

Use () if no parameters are needed.

Required Methods§

Source

fn get_id(&self) -> Option<Self::Id>

Returns the resource’s ID if it exists.

Returns None for new resources that haven’t been saved yet.

Provided Methods§

Source

fn resource_key() -> String

Returns the lowercase key used in JSON request/response bodies.

Source

async fn find( client: &RestClient, id: Self::Id, params: Option<Self::FindParams>, ) -> Result<ResourceResponse<Self>, ResourceError>

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
let product = Product::find(&client, 123, None).await?;
println!("Found: {}", product.title);
Source

async fn all( client: &RestClient, params: Option<Self::AllParams>, ) -> Result<ResourceResponse<Vec<Self>>, ResourceError>

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
let response = Product::all(&client, None).await?;
for product in response.iter() {
    println!("Product: {}", product.title);
}

if response.has_next_page() {
    // Fetch next page...
}
Source

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>

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.

Source

async fn save(&self, client: &RestClient) -> Result<Self, ResourceError>

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
// 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?;
Source

async fn save_partial( &self, client: &RestClient, changed_fields: Value, ) -> Result<Self, ResourceError>

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.

Source

async fn delete(&self, client: &RestClient) -> Result<(), ResourceError>

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
let product = Product::find(&client, 123, None).await?;
product.delete(&client).await?;
Source

async fn count( client: &RestClient, params: Option<Self::CountParams>, ) -> Result<u64, ResourceError>

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
let count = Product::count(&client, None).await?;
println!("Total products: {}", count);
Source

fn build_full_path(path: &str) -> String

Builds the full path including any prefix.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl RestResource for AccessScope

Source§

const NAME: &'static str = "AccessScope"

Source§

const PLURAL: &'static str = "access_scopes"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = String

Source§

type FindParams = ()

Source§

type AllParams = ()

Source§

type CountParams = ()

Source§

impl RestResource for ApplicationCharge

Source§

const NAME: &'static str = "ApplicationCharge"

Source§

const PLURAL: &'static str = "application_charges"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = ApplicationChargeFindParams

Source§

type AllParams = ApplicationChargeListParams

Source§

type CountParams = ()

Source§

impl RestResource for Article

Source§

impl RestResource for Blog

Source§

impl RestResource for Collect

Source§

impl RestResource for Comment

Source§

impl RestResource for Country

Source§

impl RestResource for Currency

Source§

const NAME: &'static str = "Currency"

Source§

const PLURAL: &'static str = "currencies"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = String

Source§

type FindParams = ()

Source§

type AllParams = ()

Source§

type CountParams = ()

Source§

impl RestResource for CustomCollection

Source§

impl RestResource for Customer

Source§

impl RestResource for DiscountCode

Source§

impl RestResource for DraftOrder

Source§

impl RestResource for Event

Source§

impl RestResource for Fulfillment

Source§

impl RestResource for FulfillmentOrder

Source§

impl RestResource for FulfillmentService

Source§

const NAME: &'static str = "FulfillmentService"

Source§

const PLURAL: &'static str = "fulfillment_services"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = FulfillmentServiceFindParams

Source§

type AllParams = FulfillmentServiceListParams

Source§

type CountParams = ()

Source§

impl RestResource for GiftCard

Source§

impl RestResource for InventoryItem

Source§

const NAME: &'static str = "InventoryItem"

Source§

const PLURAL: &'static str = "inventory_items"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = InventoryItemFindParams

Source§

type AllParams = InventoryItemListParams

Source§

type CountParams = ()

Source§

impl RestResource for InventoryLevel

Source§

const NAME: &'static str = "InventoryLevel"

Source§

const PLURAL: &'static str = "inventory_levels"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = String

Source§

type FindParams = ()

Source§

type AllParams = InventoryLevelListParams

Source§

type CountParams = ()

Source§

impl RestResource for Location

Source§

impl RestResource for Metafield

Source§

impl RestResource for Order

Source§

impl RestResource for Page

Source§

impl RestResource for Policy

Source§

const NAME: &'static str = "Policy"

Source§

const PLURAL: &'static str = "policies"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = String

Source§

type FindParams = ()

Source§

type AllParams = ()

Source§

type CountParams = ()

Source§

impl RestResource for PriceRule

Source§

impl RestResource for Product

Source§

impl RestResource for ProductImageResource

Source§

impl RestResource for Province

Source§

impl RestResource for RecurringApplicationCharge

Source§

const NAME: &'static str = "RecurringApplicationCharge"

Source§

const PLURAL: &'static str = "recurring_application_charges"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = RecurringApplicationChargeFindParams

Source§

type AllParams = RecurringApplicationChargeListParams

Source§

type CountParams = ()

Source§

impl RestResource for Redirect

Source§

impl RestResource for RefundResource

Source§

impl RestResource for ScriptTag

Source§

impl RestResource for Shop

Source§

const NAME: &'static str = "Shop"

Source§

const PLURAL: &'static str = "shop"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = ()

Source§

type AllParams = ()

Source§

type CountParams = ()

Source§

impl RestResource for SmartCollection

Source§

impl RestResource for StorefrontAccessToken

Source§

const NAME: &'static str = "StorefrontAccessToken"

Source§

const PLURAL: &'static str = "storefront_access_tokens"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = ()

Source§

type AllParams = ()

Source§

type CountParams = ()

Source§

impl RestResource for Theme

Source§

const NAME: &'static str = "Theme"

Source§

const PLURAL: &'static str = "themes"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = ThemeFindParams

Source§

type AllParams = ThemeListParams

Source§

type CountParams = ()

Source§

impl RestResource for Transaction

Source§

impl RestResource for UsageCharge

Source§

const NAME: &'static str = "UsageCharge"

Source§

const PLURAL: &'static str = "usage_charges"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = UsageChargeFindParams

Source§

type AllParams = UsageChargeListParams

Source§

type CountParams = ()

Source§

impl RestResource for User

Source§

const NAME: &'static str = "User"

Source§

const PLURAL: &'static str = "users"

Source§

const PATHS: &'static [ResourcePath]

Source§

type Id = u64

Source§

type FindParams = UserFindParams

Source§

type AllParams = UserListParams

Source§

type CountParams = ()

Source§

impl RestResource for Variant

Source§

impl RestResource for Webhook