Skip to main content

autogen_squareup/apis/
snippets_api.rs

1/*
2 * Square
3 *
4 * Use Square APIs to manage and run business including payment, customer, product, inventory, and employee management.
5 *
6 * The version of the OpenAPI document: 2.0
7 * Contact: developers@squareup.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`delete_snippet`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum DeleteSnippetError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`retrieve_snippet`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum RetrieveSnippetError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`upsert_snippet`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum UpsertSnippetError {
36    UnknownValue(serde_json::Value),
37}
38
39
40/// Removes your snippet from a Square Online site.  You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.   __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).
41pub async fn delete_snippet(configuration: &configuration::Configuration, site_id: &str) -> Result<models::DeleteSnippetResponse, Error<DeleteSnippetError>> {
42    // add a prefix to parameters to efficiently prevent name collisions
43    let p_site_id = site_id;
44
45    let uri_str = format!("{}/v2/sites/{site_id}/snippet", configuration.base_path, site_id=crate::apis::urlencode(p_site_id));
46    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
47
48    if let Some(ref user_agent) = configuration.user_agent {
49        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
50    }
51    if let Some(ref token) = configuration.oauth_access_token {
52        req_builder = req_builder.bearer_auth(token.to_owned());
53    };
54
55    let req = req_builder.build()?;
56    let resp = configuration.client.execute(req).await?;
57
58    let status = resp.status();
59    let content_type = resp
60        .headers()
61        .get("content-type")
62        .and_then(|v| v.to_str().ok())
63        .unwrap_or("application/octet-stream");
64    let content_type = super::ContentType::from(content_type);
65
66    if !status.is_client_error() && !status.is_server_error() {
67        let content = resp.text().await?;
68        match content_type {
69            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
70            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeleteSnippetResponse`"))),
71            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::DeleteSnippetResponse`")))),
72        }
73    } else {
74        let content = resp.text().await?;
75        let entity: Option<DeleteSnippetError> = serde_json::from_str(&content).ok();
76        Err(Error::ResponseError(ResponseContent { status, content, entity }))
77    }
78}
79
80/// Retrieves your snippet from a Square Online site. A site can contain snippets from multiple snippet applications, but you can retrieve only the snippet that was added by your application.  You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.   __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).
81pub async fn retrieve_snippet(configuration: &configuration::Configuration, site_id: &str) -> Result<models::RetrieveSnippetResponse, Error<RetrieveSnippetError>> {
82    // add a prefix to parameters to efficiently prevent name collisions
83    let p_site_id = site_id;
84
85    let uri_str = format!("{}/v2/sites/{site_id}/snippet", configuration.base_path, site_id=crate::apis::urlencode(p_site_id));
86    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
87
88    if let Some(ref user_agent) = configuration.user_agent {
89        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
90    }
91    if let Some(ref token) = configuration.oauth_access_token {
92        req_builder = req_builder.bearer_auth(token.to_owned());
93    };
94
95    let req = req_builder.build()?;
96    let resp = configuration.client.execute(req).await?;
97
98    let status = resp.status();
99    let content_type = resp
100        .headers()
101        .get("content-type")
102        .and_then(|v| v.to_str().ok())
103        .unwrap_or("application/octet-stream");
104    let content_type = super::ContentType::from(content_type);
105
106    if !status.is_client_error() && !status.is_server_error() {
107        let content = resp.text().await?;
108        match content_type {
109            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
110            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RetrieveSnippetResponse`"))),
111            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::RetrieveSnippetResponse`")))),
112        }
113    } else {
114        let content = resp.text().await?;
115        let entity: Option<RetrieveSnippetError> = serde_json::from_str(&content).ok();
116        Err(Error::ResponseError(ResponseContent { status, content, entity }))
117    }
118}
119
120/// Adds a snippet to a Square Online site or updates the existing snippet on the site.  The snippet code is appended to the end of the `head` element on every page of the site, except checkout pages. A snippet application can add one snippet to a given site.   You can call [ListSites](api-endpoint:Sites-ListSites) to get the IDs of the sites that belong to a seller.   __Note:__ Square Online APIs are publicly available as part of an early access program. For more information, see [Early access program for Square Online APIs](https://developer.squareup.com/docs/online-api#early-access-program-for-square-online-apis).
121pub async fn upsert_snippet(configuration: &configuration::Configuration, site_id: &str, upsert_snippet_request: models::UpsertSnippetRequest) -> Result<models::UpsertSnippetResponse, Error<UpsertSnippetError>> {
122    // add a prefix to parameters to efficiently prevent name collisions
123    let p_site_id = site_id;
124    let p_upsert_snippet_request = upsert_snippet_request;
125
126    let uri_str = format!("{}/v2/sites/{site_id}/snippet", configuration.base_path, site_id=crate::apis::urlencode(p_site_id));
127    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
128
129    if let Some(ref user_agent) = configuration.user_agent {
130        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
131    }
132    if let Some(ref token) = configuration.oauth_access_token {
133        req_builder = req_builder.bearer_auth(token.to_owned());
134    };
135    req_builder = req_builder.json(&p_upsert_snippet_request);
136
137    let req = req_builder.build()?;
138    let resp = configuration.client.execute(req).await?;
139
140    let status = resp.status();
141    let content_type = resp
142        .headers()
143        .get("content-type")
144        .and_then(|v| v.to_str().ok())
145        .unwrap_or("application/octet-stream");
146    let content_type = super::ContentType::from(content_type);
147
148    if !status.is_client_error() && !status.is_server_error() {
149        let content = resp.text().await?;
150        match content_type {
151            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
152            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::UpsertSnippetResponse`"))),
153            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::UpsertSnippetResponse`")))),
154        }
155    } else {
156        let content = resp.text().await?;
157        let entity: Option<UpsertSnippetError> = serde_json::from_str(&content).ok();
158        Err(Error::ResponseError(ResponseContent { status, content, entity }))
159    }
160}
161