1
use std::collections::HashMap;
2

            
3
use bytes::Bytes;
4
use chrono::{DateTime, Utc};
5
use reqwest::{Method, StatusCode};
6
use serde::{Deserialize, Serialize};
7
use serde_json::{Map, Value};
8

            
9
use super::http::{ApiError, Client, Error};
10
use crate::util::err;
11

            
12
#[derive(Deserialize)]
13
pub struct GetResData {
14
    #[serde(rename = "userId")]
15
    pub user_id: Option<String>,
16
    pub account: String,
17
    #[serde(rename = "createdAt")]
18
    pub created_at: DateTime<Utc>,
19
    #[serde(rename = "modifiedAt")]
20
    pub modified_at: DateTime<Utc>,
21
    #[serde(rename = "verifiedAt")]
22
    pub verified_at: Option<DateTime<Utc>>,
23
    pub roles: HashMap<String, bool>,
24
    pub name: String,
25
    pub info: Map<String, Value>,
26
}
27

            
28
#[derive(Default, Serialize)]
29
pub struct PatchReqData {
30
    #[serde(skip_serializing_if = "Option::is_none")]
31
    pub password: Option<String>,
32
    #[serde(skip_serializing_if = "Option::is_none")]
33
    pub name: Option<String>,
34
    #[serde(skip_serializing_if = "Option::is_none")]
35
    pub info: Option<Map<String, Value>>,
36
}
37

            
38
#[derive(Deserialize)]
39
struct GetRes {
40
    data: GetResData,
41
}
42

            
43
#[derive(Serialize)]
44
struct PatchReq {
45
    data: PatchReqData,
46
}
47

            
48
/// `GET /coremgr/api/v1/user`
49
2
pub async fn get(client: &mut Client) -> Result<GetResData, Error> {
50
2
    let (status, body) = client.request(Method::GET, "/api/v1/user", None).await?;
51
1
    if status == StatusCode::OK {
52
1
        match serde_json::from_slice::<GetRes>(body.to_vec().as_slice()) {
53
            Err(e) => {
54
                return Err(Error::Sylvia(ApiError {
55
                    code: err::E_UNKNOWN.to_string(),
56
                    message: Some(e.to_string()),
57
                }))
58
            }
59
1
            Ok(resp) => return Ok(resp.data),
60
        }
61
    }
62
    match serde_json::from_slice::<ApiError>(body.to_vec().as_slice()) {
63
        Err(e) => Err(Error::Sylvia(ApiError {
64
            code: err::E_UNKNOWN.to_string(),
65
            message: Some(e.to_string()),
66
        })),
67
        Ok(err) => Err(Error::Sylvia(err)),
68
    }
69
2
}
70

            
71
/// `PATCH /coremgr/api/v1/user`
72
2
pub async fn update(client: &mut Client, data: PatchReqData) -> Result<(), Error> {
73
2
    let body = match serde_json::to_vec(&PatchReq { data }) {
74
        Err(e) => {
75
            return Err(Error::Sylvia(ApiError {
76
                code: err::E_UNKNOWN.to_string(),
77
                message: Some(e.to_string()),
78
            }))
79
        }
80
2
        Ok(body) => Some(Bytes::from(body)),
81
    };
82

            
83
2
    let (status, body) = client.request(Method::PATCH, "/api/v1/user", body).await?;
84
2
    if status == StatusCode::NO_CONTENT {
85
1
        return Ok(());
86
1
    }
87
1
    match serde_json::from_slice::<ApiError>(body.to_vec().as_slice()) {
88
        Err(e) => Err(Error::Sylvia(ApiError {
89
            code: err::E_UNKNOWN.to_string(),
90
            message: Some(e.to_string()),
91
        })),
92
1
        Ok(err) => Err(Error::Sylvia(err)),
93
    }
94
2
}