This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclubhouse.go
81 lines (70 loc) · 2.1 KB
/
clubhouse.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
72
73
74
75
76
77
78
79
80
81
package function
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
)
type ClubhouseApiClient struct {
ApiToken string
}
// https://clubhouse.io/api/rest/v3/#Get-Member
type GetMemberResponse struct {
CreatedAt time.Time `json:"created_at"`
Disabled bool `json:"disabled"`
EntityType string `json:"entity_type"`
GroupIds []string `json:"group_ids"`
ID string `json:"id"`
Profile struct {
Deactivated bool `json:"deactivated"`
DisplayIcon struct {
CreatedAt time.Time `json:"created_at"`
EntityType string `json:"entity_type"`
ID string `json:"id"`
UpdatedAt time.Time `json:"updated_at"`
URL string `json:"url"`
} `json:"display_icon"`
EmailAddress string `json:"email_address"`
EntityType string `json:"entity_type"`
GravatarHash string `json:"gravatar_hash"`
ID string `json:"id"`
MentionName string `json:"mention_name"`
Name string `json:"name"`
TwoFactorAuthActivated bool `json:"two_factor_auth_activated"`
} `json:"profile"`
Role string `json:"role"`
UpdatedAt time.Time `json:"updated_at"`
}
func (c *ClubhouseApiClient) GetMember(memberPublicID string) (*GetMemberResponse, error) {
httpClient := http.Client{}
apiURL := fmt.Sprintf("https://api.clubhouse.io/api/v3/members/%s", memberPublicID)
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Clubhouse-Token", c.ApiToken)
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return nil, fmt.Errorf("failed to get member: %q (status code: %d)", data, res.StatusCode)
}
var memberRes GetMemberResponse
err = json.Unmarshal(data, &memberRes)
if err != nil {
log.Printf("\nraw data received: %q \n", data)
return nil, err
}
return &memberRes, nil
}