Line data Source code
1 : package api
2 :
3 : import (
4 : "encoding/json"
5 : "net/http"
6 : "time"
7 :
8 : "github.com/woofdogtw/sylvia-iot-go/sdk/constants"
9 : )
10 :
11 : type GetUserResData struct {
12 : UserID string
13 : Account string
14 : CreatedAt time.Time
15 : ModifiedAt time.Time
16 : VerifiedAt time.Time // may be zero time
17 : Roles map[string]bool
18 : Name string
19 : Info map[string]interface{}
20 : }
21 :
22 : type PatchUserReqData struct {
23 : // Empty will not change the original setting.
24 : Password string `json:"password,omitempty"`
25 : // The display name.
26 : //
27 : // `Note`: `nil` will not change setting. Empty string overwrite the original setting.
28 : Name *string `json:"name,omitempty"`
29 : // `nil` will not change the original setting.
30 : Info map[string]interface{} `json:"info,omitempty"`
31 : }
32 :
33 : type userGetRes struct {
34 : Data userGetResData `json:"data"`
35 : }
36 :
37 : type userGetResData struct {
38 : UserID string `json:"userId"`
39 : Account string `json:"account"`
40 : CreatedAt string `json:"createdAt"`
41 : ModifiedAt string `json:"modifiedAt"`
42 : VerifiedAt string `json:"verifiedAt"`
43 : Roles map[string]bool `json:"roles"`
44 : Name string `json:"name"`
45 : Info map[string]interface{} `json:"info"`
46 : }
47 :
48 : type userPatchReq struct {
49 : Data PatchUserReqData `json:"data"`
50 : }
51 :
52 : // `GET /coremgr/api/v1/user`
53 1 : func GetUser(c *Client) (*GetUserResData, *ApiError) {
54 1 : statusCode, body, err := c.Request(http.MethodGet, "/api/v1/user", nil)
55 1 : if err != nil {
56 0 : return nil, &ApiError{
57 0 : Code: constants.ErrRsc.String(),
58 0 : Message: err.Error(),
59 0 : }
60 1 : } else if statusCode != http.StatusOK {
61 0 : var apiErr ApiError
62 0 : if err = json.Unmarshal(body, &apiErr); err != nil {
63 0 : return nil, &ApiError{
64 0 : Code: constants.ErrUnknown.String(),
65 0 : Message: err.Error(),
66 0 : }
67 0 : }
68 0 : return nil, &apiErr
69 : }
70 :
71 1 : var data userGetRes
72 1 : if err = json.Unmarshal(body, &data); err != nil {
73 0 : return nil, &ApiError{
74 0 : Code: constants.ErrUnknown.String(),
75 0 : Message: err.Error(),
76 0 : }
77 0 : }
78 1 : createdAt, err := time.Parse(constants.TimeFormat, data.Data.CreatedAt)
79 1 : if err != nil {
80 0 : return nil, &ApiError{
81 0 : Code: constants.ErrUnknown.String(),
82 0 : Message: err.Error(),
83 0 : }
84 0 : }
85 1 : modifiedAt, err := time.Parse(constants.TimeFormat, data.Data.ModifiedAt)
86 1 : if err != nil {
87 0 : return nil, &ApiError{
88 0 : Code: constants.ErrUnknown.String(),
89 0 : Message: err.Error(),
90 0 : }
91 0 : }
92 1 : var verifiedAt time.Time
93 2 : if data.Data.VerifiedAt != "" {
94 1 : verifiedAt, err = time.Parse(constants.TimeFormat, data.Data.VerifiedAt)
95 1 : if err != nil {
96 0 : return nil, &ApiError{
97 0 : Code: constants.ErrUnknown.String(),
98 0 : Message: err.Error(),
99 0 : }
100 0 : }
101 : }
102 1 : return &GetUserResData{
103 1 : UserID: data.Data.UserID,
104 1 : Account: data.Data.Account,
105 1 : CreatedAt: createdAt,
106 1 : ModifiedAt: modifiedAt,
107 1 : VerifiedAt: verifiedAt,
108 1 : Roles: data.Data.Roles,
109 1 : Name: data.Data.Name,
110 1 : Info: data.Data.Info,
111 1 : }, nil
112 : }
113 :
114 : // `PATCH /coremgr/api/v1/user`
115 2 : func UpdateUser(c *Client, data PatchUserReqData) *ApiError {
116 2 : bodyBytes, err := json.Marshal(userPatchReq{Data: data})
117 2 : if err != nil {
118 0 : return &ApiError{
119 0 : Code: constants.ErrRsc.String(),
120 0 : Message: err.Error(),
121 0 : }
122 0 : }
123 :
124 2 : statusCode, body, err := c.Request(http.MethodPatch, "/api/v1/user", bodyBytes)
125 2 : if err != nil {
126 0 : return &ApiError{
127 0 : Code: constants.ErrRsc.String(),
128 0 : Message: err.Error(),
129 0 : }
130 3 : } else if statusCode != http.StatusNoContent {
131 1 : var apiErr ApiError
132 1 : if err = json.Unmarshal(body, &apiErr); err != nil {
133 0 : return &ApiError{
134 0 : Code: constants.ErrUnknown.String(),
135 0 : Message: err.Error(),
136 0 : }
137 0 : }
138 1 : return &apiErr
139 : }
140 1 : return nil
141 : }
|