Skip to content

Commit

Permalink
Merge pull request #35 from kennethloeffler/create-asset-with-contents
Browse files Browse the repository at this point in the history
Add RbxAssets::create_with_contents
  • Loading branch information
Sleitnick authored Nov 27, 2023
2 parents 46eff19 + 919e6a3 commit cd47e49
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/rbx/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub struct CreateAssetParams {
pub filepath: String,
}

pub struct CreateAssetParamsWithContents<'a> {
pub api_key: String,
pub asset: AssetCreation,
pub contents: &'a [u8],
}

pub struct UpdateAssetParams {
pub api_key: String,
pub asset_id: u64,
Expand Down Expand Up @@ -230,6 +236,29 @@ pub async fn create_asset(params: &CreateAssetParams) -> Result<AssetOperation,
handle_res::<AssetOperation>(res).await
}

pub async fn create_asset_with_contents<'a>(
params: &CreateAssetParamsWithContents<'a>,
) -> Result<AssetOperation, Error> {
let file = multipart::Part::bytes(params.contents.to_vec())
.file_name(params.asset.display_name.clone())
.mime_str(params.asset.asset_type.content_type())?;
let asset_info = serde_json::to_string(&params.asset)?;

let form = multipart::Form::new()
.text("request", asset_info)
.part("fileContent", file);

let client = reqwest::Client::new();
let url = build_url(None);
let res = client
.post(url)
.header("x-api-key", &params.api_key)
.multipart(form)
.send()
.await?;
handle_res::<AssetOperation>(res).await
}

pub async fn update_asset(params: &UpdateAssetParams) -> Result<AssetOperation, Error> {
let file_name = Path::new(&params.filepath)
.file_name()
Expand Down
19 changes: 18 additions & 1 deletion src/rbx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use serde::de::DeserializeOwned;
use self::{
assets::{
AssetCreation, AssetGetOperation, AssetOperation, AssetType, CreateAssetParams,
GetAssetParams, UpdateAssetParams,
CreateAssetParamsWithContents, GetAssetParams, UpdateAssetParams,
},
datastore::{
DeleteEntryParams, GetEntryParams, GetEntryVersionParams, IncrementEntryParams,
Expand Down Expand Up @@ -497,6 +497,11 @@ pub struct CreateAsset {
pub filepath: String,
}

pub struct CreateAssetWithContents<'a> {
pub asset: AssetCreation,
pub contents: &'a [u8],
}

pub struct UpdateAsset {
pub asset_id: u64,
pub asset_type: AssetType,
Expand All @@ -518,6 +523,18 @@ impl RbxAssets {
.await
}

pub async fn create_with_contents<'a>(
&self,
params: &CreateAssetWithContents<'a>,
) -> Result<AssetOperation, Error> {
assets::create_asset_with_contents(&CreateAssetParamsWithContents {
api_key: self.api_key.clone(),
asset: params.asset.clone(),
contents: params.contents,
})
.await
}

/// Update an asset
pub async fn update(&self, params: &UpdateAsset) -> Result<AssetOperation, Error> {
assets::update_asset(&UpdateAssetParams {
Expand Down

0 comments on commit cd47e49

Please sign in to comment.