-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
79 lines (69 loc) · 1.67 KB
/
examples_test.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
package httpclient
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
)
func ExampleClient_Get() {
sdk := NewSDK()
user, err := sdk.GetUserByUsername(context.Background(), "georgepsarakis")
panicOnError(err)
m, err := json.MarshalIndent(user, "", " ")
panicOnError(err)
fmt.Println(string(m))
// Output:
//{
// "id": 963304,
// "bio": "Software Engineer",
// "blog": "https://controlflow.substack.com/",
// "created_at": "2011-08-06T16:57:12Z",
// "login": "georgepsarakis",
// "name": "George Psarakis"
//}
}
func panicOnError(err error) {
if err != nil {
panic(err)
}
}
type GitHubSDK struct {
Client *Client
}
func NewSDK() GitHubSDK {
return NewSDKWithClient(New())
}
func NewSDKWithClient(c *Client) GitHubSDK {
c.WithDefaultHeaders(map[string]string{
"X-GitHub-Api-Version": "2022-11-28",
"Accept": "application/vnd.github+json",
})
c, _ = c.WithBaseURL("https://api.github.com")
return GitHubSDK{Client: c}
}
type User struct {
ID int `json:"id"`
Bio string `json:"bio"`
Blog string `json:"blog"`
CreatedAt time.Time `json:"created_at"`
Login string `json:"login"`
Name string `json:"name"`
}
// GetUserByUsername retrieves a user based on their public username.
// See https://docs.github.com/en/rest/users/users
func (g GitHubSDK) GetUserByUsername(ctx context.Context, username string) (User, error) {
path, err := url.JoinPath("/users", username)
if err != nil {
return User{}, err
}
resp, err := g.Client.Get(ctx, path)
if err != nil {
return User{}, err
}
u := User{}
if err := DeserializeJSON(resp, &u); err != nil {
return u, err
}
return u, nil
}