hugging_face_client/api/
mod.rs

1//! Request and response
2//!
3//! Hugging Face has open endpoints that you can use to retrieve information from the Hub as well as perform
4//! certain actions such as creating model, dataset or Space repos.
5
6use serde::Deserialize;
7
8use crate::errors::{HuggingFaceResponseSnafu, PlainMessageSnafu, Result};
9
10mod get_models;
11pub use get_models::{GetModelsReq, GetModelsRes};
12
13mod get_model;
14pub use get_model::{GetModelReq, GetModelRes};
15
16mod create_repo;
17pub use create_repo::{CreateRepoReq, CreateRepoRes};
18
19mod delete_repo;
20pub use delete_repo::DeleteRepoReq;
21
22/// Global response format of Hugging Face Hub API
23#[derive(Debug, Clone, Deserialize)]
24#[serde(untagged)]
25pub enum HuggingFaceRes<T> {
26  Success(T),
27  Failure(FailureRes),
28}
29
30impl<T> HuggingFaceRes<T> {
31  /// Unwrap data from [`HuggingFaceRes::Success`]
32  pub fn unwrap_data(self) -> Result<T> {
33    match self {
34      HuggingFaceRes::Success(v) => Ok(v),
35      HuggingFaceRes::Failure(f) => Err(
36        HuggingFaceResponseSnafu {
37          message: f.error().to_string(),
38        }
39        .build(),
40      ),
41    }
42  }
43}
44
45/// Error response format of Hugging Face Hub API
46#[derive(Debug, Clone, Deserialize)]
47pub struct FailureRes {
48  error: String,
49}
50
51impl FailureRes {
52  /// Get error message
53  pub fn error(&self) -> &str {
54    &self.error
55  }
56}