fars/api/get_user_data.rs
1//! Implements the get user data API of the Firebase Auth.
2//!
3//! You can get a user's data by issuing an HTTP POST request to the Auth getAccountInfo endpoint.
4//!
5//! See also [API reference](https://firebase.google.com/docs/reference/rest/auth#section-get-account-info)
6
7use serde::{Deserialize, Serialize};
8
9use crate::ApiKey;
10use crate::Client;
11use crate::Endpoint;
12use crate::Result;
13use crate::UserData;
14
15/// Request body payload for the get user data API.
16///
17/// See also [API reference](https://firebase.google.com/docs/reference/rest/auth#section-get-account-info).
18#[derive(Serialize)]
19pub struct GetUserDataRequestBodyPayload {
20 /// The Firebase ID token of the account.
21 #[serde(rename = "idToken")]
22 id_token: String,
23}
24
25impl GetUserDataRequestBodyPayload {
26 /// Creates a new request body payload for the get user data API.
27 ///
28 /// See also [API reference](https://firebase.google.com/docs/reference/rest/auth#section-get-account-info).
29 ///
30 /// ## Arguments
31 /// - `id_token` - The Firebase ID token of the account.
32 pub fn new(id_token: String) -> Self {
33 Self {
34 id_token,
35 }
36 }
37}
38
39/// Response payload for the get user data API.
40///
41/// See also [API reference](https://firebase.google.com/docs/reference/rest/auth#section-get-account-info).
42#[derive(Deserialize, Debug)]
43pub struct GetUserDataResponsePayload {
44 /// The account associated with the given Firebase ID token.
45 #[serde(rename = "users")]
46 pub users: Vec<UserData>,
47}
48
49/// Gets the user data.
50///
51/// See also [API reference](https://firebase.google.com/docs/reference/rest/auth#section-get-account-info).
52///
53/// ## Arguments
54/// - `client` - HTTP client.
55/// - `api_key` - Your Firebase project's API key.
56/// - `request_payload` - Request body payload.
57///
58/// ## Errors
59/// - `Error::HttpRequestError` - Failed to send a request.
60/// - `Error::ReadResponseTextFailed` - Failed to read the response body as text.
61/// - `Error::DeserializeResponseJsonFailed` - Failed to deserialize the response body as JSON.
62/// - `Error::DeserializeErrorResponseJsonFailed` - Failed to deserialize the error response body as JSON.
63/// - `Error::InvalidIdToken` - Invalid ID token.
64/// - `Error::ApiError` - API error on the Firebase Auth.
65///
66/// ## Common error codes
67/// - INVALID_ID_TOKEN:The user's credential is no longer valid. The user must sign in again.
68/// - USER_NOT_FOUND: There is no user record corresponding to this identifier. The user may have been deleted.
69///
70/// ## Example
71/// ```
72/// use fars::api;
73/// use fars::Client;
74/// use fars::ApiKey;
75///
76/// let request_payload = api::GetUserDataRequestBodyPayload::new(
77/// "id-token".to_string(),
78/// );
79///
80/// let response_payload = api::get_user_data(
81/// Client::new(),
82/// ApiKey::new("your-firebase-project-api-key"),
83/// request_payload,
84/// ).await?;
85/// ```
86pub async fn get_user_data(
87 client: &Client,
88 api_key: &ApiKey,
89 request_payload: GetUserDataRequestBodyPayload,
90) -> Result<GetUserDataResponsePayload> {
91 client
92 .send_post::<GetUserDataRequestBodyPayload, GetUserDataResponsePayload>(
93 Endpoint::Lookup,
94 api_key,
95 request_payload,
96 None,
97 )
98 .await
99}