hugging_face_client/client/
repo.rs

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