upload.rs 989 Bytes
use std::fmt::Display;

use super::super::error::*;
use super::super::client::Client;
use crate::upload::upload::Upload;

#[derive(Debug, Clone)]
pub struct UploadApi {
    client :Client,
}

impl UploadApi {
    pub(crate) async fn new() -> Result<UploadApi> {
        let client = Client::new()?;
        Ok(UploadApi { client })
    }

    pub(crate) async fn store(&self, upload :&Upload) -> Result<&UploadApi> {
        let response = self.client.post_stream( "/api/v0/upload"
                                              , &upload.mime_type()
                                              , upload.size()
                                              , upload.data() ).await?;
        match response.status() {
            200 => Ok(self),
            status => Err(Self::status_error(status)),
        }
    }

    fn status_error<I: Display>(status :I) -> Error {
        let err_str = format!("Invalid response status: {}", status);
        Error::from(err_str.as_str())
    }
}