-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
71 lines (58 loc) · 1.79 KB
/
user.go
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
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2023-Present Datadog, Inc.
package cloudcraft
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
)
// userPath is the path to the user endpoint of the Cloudcraft API.
const userPath string = "user"
// UserService handles communication with the "/user" endpoint of Cloudcraft's
// developer API.
type UserService service
// User represents a Cloudcraft user.
type User struct {
AccessedAt time.Time `json:"accessedAt,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
UpdatedAt time.Time `json:"updatedAt,omitempty"`
Settings map[string]any `json:"settings,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
}
// Me returns the user profile.
//
// [API reference].
//
// [API reference]: https://docs.datadoghq.com/cloudcraft/api/users/#get-user-profile
func (s *UserService) Me(ctx context.Context) (*User, *Response, error) {
if ctx == nil {
return nil, nil, ErrNilContext
}
var (
baseURL = s.client.cfg.endpoint.String()
endpoint strings.Builder
)
endpoint.Grow(len(baseURL) + len(userPath) + 3)
endpoint.WriteString(baseURL)
endpoint.WriteString(userPath)
endpoint.WriteString("/me")
req, err := s.client.request(ctx, http.MethodGet, endpoint.String(), http.NoBody)
if err != nil {
return nil, nil, err
}
ret, err := s.client.do(req)
if err != nil {
return nil, nil, err
}
var user *User
if err := json.Unmarshal(ret.Body, &user); err != nil {
return nil, nil, fmt.Errorf("%w", err)
}
return user, ret, nil
}