shopify_sdk/rest/resources/v2025_10/
access_scope.rs1use serde::{Deserialize, Serialize};
30
31use crate::clients::RestClient;
32use crate::rest::{ReadOnlyResource, ResourceError, ResourceResponse, RestResource, ResourceOperation, ResourcePath};
33use crate::HttpMethod;
34
35#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
54pub struct AccessScope {
55 #[serde(skip_serializing)]
57 pub handle: Option<String>,
58}
59
60impl AccessScope {
61 pub async fn all(client: &RestClient) -> Result<ResourceResponse<Vec<Self>>, ResourceError> {
74 let url = "oauth/access_scopes";
76 let response = client.get(url, None).await?;
77
78 if !response.is_ok() {
79 return Err(ResourceError::from_http_response(
80 response.code,
81 &response.body,
82 Self::NAME,
83 None,
84 response.request_id(),
85 ));
86 }
87
88 let key = Self::PLURAL;
89 ResourceResponse::from_http_response(response, key)
90 }
91}
92
93impl RestResource for AccessScope {
94 type Id = String;
95 type FindParams = ();
96 type AllParams = ();
97 type CountParams = ();
98
99 const NAME: &'static str = "AccessScope";
100 const PLURAL: &'static str = "access_scopes";
101
102 const PATHS: &'static [ResourcePath] = &[
107 ResourcePath::new(
109 HttpMethod::Get,
110 ResourceOperation::All,
111 &[],
112 "oauth/access_scopes",
113 ),
114 ];
116
117 fn get_id(&self) -> Option<Self::Id> {
118 self.handle.clone()
119 }
120}
121
122impl ReadOnlyResource for AccessScope {}
123
124#[cfg(test)]
125mod tests {
126 use super::*;
127 use crate::rest::{get_path, ReadOnlyResource, ResourceOperation, RestResource};
128
129 #[test]
130 fn test_access_scope_implements_read_only_resource() {
131 fn assert_read_only<T: ReadOnlyResource>() {}
132 assert_read_only::<AccessScope>();
133 }
134
135 #[test]
136 fn test_access_scope_deserialization() {
137 let json = r#"{
138 "handle": "read_products"
139 }"#;
140
141 let scope: AccessScope = serde_json::from_str(json).unwrap();
142
143 assert_eq!(scope.handle, Some("read_products".to_string()));
144 }
145
146 #[test]
147 fn test_access_scope_list_deserialization() {
148 let json = r#"[
149 {"handle": "read_products"},
150 {"handle": "write_products"},
151 {"handle": "read_orders"}
152 ]"#;
153
154 let scopes: Vec<AccessScope> = serde_json::from_str(json).unwrap();
155
156 assert_eq!(scopes.len(), 3);
157 assert_eq!(scopes[0].handle, Some("read_products".to_string()));
158 assert_eq!(scopes[1].handle, Some("write_products".to_string()));
159 assert_eq!(scopes[2].handle, Some("read_orders".to_string()));
160 }
161
162 #[test]
163 fn test_access_scope_special_oauth_path() {
164 let all_path = get_path(AccessScope::PATHS, ResourceOperation::All, &[]);
166 assert!(all_path.is_some());
167 assert_eq!(all_path.unwrap().template, "oauth/access_scopes");
168 }
169
170 #[test]
171 fn test_access_scope_list_only_no_other_operations() {
172 let find_path = get_path(AccessScope::PATHS, ResourceOperation::Find, &["id"]);
174 assert!(find_path.is_none());
175
176 let count_path = get_path(AccessScope::PATHS, ResourceOperation::Count, &[]);
178 assert!(count_path.is_none());
179
180 let create_path = get_path(AccessScope::PATHS, ResourceOperation::Create, &[]);
182 assert!(create_path.is_none());
183
184 let update_path = get_path(AccessScope::PATHS, ResourceOperation::Update, &["id"]);
186 assert!(update_path.is_none());
187
188 let delete_path = get_path(AccessScope::PATHS, ResourceOperation::Delete, &["id"]);
190 assert!(delete_path.is_none());
191 }
192
193 #[test]
194 fn test_access_scope_constants() {
195 assert_eq!(AccessScope::NAME, "AccessScope");
196 assert_eq!(AccessScope::PLURAL, "access_scopes");
197 }
198
199 #[test]
200 fn test_access_scope_get_id_returns_handle() {
201 let scope_with_handle = AccessScope {
202 handle: Some("read_products".to_string()),
203 };
204 assert_eq!(scope_with_handle.get_id(), Some("read_products".to_string()));
205
206 let scope_without_handle = AccessScope::default();
207 assert_eq!(scope_without_handle.get_id(), None);
208 }
209
210 #[test]
211 fn test_access_scope_all_fields_are_read_only() {
212 let scope = AccessScope {
214 handle: Some("write_orders".to_string()),
215 };
216
217 let json = serde_json::to_value(&scope).unwrap();
218 assert_eq!(json, serde_json::json!({}));
220 }
221}