-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
139 lines (121 loc) · 3.03 KB
/
client.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Package retroachievements contains all call handlers to the retro achievements API
package retroachievements
import (
"fmt"
"io"
"net/http"
"runtime/debug"
"sync"
raHttp "github.com/joshraphael/go-retroachievements/http"
)
const (
RetroAchievementHost = "https://retroachievements.org"
)
type ClientConfig struct {
Host string
UserAgent string
APISecret string
ConnectConfig *ClientConnectConfig
}
type ClientConnectConfig struct {
ConnectSecret string
ConnectUsername string
}
type Client struct {
UserAgent string
Host string
APISecret string
ConnectSecret string
ConnectUsername string
HttpClient *http.Client
}
type ClientDetail interface {
detail(c *Client)
}
type clientDetailInstance struct {
fn func(c *Client)
}
func (cdi *clientDetailInstance) detail(c *Client) {
cdi.fn(c)
}
func clientDetailFn(fn func(c *Client)) ClientDetail {
return &clientDetailInstance{fn: fn}
}
// HttpClient overrides the default http client used
func HttpClient(httpClient *http.Client) ClientDetail {
return clientDetailFn(func(c *Client) {
c.HttpClient = httpClient
})
}
var version = sync.OnceValue(func() string {
libraryVersion := "v0.0.0"
buildInfo, ok := debug.ReadBuildInfo()
if ok {
for _, dep := range buildInfo.Deps {
if dep.Path == "github.com/joshraphael/go-retroachievements" {
libraryVersion = dep.Version
break
}
}
}
return "go-retroachievements/" + libraryVersion
})
// NewClient makes a new client using the default retroachievement host
func NewClient(secret string) *Client {
return New(ClientConfig{
Host: RetroAchievementHost,
UserAgent: version(),
APISecret: secret,
})
}
// New creates a new client for a given hostname
func New(config ClientConfig, details ...ClientDetail) *Client {
client := &Client{
UserAgent: config.UserAgent,
Host: config.Host,
APISecret: config.APISecret,
HttpClient: &http.Client{
Transport: http.DefaultTransport,
},
}
if config.ConnectConfig != nil && len(config.ConnectConfig.ConnectSecret) > 0 && len(config.ConnectConfig.ConnectUsername) > 0 {
client.ConnectSecret = config.ConnectConfig.ConnectSecret
client.ConnectUsername = config.ConnectConfig.ConnectUsername
}
for _, detail := range details {
detail.detail(client)
}
return client
}
func (c *Client) do(details ...raHttp.RequestDetail) (*raHttp.Response, error) {
r := raHttp.NewRequest(c.Host, details...)
url := r.Host
if r.Path != "" {
url = fmt.Sprintf("%s%s", r.Host, r.Path)
}
req, err := http.NewRequest(r.Method, url, nil)
if err != nil {
return nil, fmt.Errorf("creating new http request: %w", err)
}
q := req.URL.Query()
for k, v := range r.Params {
q.Add(k, v)
}
req.URL.RawQuery = q.Encode()
for k, v := range r.Headers {
req.Header.Add(k, v)
}
resp, err := c.HttpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return &raHttp.Response{
StatusCode: resp.StatusCode,
Data: data,
}, nil
}