image.rs 816 Bytes
use std::fmt::Display;

use artshop_common::types::ImageJson;

use super::super::error::*;
use super::super::client::Client;

#[derive(Debug, Clone)]
pub struct Image {
    pub json: ImageJson,
}

pub(crate) async fn images() -> Result<Vec<Image>> {
    let client = Client::new()?;
    let (response, data) = client.get("/api/v0/images").await?;

    match response.status() {
        200 => Ok ( serde_json::from_str(data.as_str())
                  . map(|images :Vec<ImageJson>| {
                      images.into_iter().map(|json| Image { json }).collect()
                  })? ),
        status => Err(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())
}

// impl Image {
// }