gitea_sdk/api/user/mod.rs
1pub mod create_repo;
2pub mod current;
3pub mod list_repos;
4pub mod orgs;
5pub mod settings;
6pub mod starred;
7pub mod tokens;
8
9pub struct User;
10
11impl User {
12 /// Gets the currently authenticated user.
13 /// This will return a [User] object representing the currently authenticated user.
14 /// As long as the token is valid, this method will always return a user.
15 ///
16 /// # Example
17 /// ```
18 /// # use gitea_sdk::{Client, Auth};
19 /// # async fn get_authenticated_user() {
20 /// let client = Client::new(
21 /// "https://gitea.example.com",
22 /// Auth::Token("your-token")
23 /// );
24 /// let user = client
25 /// .user()
26 /// .current()
27 /// .send(&client)
28 /// .await
29 /// .unwrap();
30 /// # }
31 pub fn current(&self) -> current::GetAuthenticatedUserBuilder {
32 current::GetAuthenticatedUserBuilder::new()
33 }
34
35 /// Creates a new repository for the authenticated user.
36 ///
37 /// # Example
38 /// ```
39 /// # use gitea_sdk::{Client, Auth};
40 /// # async fn create_repo() {
41 /// let client = Client::new(
42 /// "https://gitea.example.com",
43 /// Auth::Token("your-token")
44 /// );
45 /// let repo = client
46 /// .user()
47 /// .create_repo("my-new-repo")
48 /// .private(true)
49 /// .send(&client)
50 /// .await
51 /// .unwrap();
52 /// # }
53 /// ```
54 /// This will create a new private repository with the name "my-new-repo" for the authenticated user.
55 pub fn create_repo(&self, name: impl ToString) -> create_repo::CreateRepoBuilder {
56 create_repo::CreateRepoBuilder::new(name)
57 }
58
59 /// Lists all repositories for the authenticated user.
60 /// This will return a list of all [Repository](crate::model::repos::Repository) objects
61 /// owned by the authenticated user.
62 ///
63 /// # Example
64 /// ```
65 /// # use gitea_sdk::{Client, Auth};
66 /// # async fn list_repos() {
67 /// let client = Client::new(
68 /// "https://gitea.example.com",
69 /// Auth::Token("your-token")
70 /// );
71 /// let repos = client
72 /// .user()
73 /// .list_repos()
74 /// .limit(10)
75 /// .page(2)
76 /// .send(&client)
77 /// .await
78 /// .unwrap();
79 /// # }
80 /// ```
81 pub fn list_repos(&self) -> list_repos::ListReposBuilder {
82 list_repos::ListReposBuilder::new()
83 }
84
85 /// List the current user's organizations.
86 ///
87 /// # Example
88 /// ```
89 /// # use gitea_sdk::{Client, Auth};
90 /// # async fn list_orgs() {
91 /// let client = Client::new(
92 /// "https://gitea.example.com",
93 /// Auth::Token("your-token")
94 /// );
95 /// let orgs = client
96 /// .user()
97 /// .orgs()
98 /// .page(2)
99 /// .limit(10)
100 /// .send(&client)
101 /// .await
102 /// .unwrap();
103 /// # }
104 /// ```
105 pub fn orgs(&self) -> orgs::Orgs {
106 orgs::Orgs::new()
107 }
108
109 /// Creates a new access token for a user.
110 /// NOTE: This endpoint requires basic authentication and will fail otherwise.
111 ///
112 /// # Example
113 /// ```
114 /// # use gitea_sdk::{Client, CreateAccessTokenOption, Auth};
115 /// # async fn create_token() {
116 /// let client = Client::new(
117 /// "https://gitea.example.com",
118 /// Auth::Basic("username", "password")
119 /// );
120 /// let token = client
121 /// .user()
122 /// .create_access_token("username", "my-new-token", vec!["write:repository", "read:user"])
123 /// .send(&client)
124 /// .await
125 /// .unwrap();
126 /// println!("Token {} created: {}", token.name, token.sha1);
127 /// let new_client = Client::new(
128 /// "https://gitea.example.com",
129 /// Auth::Token(token.sha1)
130 /// );
131 /// # }
132 /// ```
133 /// This will create a new token with the name "my-new-token", which can read all user data and
134 /// read and write to repositories.
135 ///
136 /// If the token is successfully created, this method will return a [AccessToken] object.
137 /// If the user is not authenticated correctly (e.g. not using basic auth), this method will
138 /// return a 403 status code.
139 /// In case of any client-side errors, this method will return a 400 status code.
140 pub fn create_access_token(
141 &self,
142 user: impl ToString,
143 name: impl ToString,
144 scopes: Vec<impl ToString>,
145 ) -> tokens::CreateAccessTokenBuilder {
146 tokens::CreateAccessTokenBuilder::new(user, name, scopes)
147 }
148
149 /// Lists all access tokens for a user.
150 /// NOTE: This endpoint requires basic authentication and will fail otherwise.
151 ///
152 /// # Example
153 /// ```
154 /// # use gitea_sdk::{Client, Auth};
155 /// # async fn list_tokens() {
156 /// let client = Client::new(
157 /// "https://gitea.example.com",
158 /// Auth::Basic("username", "password")
159 /// );
160 /// let tokens = client
161 /// .user()
162 /// .list_access_tokens("username")
163 /// .send(&client)
164 /// .await
165 /// .unwrap();
166 /// # }
167 /// ```
168 /// This will list all access tokens for the user "username".
169 pub fn list_access_tokens(&self, username: impl ToString) -> tokens::ListAccessTokensBuilder {
170 tokens::ListAccessTokensBuilder::new(username)
171 }
172
173 /// Deletes an access token by its username and token.
174 /// This will delete the token and revoke all permissions associated with it.
175 /// NOTE: This endpoint requires basic authentication and will fail otherwise.
176 ///
177 /// # Example
178 /// ```
179 /// # use gitea_sdk::{Client, Auth};
180 /// # async fn delete_token() {
181 /// let client = Client::new(
182 /// "https://gitea.example.com",
183 /// Auth::Basic("username", "password")
184 /// );
185 /// client.
186 /// user()
187 /// .delete_access_token("username", "token")
188 /// .send(&client)
189 /// .await
190 /// .unwrap();
191 /// # }
192 /// ```
193 /// This will delete the token with the name "token-name" for the user "username".
194 ///
195 /// If the token does not exist, this method will return a 404 status code.
196 /// If the target user is not the authenticated user and the authenticated user is not an
197 /// administrator, this method will return a 403 status code.
198 /// For any client-side other errors, this method will return a 422 status code.
199 /// If the token is successfully deleted, this method will return a 204 status code.
200 pub fn delete_access_token(
201 &self,
202 user: impl ToString,
203 token: impl ToString,
204 ) -> tokens::DeleteAccessTokenBuilder {
205 tokens::DeleteAccessTokenBuilder::new(user, token)
206 }
207
208 /// Gets the current user's settings.
209 ///
210 /// # Example
211 /// ```
212 /// # use gitea_sdk::{Client, Auth};
213 /// # async fn get_settings() {
214 /// let client = Client::new(
215 /// "https://gitea.example.com",
216 /// Auth::Token("your-token")
217 /// );
218 /// let settings = client
219 /// .user()
220 /// .get_settings()
221 /// .send(&client)
222 /// .await
223 /// .unwrap();
224 /// println!("User settings: {:?}", settings);
225 /// # }
226 /// ```
227 pub fn get_settings(&self) -> settings::GetSettingsBuilder {
228 settings::GetSettingsBuilder::new()
229 }
230
231 /// Updates the current user's settings.
232 ///
233 /// # Example
234 /// ```
235 /// # use gitea_sdk::{Client, Auth};
236 /// # async fn update_settings() {
237 /// let client = Client::new(
238 /// "https://gitea.example.com",
239 /// Auth::Token("your-token")
240 /// );
241 /// let settings = client
242 /// .user()
243 /// .update_settings()
244 /// .theme("dark")
245 /// .send(&client)
246 /// .await
247 /// .unwrap();
248 /// println!("User settings: {:?}", settings);
249 /// # }
250 /// ```
251 /// This will update the user's theme to "dark".
252 pub fn update_settings(&self) -> settings::UpdateSettingsBuilder {
253 settings::UpdateSettingsBuilder::new()
254 }
255
256 /// Lists all repositories starred by the authenticated user.
257 ///
258 /// # Example
259 /// ```
260 /// # use gitea_sdk::{Client, Auth};
261 /// # async fn list_starred_repos() {
262 /// let client = Client::new(
263 /// "https://gitea.example.com",
264 /// Auth::Token("your-token")
265 /// );
266 /// let repos = client
267 /// .user()
268 /// .list_starred()
269 /// .send(&client)
270 /// .await
271 /// .unwrap();
272 /// # }
273 /// ```
274 pub fn list_starred(&self) -> starred::ListStarredBuilder {
275 starred::ListStarredBuilder::new()
276 }
277
278 /// Checks if the authenticated user has starred a repository.
279 ///
280 /// # Example
281 /// ```
282 /// # use gitea_sdk::{Client, Auth};
283 /// # async fn is_starred() {
284 /// let client = Client::new(
285 /// "https://gitea.example.com",
286 /// Auth::Token("your-token")
287 /// );
288 /// let starred = client
289 /// .user()
290 /// .is_starred("owner", "repo")
291 /// .send(&client)
292 /// .await
293 /// .unwrap();
294 /// if starred {
295 /// println!("You have starred this repo!");
296 /// } else {
297 /// println!("You have not starred this repo.");
298 /// }
299 /// # }
300 /// ```
301 pub fn is_starred(
302 &self,
303 owner: impl ToString,
304 repo: impl ToString,
305 ) -> starred::IsStarredBuilder {
306 starred::IsStarredBuilder::new(owner, repo)
307 }
308
309 /// Stars a repository for the authenticated user.
310 /// This will star the repository with the given owner and name.
311 ///
312 /// # Example
313 /// ```
314 /// # use gitea_sdk::{Client, Auth};
315 /// # async fn star_repo() {
316 /// let client = Client::new(
317 /// "https://gitea.example.com",
318 /// Auth::Token("your-token")
319 /// );
320 /// client
321 /// .user()
322 /// .star_repo("owner", "repo")
323 /// .send(&client)
324 /// .await
325 /// .unwrap();
326 /// # }
327 /// ```
328 /// This will star the repository "repo" owned by "owner".
329 pub fn star_repo(&self, owner: impl ToString, repo: impl ToString) -> starred::StarRepoBuilder {
330 starred::StarRepoBuilder::new(owner, repo)
331 }
332
333 /// Unstars a repository for the authenticated user.
334 /// This will unstar the repository with the given owner and name.
335 ///
336 /// # Example
337 /// ```
338 /// # use gitea_sdk::{Client, Auth};
339 /// # async fn unstar_repo() {
340 /// let client = Client::new(
341 /// "https://gitea.example.com",
342 /// Auth::Token("your-token")
343 /// );
344 /// client
345 /// .user()
346 /// .unstar_repo("owner", "repo")
347 /// .send(&client)
348 /// .await
349 /// .unwrap();
350 /// # }
351 /// ```
352 pub fn unstar_repo(
353 &self,
354 owner: impl ToString,
355 repo: impl ToString,
356 ) -> starred::UnstarRepoBuilder {
357 starred::UnstarRepoBuilder::new(owner, repo)
358 }
359}