rocie_client/apis/
api_set_auth_product_api.rs1use super::{ContentType, Error, configuration};
22use crate::{apis::ResponseContent, models};
23use reqwest;
24use serde::{Deserialize, Serialize, de::Error as _};
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28#[serde(untagged)]
29pub enum AssociateBarcodeError {
30 Status400(String),
31 Status401(),
32 Status404(),
33 Status500(String),
34 UnknownValue(serde_json::Value),
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(untagged)]
40pub enum RegisterProductError {
41 Status401(),
42 Status500(String),
43 UnknownValue(serde_json::Value),
44}
45
46pub async fn associate_barcode(
47 configuration: &configuration::Configuration,
48 id: models::ProductId,
49 barcode: models::Barcode,
50) -> Result<(), Error<AssociateBarcodeError>> {
51 let p_path_id = id;
53 let p_body_barcode = barcode;
54
55 let uri_str = format!(
56 "{}/product/{id}/associate",
57 configuration.base_path,
58 id = p_path_id.to_string()
59 );
60 let mut req_builder = configuration
61 .client
62 .request(reqwest::Method::POST, &uri_str);
63
64 if let Some(ref user_agent) = configuration.user_agent {
65 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
66 }
67 req_builder = req_builder.json(&p_body_barcode);
68
69 let req = req_builder.build()?;
70 let resp = configuration.client.execute(req).await?;
71
72 let status = resp.status();
73
74 if !status.is_client_error() && !status.is_server_error() {
75 Ok(())
76 } else {
77 let content = resp.text().await?;
78 let entity: Option<AssociateBarcodeError> = serde_json::from_str(&content).ok();
79 Err(Error::ResponseError(ResponseContent {
80 status,
81 content,
82 entity,
83 }))
84 }
85}
86
87pub async fn register_product(
88 configuration: &configuration::Configuration,
89 product_stub: models::ProductStub,
90) -> Result<models::ProductId, Error<RegisterProductError>> {
91 let p_body_product_stub = product_stub;
93
94 let uri_str = format!("{}/product/new", configuration.base_path);
95 let mut req_builder = configuration
96 .client
97 .request(reqwest::Method::POST, &uri_str);
98
99 if let Some(ref user_agent) = configuration.user_agent {
100 req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
101 }
102 req_builder = req_builder.json(&p_body_product_stub);
103
104 let req = req_builder.build()?;
105 let resp = configuration.client.execute(req).await?;
106
107 let status = resp.status();
108 let content_type = resp
109 .headers()
110 .get("content-type")
111 .and_then(|v| v.to_str().ok())
112 .unwrap_or("application/octet-stream");
113 let content_type = super::ContentType::from(content_type);
114
115 if !status.is_client_error() && !status.is_server_error() {
116 let content = resp.text().await?;
117 match content_type {
118 ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
119 ContentType::Text => {
120 return Err(Error::from(serde_json::Error::custom(
121 "Received `text/plain` content type response that cannot be converted to `models::ProductId`",
122 )));
123 }
124 ContentType::Unsupported(unknown_type) => {
125 return Err(Error::from(serde_json::Error::custom(format!(
126 "Received `{unknown_type}` content type response that cannot be converted to `models::ProductId`"
127 ))));
128 }
129 }
130 } else {
131 let content = resp.text().await?;
132 let entity: Option<RegisterProductError> = serde_json::from_str(&content).ok();
133 Err(Error::ResponseError(ResponseContent {
134 status,
135 content,
136 entity,
137 }))
138 }
139}