rbx_asset: move code into util
This commit is contained in:
parent
4985f43f1e
commit
16f2a674c0
@ -1,5 +1,4 @@
|
||||
use crate::util::{serialize_u64,deserialize_u64};
|
||||
use crate::{ResponseError,maybe_gzip_decode};
|
||||
use crate::util::{serialize_u64,deserialize_u64,response_ok,ResponseError,maybe_gzip_decode};
|
||||
|
||||
#[derive(Debug,serde::Deserialize,serde::Serialize)]
|
||||
#[allow(nonstandard_style,dead_code)]
|
||||
@ -381,7 +380,7 @@ impl Context{
|
||||
.text("request",request_config)
|
||||
.part("fileContent",part);
|
||||
|
||||
let operation=crate::response_ok(
|
||||
let operation=response_ok(
|
||||
self.post_form(url,form).await.map_err(CreateError::Reqwest)?
|
||||
).await.map_err(CreateError::Response)?
|
||||
.json::<RobloxOperation>().await.map_err(CreateError::Reqwest)?;
|
||||
@ -400,7 +399,7 @@ impl Context{
|
||||
.text("request",request_config)
|
||||
.part("fileContent",reqwest::multipart::Part::bytes(body));
|
||||
|
||||
let operation=crate::response_ok(
|
||||
let operation=response_ok(
|
||||
self.patch_form(url,form).await.map_err(UpdateError::Reqwest)?
|
||||
).await.map_err(UpdateError::Response)?
|
||||
.json::<RobloxOperation>().await.map_err(UpdateError::Reqwest)?;
|
||||
@ -413,7 +412,7 @@ impl Context{
|
||||
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)?;
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.json::<RobloxOperation>().await.map_err(GetError::Reqwest)
|
||||
@ -422,7 +421,7 @@ impl Context{
|
||||
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)?;
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.json::<AssetResponse>().await.map_err(GetError::Reqwest)
|
||||
@ -431,7 +430,7 @@ impl Context{
|
||||
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)?;
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.json::<AssetResponse>().await.map_err(GetError::Reqwest)
|
||||
@ -440,7 +439,7 @@ impl Context{
|
||||
let raw_url=format!("https://apis.roblox.com/asset-delivery-api/v1/assetId/{}",config.asset_id);
|
||||
let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?;
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.json().await.map_err(GetError::Reqwest)
|
||||
@ -449,7 +448,7 @@ impl Context{
|
||||
let raw_url=format!("https://apis.roblox.com/asset-delivery-api/v1/assetId/{}/version/{}",config.asset_id,config.version);
|
||||
let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?;
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.json().await.map_err(GetError::Reqwest)
|
||||
@ -457,7 +456,7 @@ impl Context{
|
||||
pub async fn get_asset(&self,config:&AssetLocation)->Result<Vec<u8>,GetError>{
|
||||
let url=reqwest::Url::parse(config.location()).map_err(GetError::ParseError)?;
|
||||
|
||||
let body=crate::response_ok(
|
||||
let body=response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.bytes().await.map_err(GetError::Reqwest)?;
|
||||
@ -468,7 +467,7 @@ impl Context{
|
||||
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)?;
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(AssetVersionsError::Reqwest)?
|
||||
).await.map_err(AssetVersionsError::Response)?
|
||||
.json::<AssetVersionsResponse>().await.map_err(AssetVersionsError::Reqwest)
|
||||
@ -483,7 +482,7 @@ impl Context{
|
||||
}
|
||||
}
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(InventoryPageError::Reqwest)?
|
||||
).await.map_err(InventoryPageError::Response)?
|
||||
.json::<InventoryPageResponse>().await.map_err(InventoryPageError::Reqwest)
|
||||
@ -497,7 +496,7 @@ impl Context{
|
||||
query.append_pair("versionType","Published");
|
||||
}
|
||||
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.post(url,body).await.map_err(UpdateError::Reqwest)?
|
||||
).await.map_err(UpdateError::Response)?
|
||||
.json::<UpdatePlaceResponse>().await.map_err(UpdateError::Reqwest)
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{ResponseError,maybe_gzip_decode};
|
||||
use crate::util::{response_ok,ResponseError,maybe_gzip_decode};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PostError{
|
||||
@ -371,7 +371,7 @@ impl Context{
|
||||
query.append_pair("groupId",group_id.to_string().as_str());
|
||||
}
|
||||
}
|
||||
let response=crate::response_ok(
|
||||
let response=response_ok(
|
||||
self.post(url,body).await.map_err(CreateError::PostError)?
|
||||
).await.map_err(CreateError::Response)?;
|
||||
|
||||
@ -423,7 +423,7 @@ impl Context{
|
||||
query.append_pair("groupId",group_id.to_string().as_str());
|
||||
}
|
||||
}
|
||||
let response=crate::response_ok(
|
||||
let response=response_ok(
|
||||
self.post(url,body).await.map_err(UploadError::PostError)?
|
||||
).await.map_err(UploadError::Response)?;
|
||||
|
||||
@ -459,7 +459,7 @@ impl Context{
|
||||
query.append_pair("version",version.to_string().as_str());
|
||||
}
|
||||
}
|
||||
let body=crate::response_ok(
|
||||
let body=response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.bytes().await.map_err(GetError::Reqwest)?;
|
||||
@ -477,7 +477,7 @@ impl Context{
|
||||
query.append_pair("version",version.to_string().as_str());
|
||||
}
|
||||
}
|
||||
let response=crate::response_ok(
|
||||
let response=response_ok(
|
||||
self.get(url).await.map_err(GetAssetV2Error::Reqwest)?
|
||||
).await.map_err(GetAssetV2Error::Response)?;
|
||||
|
||||
@ -500,7 +500,7 @@ impl Context{
|
||||
pub async fn get_asset_v2_download(&self,config:&GetAssetV2Location)->Result<Vec<u8>,GetError>{
|
||||
let url=reqwest::Url::parse(config.location.as_str()).map_err(GetError::ParseError)?;
|
||||
|
||||
let body=crate::response_ok(
|
||||
let body=response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.bytes().await.map_err(GetError::Reqwest)?;
|
||||
@ -509,7 +509,7 @@ impl Context{
|
||||
}
|
||||
pub async fn get_asset_details(&self,config:GetAssetDetailsRequest)->Result<AssetDetails,GetError>{
|
||||
let url=reqwest::Url::parse(format!("https://economy.roblox.com/v2/assets/{}/details",config.asset_id).as_str()).map_err(GetError::ParseError)?;
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(GetError::Reqwest)?
|
||||
).await.map_err(GetError::Response)?
|
||||
.json().await.map_err(GetError::Reqwest)
|
||||
@ -526,7 +526,7 @@ impl Context{
|
||||
query.append_pair("cursor",cursor);
|
||||
}
|
||||
}
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(PageError::Reqwest)?
|
||||
).await.map_err(PageError::Response)?
|
||||
.json::<AssetVersionsPageResponse>().await.map_err(PageError::Reqwest)
|
||||
@ -541,7 +541,7 @@ impl Context{
|
||||
query.append_pair("cursor",cursor);
|
||||
}
|
||||
}
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(PageError::Reqwest)?
|
||||
).await.map_err(PageError::Response)?
|
||||
.json::<CreationsPageResponse>().await.map_err(PageError::Reqwest)
|
||||
@ -555,7 +555,7 @@ impl Context{
|
||||
query.append_pair("cursor",cursor);
|
||||
}
|
||||
}
|
||||
crate::response_ok(
|
||||
response_ok(
|
||||
self.get(url).await.map_err(PageError::Reqwest)?
|
||||
).await.map_err(PageError::Response)?
|
||||
.json::<UserInventoryPageResponse>().await.map_err(PageError::Reqwest)
|
||||
|
@ -1,50 +1,3 @@
|
||||
pub mod cloud;
|
||||
pub mod cookie;
|
||||
mod util;
|
||||
|
||||
#[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(crate) 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,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn maybe_gzip_decode(data:bytes::Bytes)->std::io::Result<Vec<u8>>{
|
||||
match data.get(0..2){
|
||||
Some(b"\x1f\x8b")=>{
|
||||
use std::io::Read;
|
||||
let mut buf=Vec::new();
|
||||
flate2::read::GzDecoder::new(std::io::Cursor::new(data)).read_to_end(&mut buf)?;
|
||||
Ok(buf)
|
||||
},
|
||||
_=>Ok(data.to_vec()),
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,50 @@
|
||||
#[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(crate) 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,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn maybe_gzip_decode(data:bytes::Bytes)->std::io::Result<Vec<u8>>{
|
||||
match data.get(0..2){
|
||||
Some(b"\x1f\x8b")=>{
|
||||
use std::io::Read;
|
||||
let mut buf=Vec::new();
|
||||
flate2::read::GzDecoder::new(std::io::Cursor::new(data)).read_to_end(&mut buf)?;
|
||||
Ok(buf)
|
||||
},
|
||||
_=>Ok(data.to_vec()),
|
||||
}
|
||||
}
|
||||
|
||||
use serde::de::{Error,Unexpected};
|
||||
use serde::{Deserializer,Serializer};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user