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