bachs-sdk 0.1.0

Unofficial Rust SDK for the Bachs API — payments and billing for internet businesses.
Documentation
use crate::client::Bachs;
use crate::common::{FlexObject, Timestamp};
use crate::error::BachsResult;
use reqwest::multipart::{Form, Part};
use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
pub struct Upload {
    /// Attach this to a product (e.g. as `image_upload_id`).
    #[serde(default)]
    pub upload_id: Option<String>,
    #[serde(default)]
    pub url: Option<String>,
    #[serde(default)]
    pub content_type: Option<String>,
    #[serde(default)]
    pub created_at: Option<Timestamp>,
    #[serde(flatten)]
    pub extra: FlexObject,
}

pub struct Media<'a> {
    pub(crate) client: &'a Bachs,
}

impl<'a> Media<'a> {
    /// Upload a file's bytes and get back an `upload_id` you can attach to
    /// other resources (e.g. a product's image).
    pub async fn upload(
        &self,
        filename: impl Into<String>,
        content_type: impl Into<String>,
        bytes: Vec<u8>,
    ) -> BachsResult<Upload> {
        let part = Part::bytes(bytes)
            .file_name(filename.into())
            .mime_str(&content_type.into())
            .map_err(|e| crate::error::BachsError::InvalidRequest(e.to_string()))?;
        let form = Form::new().part("file", part);
        self.client.send_multipart("/v1/media", form).await
    }
}