forked from sivchari/gotwtr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathme.go
More file actions
47 lines (42 loc) · 1017 Bytes
/
Copy pathme.go
File metadata and controls
47 lines (42 loc) · 1017 Bytes
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
package gotwtr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
)
func me(ctx context.Context, c *client, opt ...*MeOption) (*MeResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, meURL, nil)
if err != nil {
return nil, fmt.Errorf("me new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var mopt MeOption
switch len(opt) {
case 0:
// do nothing
case 1:
mopt = *opt[0]
default:
return nil, errors.New("me: only one option is allowed")
}
mopt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("me response: %w", err)
}
defer func() { _ = resp.Body.Close() }()
var me MeResponse
if err := json.NewDecoder(resp.Body).Decode(&me); err != nil {
return nil, fmt.Errorf("me decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &me, &HTTPError{
APIName: "me",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &me, nil
}