oxyde_cloud_client/
lib.rs1mod errors;
2
3use headers_core::Header;
4use log::error;
5use reqwest::header::{HeaderName, HeaderValue};
6use reqwest::multipart::{Form, Part};
7use reqwest::Body;
8use serde::{Deserialize, Serialize};
9use std::path::Path;
10use tokio::io::{AsyncReadExt, BufReader};
11use tokio_util::codec::{BytesCodec, FramedRead};
12
13pub use errors::*;
14use oxyde_cloud_common::config::CloudConfig;
15use oxyde_cloud_common::net::{
16 AppMeta, CheckAvailabilityResponse, LogRequest, LogResponse, LoginResponse, NewAppRequest,
17 NewTeamRequest, SetTeamNameRequest, SuccessResponse, Team,
18};
19
20const BASE_URL: Option<&str> = option_env!("OXYDE_CLOUD_API_URL");
21const DEFAULT_BASE_URL: &str = "https://oxyde.cloud/api/v1/";
22const UPLOAD_CHUNK_SIZE: usize = 99 * 1024 * 1024;
23
24#[derive(Clone)]
25pub struct Client {
26 client: reqwest::Client,
27 api_key: String,
28}
29
30impl Client {
31 pub fn new(api_key: String) -> Self {
32 Self {
33 client: reqwest::Client::new(),
34 api_key,
35 }
36 }
37
38 pub async fn teams(self) -> Result<Vec<Team>, ReqwestJsonError> {
39 let teams = self.get("teams").send().await?;
40
41 Ok(teams)
42 }
43
44 pub async fn new_app(self, app_slug: &str, team_slug: &str) -> Result<bool, ReqwestJsonError> {
45 let CheckAvailabilityResponse { available } = self
46 .post("apps/new")
47 .json(&NewAppRequest {
48 app_slug: app_slug.to_string(),
49 team_slug: team_slug.to_string(),
50 })?
51 .send()
52 .await?;
53
54 Ok(available)
55 }
56
57 pub async fn new_team(self, team_slug: &str) -> Result<bool, ReqwestJsonError> {
58 let CheckAvailabilityResponse { available } = self
59 .post("teams/new")
60 .json(&NewTeamRequest {
61 team_slug: team_slug.to_string(),
62 })?
63 .send()
64 .await?;
65
66 Ok(available)
67 }
68
69 pub async fn set_team_name(
70 self,
71 team_slug: &str,
72 team_name: &str,
73 ) -> Result<(), ReqwestJsonError> {
74 let _: SuccessResponse = self
75 .post("teams/name")
76 .json(&SetTeamNameRequest {
77 team_slug: team_slug.to_string(),
78 team_name: team_name.to_string(),
79 })?
80 .send()
81 .await?;
82
83 Ok(())
84 }
85
86 pub async fn login(self) -> Result<LoginResponse, ReqwestJsonError> {
87 Ok(self.post("login").json(())?.send().await?)
88 }
89
90 pub async fn upload_file(
91 self,
92 app_slug: impl AsRef<str>,
93 path: impl AsRef<Path>,
94 ) -> Result<(), UploadFileError> {
95 let file = tokio::fs::File::open(path.as_ref()).await?;
96 let metadata = file.metadata().await?;
97 let total_size = metadata.len() as usize;
98 let total_chunks = (total_size + UPLOAD_CHUNK_SIZE - 1) / UPLOAD_CHUNK_SIZE;
99
100 let mut reader = BufReader::new(file);
101 let mut buffer = vec![0u8; UPLOAD_CHUNK_SIZE];
102 let mut chunk_number = 0;
103
104 loop {
105 let n = reader.read(&mut buffer).await?;
106 if n == 0 {
107 break;
108 }
109
110 let part = reqwest::multipart::Part::bytes(buffer[..n].to_vec())
111 .file_name(path.as_ref().to_string_lossy().to_string());
112
113 let form = reqwest::multipart::Form::new()
114 .part("file", part)
115 .text("chunk_number", chunk_number.to_string())
116 .text("total_chunks", total_chunks.to_string());
117
118 let _: SuccessResponse = self
119 .clone()
120 .post("apps/upload-file")
121 .header(
122 AppMeta::name(),
123 AppMeta {
124 app_slug: app_slug.as_ref().to_string(),
125 }
126 .to_string_value(),
127 )
128 .send()
129 .await?;
130
131 chunk_number += 1;
132 }
133
134 Ok(())
135 }
136
137 pub async fn upload_done(self, config: &CloudConfig) -> Result<(), ReqwestJsonError> {
138 let _: SuccessResponse = self.post("apps/upload-done").json(config)?.send().await?;
139
140 Ok(())
141 }
142
143 pub async fn log(self, name: &str) -> Result<String, ReqwestJsonError> {
144 let res: LogResponse = self
145 .post("log")
146 .json(&LogRequest {
147 name: name.to_string(),
148 })?
149 .send()
150 .await?;
151
152 Ok(res.log)
153 }
154
155 pub fn post(self, route: &str) -> ClientBuilder {
156 let url = Self::build_route(route);
157
158 ClientBuilder(self.client.post(url)).auth_header(&self.api_key)
159 }
160
161 pub fn get(self, route: &str) -> ClientBuilder {
162 let url = Self::build_route(route);
163
164 ClientBuilder(self.client.get(url)).auth_header(&self.api_key)
165 }
166
167 fn build_route(route: &str) -> String {
168 let base_url = std::env::var("OXYDE_CLOUD_API_URL")
169 .unwrap_or(BASE_URL.unwrap_or(DEFAULT_BASE_URL).to_string());
170 format!("{base_url}{route}")
171 }
172}
173
174pub struct ClientBuilder(reqwest::RequestBuilder);
175
176impl ClientBuilder {
177 pub fn auth_header(self, api_key: &str) -> Self {
178 Self(
179 self.0
180 .header("Authorization", format!("Bearer {}", api_key)),
181 )
182 }
183
184 pub fn body<T: Into<reqwest::Body>>(self, body: T) -> Self {
185 Self(self.0.body(body))
186 }
187
188 pub fn multipart(self, form: Form) -> Self {
189 Self(self.0.multipart(form))
190 }
191
192 pub fn json<Body: Serialize>(self, json: Body) -> Result<ClientBuilder, serde_json::Error> {
193 let json = serde_json::to_string(&json)?;
194
195 Ok(Self(
196 self.0.header("Content-Type", "application/json").body(json),
197 ))
198 }
199
200 pub fn header<K, V>(self, key: K, value: V) -> Self
201 where
202 HeaderName: TryFrom<K>,
203 <HeaderName as TryFrom<K>>::Error: Into<http::Error>,
204 HeaderValue: TryFrom<V>,
205 <HeaderValue as TryFrom<V>>::Error: Into<http::Error>,
206 {
207 Self(self.0.header(key, value))
208 }
209
210 pub async fn send<Resp>(self) -> Result<Resp, reqwest::Error>
211 where
212 for<'de> Resp: Deserialize<'de>,
213 {
214 let res = self.0.send().await?;
215
216 match res.error_for_status_ref() {
217 Ok(_) => Ok(res.json::<Resp>().await?),
218 Err(err) => {
219 let err_text = res.text().await?;
220 error!("Received error:\n{err_text:#?}");
221
222 Err(err)
223 }
224 }
225 }
226}