-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
176 lines (138 loc) · 4.21 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gobalt
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"path"
)
type Cobalt struct {
client *http.Client
apiBaseURL string
}
func NewCobaltWithAPI(apiBaseURL string) *Cobalt {
return &Cobalt{
client: http.DefaultClient,
apiBaseURL: apiBaseURL,
}
}
func (c *Cobalt) WithHTTPClient(client *http.Client) *Cobalt {
c.client = client
return c
}
// Post will return a [PostResponse] from where the file can be downloaded
// headers are passed as key value pairs. Examples `"API-KEY", "MyApiKey"`
func (c *Cobalt) Post(ctx context.Context, params PostRequest, headers ...string) (*PostResponse, error) {
buff := &bytes.Buffer{}
if err := json.NewEncoder(buff).Encode(params); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.apiBaseURL, buff)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
if len(headers)%2 != 0 {
return nil, fmt.Errorf("odd number of headers params, they must be passed as key value pairs")
}
for i := 0; i < len(headers); i += 2 {
req.Header.Add(headers[i], headers[i+1])
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
media := &PostResponse{client: c.client}
if err := json.NewDecoder(resp.Body).Decode(media); err != nil {
return nil, err
}
if media.Status == ResponseStatusError {
return nil, CobaltAPIError(*media)
}
return media, nil
}
// Stream is a helper utility that will return an [io.ReadCloser] using the [PostResponse.URL] from this media object
// The returned [io.ReadCloser] is the Body of [*http.Response] and must be closed when you are done with the stream.
// When the [PostResponse.Status] == [ResponseStatusPicker] it will stream the first item from the [PostResponse.Picker] array.
func (m *PostResponse) Stream(ctx context.Context) (io.ReadCloser, error) {
if m.Status != ResponseStatusTunnel && m.Status != ResponseStatusRedirect && m.Status != ResponseStatusPicker {
return nil, fmt.Errorf("unstreamable response type %s", m.Status)
}
url := m.URL
if m.Status == ResponseStatusPicker && len(m.Picker) > 0 {
url = m.Picker[0].URL
}
if len(url) == 0 {
return nil, fmt.Errorf("url is empty, nothing to stream")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
resp, err := m.client.Do(req)
if err != nil {
return nil, err
}
return resp.Body, nil
}
func (c *Cobalt) Get(ctx context.Context, headers ...string) (*GetResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.apiBaseURL, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
if len(headers)%2 != 0 {
return nil, fmt.Errorf("odd number of headers params, they must be passed as key value pairs")
}
for i := 0; i < len(headers); i += 2 {
req.Header.Add(headers[i], headers[i+1])
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
info := &GetResponse{}
if err := json.NewDecoder(resp.Body).Decode(info); err != nil {
return nil, err
}
return info, nil
}
const (
EndpointSession = "session"
)
func (c *Cobalt) Session(ctx context.Context, headers ...string) (*SessionResponse, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path.Join(c.apiBaseURL, EndpointSession), nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
if len(headers)%2 != 0 {
return nil, fmt.Errorf("odd number of headers params, they must be passed as key value pairs")
}
for i := 0; i < len(headers); i += 2 {
req.Header.Add(headers[i], headers[i+1])
}
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
token := &SessionResponse{}
if err := json.NewDecoder(resp.Body).Decode(token); err != nil {
return nil, err
}
if token.Status == ResponseStatusError {
return nil, fmt.Errorf("%+v", token.ErrorInfo)
}
return token, nil
}
// CobalAPIError is just a convenient type to convert [PostResponse] into an error.
type CobaltAPIError PostResponse
func (err CobaltAPIError) Error() string {
return fmt.Sprintf("%+v", err.ErrorInfo)
}