hugging_face_client/client/
repo.rs

1use reqwest::Method;
2
3use crate::{
4  api::{
5    CreateRepoReq, CreateRepoRes, DeleteRepoReq, GetDatasetReq, GetDatasetRes, GetDatasetTagRes,
6    SearchDatasetReq, SearchDatasetRes, GetModelReq, GetModelRes, GetModelTagsRes, SearchModelReq,
7    SearchModelRes, GetSpaceReq, GetSpaceRes, SearchSpaceReq, SearchSpaceRes,
8  },
9  client::Client,
10  errors::Result,
11};
12
13impl Client {
14  /// Get information from all models in the Hub
15  ///
16  /// Endpoint: `GET /api/models`
17  pub async fn search_model(&self, req: SearchModelReq<'_>) -> Result<SearchModelRes> {
18    let url = format!("{}/api/models", &self.api_endpoint);
19    self.get_request(&url, Some(&req), true).await
20  }
21
22  /// Get all information for a specific model
23  ///
24  /// Endpoint: `GET /api/models/{repo_id}` or
25  ///
26  /// Endpoint: `GET /api/models/{repo_id}/revision/{revision}`
27  pub async fn get_model(&self, req: GetModelReq<'_>) -> Result<GetModelRes> {
28    let url = if let Some(revision) = req.revision {
29      format!(
30        "{}/api/models/{}/revision/{}",
31        &self.api_endpoint, req.repo_name, revision
32      )
33    } else {
34      format!("{}/api/models/{}", &self.api_endpoint, req.repo_name)
35    };
36    let req = if true { None } else { Some(&req) };
37    self.get_request(&url, req, true).await
38  }
39
40  /// Gets all the available model tags hosted in the Hub
41  ///
42  /// Endpoint: `GET /api/models-tags-by-type`
43  pub async fn get_model_tags(&self) -> Result<GetModelTagsRes> {
44    let url = format!("{}/api/models-tags-by-type", &self.api_endpoint);
45    let req = if true { None } else { Some(&()) };
46    self.get_request(&url, req, false).await
47  }
48
49  /// Get information from all datasets in the Hub
50  ///
51  /// Endpoint: ` GET /api/datasets`
52  pub async fn search_dataset(&self, req: SearchDatasetReq<'_>) -> Result<SearchDatasetRes> {
53    let url = format!("{}/api/datasets", &self.api_endpoint);
54    self.get_request(&url, Some(&req), true).await
55  }
56
57  /// Get all information for a specific dataset
58  ///
59  /// Endpoint: `GET /api/datasets/{repo_id}` or
60  ///
61  /// Endpoint: `GET /api/datasets/{repo_id}/revision/{revision}`
62  pub async fn get_dataset(&self, req: GetDatasetReq<'_>) -> Result<GetDatasetRes> {
63    let url = if let Some(revision) = req.revision {
64      format!(
65        "{}/api/datasets/{}/revision/{}",
66        &self.api_endpoint, req.repo_name, revision
67      )
68    } else {
69      format!("{}/api/datasets/{}", &self.api_endpoint, req.repo_name)
70    };
71    let req = if true { None } else { Some(&req) };
72    self.get_request(&url, req, true).await
73  }
74
75  /// Gets all the available dataset tags hosted in the Hub.
76  ///
77  /// Endpoint: `GET /api/datasets-tags-by-type`
78  pub async fn get_dataset_tags(&self) -> Result<GetDatasetTagRes> {
79    let url = format!("{}/api/models-tags-by-type", &self.api_endpoint);
80    let req = if true { None } else { Some(&()) };
81    self.get_request(&url, req, false).await
82  }
83
84  /// Get information from all spaces in the Hub
85  ///
86  /// Endpoint: ` GET /api/spaces`
87  pub async fn search_space(&self, req: SearchSpaceReq<'_>) -> Result<SearchSpaceRes> {
88    let url = format!("{}/api/spaces", &self.api_endpoint);
89    self.get_request(&url, Some(&req), true).await
90  }
91
92  /// Get all information for a specific space
93  ///
94  /// Endpoint: `GET /api/spaces/{repo_id}` or
95  ///
96  /// Endpoint: `GET /api/spaces/{repo_id}/revision/{revision}`
97  pub async fn get_space(&self, req: GetSpaceReq<'_>) -> Result<GetSpaceRes> {
98    let url = if let Some(revision) = req.revision {
99      format!(
100        "{}/api/spaces/{}/revision/{}",
101        &self.api_endpoint, req.repo_name, revision
102      )
103    } else {
104      format!("{}/api/spaces/{}", &self.api_endpoint, req.repo_name)
105    };
106    let req = if true { None } else { Some(&req) };
107    self.get_request(&url, req, true).await
108  }
109
110  /// Create a repository, model repo by default.
111  ///
112  /// Endpoint:  POST /api/repos/create
113  pub async fn create_repo(&self, req: CreateRepoReq<'_>) -> Result<CreateRepoRes> {
114    let url = format!("{}/api/repos/create", &self.api_endpoint);
115    self.exec_request(&url, Method::POST, Some(&req)).await
116  }
117
118  /// Delete a repository, model repo by default
119  ///
120  /// Endpoint: `DELETE /api/repos/delete`
121  ///
122  pub async fn delete_repo(&self, req: DeleteRepoReq<'_>) -> Result<()> {
123    let url = format!("{}/api/repos/delete", &self.api_endpoint);
124    self
125      .exec_request_without_response(&url, Method::DELETE, Some(&req))
126      .await
127  }
128}