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, Serialize};
7
8use crate::errors::{HuggingFaceResponseSnafu, Result};
9
10mod search_models;
11pub use search_models::{SearchModelReq, SearchModelRes};
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
22mod get_model_tags;
23pub use get_model_tags::GetModelTagsRes;
24
25mod search_datasets;
26pub use search_datasets::{SearchDatasetReq, SearchDatasetRes};
27
28mod get_dataset;
29pub use get_dataset::{GetDatasetReq, GetDatasetRes};
30
31mod get_dataset_tags;
32pub use get_dataset_tags::GetDatasetTagRes;
33
34mod get_space;
35pub use get_space::{GetSpaceReq, GetSpaceRes};
36
37mod search_spaces;
38pub use search_spaces::{SearchSpaceReq, SearchSpaceRes};
39
40/// Global response format of Hugging Face Hub API
41#[derive(Debug, Clone, Deserialize)]
42#[serde(untagged)]
43pub enum HuggingFaceRes<T> {
44  Success(T),
45  Failure(FailureRes),
46}
47
48impl<T> HuggingFaceRes<T> {
49  /// Unwrap data from [`HuggingFaceRes::Success`]
50  pub fn unwrap_data(self) -> Result<T> {
51    match self {
52      HuggingFaceRes::Success(v) => Ok(v),
53      HuggingFaceRes::Failure(f) => Err(
54        HuggingFaceResponseSnafu {
55          message: f.error().to_string(),
56        }
57        .build(),
58      ),
59    }
60  }
61}
62
63/// Error response format of Hugging Face Hub API
64#[derive(Debug, Clone, Deserialize)]
65pub struct FailureRes {
66  error: String,
67}
68
69impl FailureRes {
70  /// Get error message
71  pub fn error(&self) -> &str {
72    &self.error
73  }
74}
75
76#[derive(Debug, Default, Serialize)]
77pub struct SearchReq<'a> {
78  #[serde(skip_serializing_if = "Option::is_none")]
79  search: Option<&'a str>,
80
81  #[serde(skip_serializing_if = "Option::is_none")]
82  author: Option<&'a str>,
83
84  #[serde(skip_serializing_if = "Option::is_none")]
85  filter: Option<&'a str>,
86
87  #[serde(skip_serializing_if = "Option::is_none")]
88  sort: Option<&'a str>,
89
90  #[serde(skip_serializing_if = "Option::is_none")]
91  direction: Option<i32>,
92
93  #[serde(skip_serializing_if = "Option::is_none")]
94  limit: Option<usize>,
95
96  #[serde(skip_serializing_if = "Option::is_none")]
97  full: Option<bool>,
98
99  #[serde(skip_serializing_if = "Option::is_none")]
100  config: Option<bool>,
101}
102
103impl<'a> SearchReq<'a> {
104  pub fn search(mut self, search: &'a str) -> Self {
105    self.search = Some(search);
106    self
107  }
108
109  pub fn author(mut self, author: &'a str) -> Self {
110    self.author = Some(author);
111    self
112  }
113
114  pub fn filter(mut self, filter: &'a str) -> Self {
115    self.filter = Some(filter);
116    self
117  }
118
119  pub fn sort(mut self, sort: &'a str) -> Self {
120    self.sort = Some(sort);
121    self
122  }
123
124  pub fn direction(mut self, direction: i32) -> Self {
125    self.direction = Some(direction);
126    self
127  }
128
129  pub fn limit(mut self, limit: usize) -> Self {
130    self.limit = Some(limit);
131    self
132  }
133
134  pub fn full(mut self, full: bool) -> Self {
135    self.full = Some(full);
136    self
137  }
138
139  pub fn config(mut self, config: bool) -> Self {
140    self.config = Some(config);
141    self
142  }
143}