1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
use oauth2_client::re_exports::{
    http::{
        header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE},
        Method,
    },
    serde_json, thiserror, Body, Deserialize, Endpoint, HttpError, Map, Request, Response,
    SerdeJsonError, Serialize, Value, MIME_APPLICATION_JSON,
};

// https://www.dropbox.com/developers/documentation/http/documentation#users-get_account
pub const URL: &str = "https://api.dropboxapi.com/2/users/get_account";

//
#[derive(Debug, Clone)]
pub struct GetAccountEndpoint {
    access_token: String,
    account_id: String,
}
impl GetAccountEndpoint {
    pub fn new(access_token: impl AsRef<str>, account_id: impl AsRef<str>) -> Self {
        Self {
            access_token: access_token.as_ref().to_owned(),
            account_id: account_id.as_ref().to_owned(),
        }
    }
}

impl Endpoint for GetAccountEndpoint {
    type RenderRequestError = GetAccountEndpointError;

    type ParseResponseOutput = Account;
    type ParseResponseError = GetAccountEndpointError;

    fn render_request(&self) -> Result<Request<Body>, Self::RenderRequestError> {
        let body = GetAccountEndpointRequestBody {
            account_id: self.account_id.to_owned(),
        };
        let body_str =
            serde_json::to_string(&body).map_err(GetAccountEndpointError::SerResponseBodyFailed)?;

        let request = Request::builder()
            .uri(URL)
            .method(Method::POST)
            .header(AUTHORIZATION, format!("Bearer {}", &self.access_token))
            .header(CONTENT_TYPE, MIME_APPLICATION_JSON)
            .header(ACCEPT, MIME_APPLICATION_JSON)
            .body(body_str.as_bytes().to_vec())
            .map_err(GetAccountEndpointError::MakeRequestFailed)?;

        Ok(request)
    }

    fn parse_response(
        &self,
        response: Response<Body>,
    ) -> Result<Self::ParseResponseOutput, Self::ParseResponseError> {
        let body = serde_json::from_slice::<Account>(response.body())
            .map_err(GetAccountEndpointError::DeResponseBodyFailed)?;

        Ok(body)
    }
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct GetAccountEndpointRequestBody {
    pub account_id: String,
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Account {
    pub account_id: String,
    pub email: Option<String>,
    //
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub _extra: Option<Map<String, Value>>,
}

#[derive(thiserror::Error, Debug)]
pub enum GetAccountEndpointError {
    #[error("SerResponseBodyFailed {0}")]
    SerResponseBodyFailed(SerdeJsonError),
    #[error("MakeRequestFailed {0}")]
    MakeRequestFailed(HttpError),
    //
    #[error("DeResponseBodyFailed {0}")]
    DeResponseBodyFailed(SerdeJsonError),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn de_account() {
        match serde_json::from_str::<Account>(include_str!(
            "../../tests/response_body_json_files/get_account.json"
        )) {
            Ok(account) => {
                assert_eq!(
                    account.account_id,
                    "dbid:AADMB_j_i3y-F9qhVBv68H3eRnzSJ3bZ9Nw"
                );
            }
            Err(err) => panic!("{}", err),
        }
    }
}