cloud: use response_ok helper function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Quaternions 2024-12-13 19:35:31 -08:00
parent 3cd65158a0
commit e45f3c2cf9
3 changed files with 74 additions and 59 deletions

View File

@ -1,3 +1,5 @@
use crate::ResponseError;
#[derive(Debug,serde::Deserialize,serde::Serialize)] #[derive(Debug,serde::Deserialize,serde::Serialize)]
#[allow(nonstandard_style,dead_code)] #[allow(nonstandard_style,dead_code)]
pub enum AssetType{ pub enum AssetType{
@ -40,6 +42,7 @@ impl AssetOperation{
#[derive(Debug)] #[derive(Debug)]
pub enum CreateError{ pub enum CreateError{
Parse(url::ParseError), Parse(url::ParseError),
Response(ResponseError),
Serialize(serde_json::Error), Serialize(serde_json::Error),
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
} }
@ -102,6 +105,7 @@ pub struct UpdatePlaceResponse{
#[derive(Debug)] #[derive(Debug)]
pub enum UpdateError{ pub enum UpdateError{
ParseError(url::ParseError), ParseError(url::ParseError),
Response(ResponseError),
SerializeError(serde_json::Error), SerializeError(serde_json::Error),
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
} }
@ -167,6 +171,7 @@ pub struct GetAssetRequest{
#[derive(Debug)] #[derive(Debug)]
pub enum GetError{ pub enum GetError{
ParseError(url::ParseError), ParseError(url::ParseError),
Response(ResponseError),
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
IO(std::io::Error) IO(std::io::Error)
} }
@ -203,6 +208,7 @@ pub struct AssetVersionsResponse{
#[derive(Debug)] #[derive(Debug)]
pub enum AssetVersionsError{ pub enum AssetVersionsError{
ParseError(url::ParseError), ParseError(url::ParseError),
Response(ResponseError),
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
} }
impl std::fmt::Display for AssetVersionsError{ impl std::fmt::Display for AssetVersionsError{
@ -238,6 +244,7 @@ pub struct InventoryPageResponse{
#[derive(Debug)] #[derive(Debug)]
pub enum InventoryPageError{ pub enum InventoryPageError{
ParseError(url::ParseError), ParseError(url::ParseError),
Response(ResponseError),
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
} }
impl std::fmt::Display for InventoryPageError{ impl std::fmt::Display for InventoryPageError{
@ -368,9 +375,9 @@ impl CloudContext{
.text("request",request_config) .text("request",request_config)
.part("fileContent",part); .part("fileContent",part);
let operation=self.post_form(url,form).await let operation=crate::response_ok(
.map_err(CreateError::Reqwest)? self.post_form(url,form).await.map_err(CreateError::Reqwest)?
.error_for_status().map_err(CreateError::Reqwest)? ).await.map_err(CreateError::Response)?
.json::<RobloxOperation>().await.map_err(CreateError::Reqwest)?; .json::<RobloxOperation>().await.map_err(CreateError::Reqwest)?;
Ok(AssetOperation{ Ok(AssetOperation{
@ -387,10 +394,9 @@ impl CloudContext{
.text("request",request_config) .text("request",request_config)
.part("fileContent",reqwest::multipart::Part::bytes(body)); .part("fileContent",reqwest::multipart::Part::bytes(body));
let operation=self.patch_form(url,form).await let operation=crate::response_ok(
.map_err(UpdateError::Reqwest)? self.patch_form(url,form).await.map_err(UpdateError::Reqwest)?
//roblox api documentation is very poor, just give the status code and drop the json ).await.map_err(UpdateError::Response)?
.error_for_status().map_err(UpdateError::Reqwest)?
.json::<RobloxOperation>().await.map_err(UpdateError::Reqwest)?; .json::<RobloxOperation>().await.map_err(UpdateError::Reqwest)?;
Ok(AssetOperation{ Ok(AssetOperation{
@ -401,24 +407,27 @@ impl CloudContext{
let raw_url=format!("https://apis.roblox.com/assets/v1/operations/{}",config.operation_id); let raw_url=format!("https://apis.roblox.com/assets/v1/operations/{}",config.operation_id);
let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?; let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?;
crate::response_ok(
self.get(url).await.map_err(GetError::Reqwest)? self.get(url).await.map_err(GetError::Reqwest)?
.error_for_status().map_err(GetError::Reqwest)? ).await.map_err(GetError::Response)?
.json::<RobloxOperation>().await.map_err(GetError::Reqwest) .json::<RobloxOperation>().await.map_err(GetError::Reqwest)
} }
pub async fn get_asset_info(&self,config:GetAssetInfoRequest)->Result<AssetResponse,GetError>{ pub async fn get_asset_info(&self,config:GetAssetInfoRequest)->Result<AssetResponse,GetError>{
let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}",config.asset_id); let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}",config.asset_id);
let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?; let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?;
crate::response_ok(
self.get(url).await.map_err(GetError::Reqwest)? self.get(url).await.map_err(GetError::Reqwest)?
.error_for_status().map_err(GetError::Reqwest)? ).await.map_err(GetError::Response)?
.json::<AssetResponse>().await.map_err(GetError::Reqwest) .json::<AssetResponse>().await.map_err(GetError::Reqwest)
} }
pub async fn get_asset_version(&self,config:GetAssetVersionRequest)->Result<Vec<u8>,GetError>{ pub async fn get_asset_version(&self,config:GetAssetVersionRequest)->Result<Vec<u8>,GetError>{
let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}/versions/{}",config.asset_id,config.version); let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}/versions/{}",config.asset_id,config.version);
let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?; let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?;
let body=self.get(url).await.map_err(GetError::Reqwest)? let body=crate::response_ok(
.error_for_status().map_err(GetError::Reqwest)? self.get(url).await.map_err(GetError::Reqwest)?
).await.map_err(GetError::Response)?
.bytes().await.map_err(GetError::Reqwest)?; .bytes().await.map_err(GetError::Reqwest)?;
match maybe_gzip_decode(&mut std::io::Cursor::new(body)){ match maybe_gzip_decode(&mut std::io::Cursor::new(body)){
@ -441,8 +450,9 @@ impl CloudContext{
let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}/versions",config.asset_id); let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}/versions",config.asset_id);
let url=reqwest::Url::parse(raw_url.as_str()).map_err(AssetVersionsError::ParseError)?; let url=reqwest::Url::parse(raw_url.as_str()).map_err(AssetVersionsError::ParseError)?;
crate::response_ok(
self.get(url).await.map_err(AssetVersionsError::Reqwest)? self.get(url).await.map_err(AssetVersionsError::Reqwest)?
.error_for_status().map_err(AssetVersionsError::Reqwest)? ).await.map_err(AssetVersionsError::Response)?
.json::<AssetVersionsResponse>().await.map_err(AssetVersionsError::Reqwest) .json::<AssetVersionsResponse>().await.map_err(AssetVersionsError::Reqwest)
} }
pub async fn inventory_page(&self,config:InventoryPageRequest)->Result<InventoryPageResponse,InventoryPageError>{ pub async fn inventory_page(&self,config:InventoryPageRequest)->Result<InventoryPageResponse,InventoryPageError>{
@ -455,8 +465,9 @@ impl CloudContext{
} }
} }
crate::response_ok(
self.get(url).await.map_err(InventoryPageError::Reqwest)? self.get(url).await.map_err(InventoryPageError::Reqwest)?
.error_for_status().map_err(InventoryPageError::Reqwest)? ).await.map_err(InventoryPageError::Response)?
.json::<InventoryPageResponse>().await.map_err(InventoryPageError::Reqwest) .json::<InventoryPageResponse>().await.map_err(InventoryPageError::Reqwest)
} }
pub async fn update_place(&self,config:UpdatePlaceRequest,body:impl Into<reqwest::Body>+Clone)->Result<UpdatePlaceResponse,UpdateError>{ pub async fn update_place(&self,config:UpdatePlaceRequest,body:impl Into<reqwest::Body>+Clone)->Result<UpdatePlaceResponse,UpdateError>{
@ -468,8 +479,9 @@ impl CloudContext{
query.append_pair("versionType","Published"); query.append_pair("versionType","Published");
} }
crate::response_ok(
self.post(url,body).await.map_err(UpdateError::Reqwest)? self.post(url,body).await.map_err(UpdateError::Reqwest)?
.error_for_status().map_err(UpdateError::Reqwest)? ).await.map_err(UpdateError::Response)?
.json::<UpdatePlaceResponse>().await.map_err(UpdateError::Reqwest) .json::<UpdatePlaceResponse>().await.map_err(UpdateError::Reqwest)
} }
} }

View File

@ -1,3 +1,5 @@
use crate::ResponseError;
#[derive(Debug)] #[derive(Debug)]
pub enum PostError{ pub enum PostError{
Reqwest(reqwest::Error), Reqwest(reqwest::Error),
@ -199,40 +201,6 @@ fn read_readable(mut readable:impl std::io::Read)->std::io::Result<Vec<u8>>{
readable.read_to_end(&mut contents)?; readable.read_to_end(&mut contents)?;
Ok(contents) Ok(contents)
} }
#[allow(dead_code)]
#[derive(Debug)]
pub struct StatusCodeWithUrlAndBody{
pub status_code:reqwest::StatusCode,
pub url:url::Url,
pub body:String,
}
#[derive(Debug)]
pub enum ResponseError{
Reqwest(reqwest::Error),
StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody),
}
impl std::fmt::Display for ResponseError{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"{self:?}")
}
}
impl std::error::Error for ResponseError{}
// lazy function to draw out meaningful info from http response on failure
async fn response_ok(response:reqwest::Response)->Result<reqwest::Response,ResponseError>{
let status_code=response.status();
if status_code.is_success(){
Ok(response)
}else{
let url=response.url().to_owned();
let bytes=response.bytes().await.map_err(ResponseError::Reqwest)?;
let body=String::from_utf8_lossy(&bytes).to_string();
Err(ResponseError::StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody{
status_code,
url,
body,
}))
}
}
#[derive(Clone)] #[derive(Clone)]
pub struct Cookie(String); pub struct Cookie(String);
@ -300,7 +268,7 @@ impl CookieContext{
} }
} }
response_ok( crate::response_ok(
self.post(url,body).await.map_err(CreateError::PostError)? self.post(url,body).await.map_err(CreateError::PostError)?
).await.map_err(CreateError::Response)? ).await.map_err(CreateError::Response)?
.json::<UploadResponse>().await.map_err(CreateError::Reqwest) .json::<UploadResponse>().await.map_err(CreateError::Reqwest)
@ -332,7 +300,7 @@ impl CookieContext{
} }
} }
response_ok( crate::response_ok(
self.post(url,body).await.map_err(UploadError::PostError)? self.post(url,body).await.map_err(UploadError::PostError)?
).await.map_err(UploadError::Response)? ).await.map_err(UploadError::Response)?
.json::<UploadResponse>().await.map_err(UploadError::Reqwest) .json::<UploadResponse>().await.map_err(UploadError::Reqwest)
@ -347,7 +315,7 @@ impl CookieContext{
query.append_pair("version",version.to_string().as_str()); query.append_pair("version",version.to_string().as_str());
} }
} }
let body=response_ok( let body=crate::response_ok(
self.get(url).await.map_err(GetError::Reqwest)? self.get(url).await.map_err(GetError::Reqwest)?
).await.map_err(GetError::Response)? ).await.map_err(GetError::Response)?
.bytes().await.map_err(GetError::Reqwest)?; .bytes().await.map_err(GetError::Reqwest)?;
@ -370,7 +338,7 @@ impl CookieContext{
query.append_pair("cursor",cursor); query.append_pair("cursor",cursor);
} }
} }
response_ok( crate::response_ok(
self.get(url).await.map_err(PageError::Reqwest)? self.get(url).await.map_err(PageError::Reqwest)?
).await.map_err(PageError::Response)? ).await.map_err(PageError::Response)?
.json::<AssetVersionsPageResponse>().await.map_err(PageError::Reqwest) .json::<AssetVersionsPageResponse>().await.map_err(PageError::Reqwest)
@ -385,7 +353,7 @@ impl CookieContext{
query.append_pair("cursor",cursor); query.append_pair("cursor",cursor);
} }
} }
response_ok( crate::response_ok(
self.get(url).await.map_err(PageError::Reqwest)? self.get(url).await.map_err(PageError::Reqwest)?
).await.map_err(PageError::Response)? ).await.map_err(PageError::Response)?
.json::<CreationsPageResponse>().await.map_err(PageError::Reqwest) .json::<CreationsPageResponse>().await.map_err(PageError::Reqwest)
@ -399,7 +367,7 @@ impl CookieContext{
query.append_pair("cursor",cursor); query.append_pair("cursor",cursor);
} }
} }
response_ok( crate::response_ok(
self.get(url).await.map_err(PageError::Reqwest)? self.get(url).await.map_err(PageError::Reqwest)?
).await.map_err(PageError::Response)? ).await.map_err(PageError::Response)?
.json::<UserInventoryPageResponse>().await.map_err(PageError::Reqwest) .json::<UserInventoryPageResponse>().await.map_err(PageError::Reqwest)

View File

@ -1,2 +1,37 @@
pub mod cloud; pub mod cloud;
pub mod cookie; pub mod cookie;
#[allow(dead_code)]
#[derive(Debug)]
pub struct StatusCodeWithUrlAndBody{
pub status_code:reqwest::StatusCode,
pub url:url::Url,
pub body:String,
}
#[derive(Debug)]
pub enum ResponseError{
Reqwest(reqwest::Error),
StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody),
}
impl std::fmt::Display for ResponseError{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f,"{self:?}")
}
}
impl std::error::Error for ResponseError{}
// lazy function to draw out meaningful info from http response on failure
pub async fn response_ok(response:reqwest::Response)->Result<reqwest::Response,ResponseError>{
let status_code=response.status();
if status_code.is_success(){
Ok(response)
}else{
let url=response.url().to_owned();
let bytes=response.bytes().await.map_err(ResponseError::Reqwest)?;
let body=String::from_utf8_lossy(&bytes).to_string();
Err(ResponseError::StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody{
status_code,
url,
body,
}))
}
}