-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
81 lines (64 loc) · 1.58 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
package disk
import (
"context"
"io"
"log"
"net/http"
"os"
"time"
)
// todo: add context cancellation
const API_URL = "https://cloud-api.yandex.net/v1/disk/"
type HttpMethod string
const (
GET HttpMethod = "GET"
POST HttpMethod = "POST"
PUT HttpMethod = "PUT"
PATCH HttpMethod = "PATCH"
DELETE HttpMethod = "DELETE"
)
type Client struct {
AccessToken string
HTTPClient *http.Client
Logger *log.Logger
}
// New(token ...string) fetch token from OS env var if has not direct defined
func New(token ...string) *Client {
if len(token) == 0 {
envToken := os.Getenv("YANDEX_DISK_ACCESS_TOKEN")
if envToken == "" {
return nil
}
token = append(token, envToken)
}
return &Client{
AccessToken: token[0],
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
}
}
func (c *Client) doRequest(ctx context.Context, method HttpMethod, resource string, data io.Reader) (*http.Response, error) {
var resp *http.Response
var err error
var body io.Reader
body = data
// todo: make time parameterized, not const
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second))
defer cancel()
if method == GET || method == DELETE {
body = nil
}
req, err := http.NewRequestWithContext(ctx, string(method), API_URL+resource, body)
if err != nil {
c.Logger.Fatal("error request", err)
return nil, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "OAuth "+c.AccessToken)
if resp, err = c.HTTPClient.Do(req); err != nil {
c.Logger.Fatal("error response", err)
return nil, err
}
return resp, err
}