-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapi.go
109 lines (97 loc) · 1.94 KB
/
api.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
package requests
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"github.com/cenkalti/backoff"
)
func doReq(r *Request, c *http.Client) (*http.Response, error) {
res, err := c.Do(r.Request)
if err != nil && r.retry > 0 {
r.retry--
return nil, err
}
if res != nil && res.StatusCode >= 500 && res.StatusCode <= 599 && r.retry > 0 {
r.retry--
return nil, errors.New("Server Error")
}
return res, nil
}
func ToBytes(res *http.Response) ([]byte, error) {
if res != nil {
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
return body, nil
}
return nil, errors.New("Invalid response")
}
func ToByteChannel(res *http.Response, bufferSize int) <-chan []byte {
resp_chan := make(chan []byte)
b := bufio.NewReader(res.Body)
buf := make([]byte, 0, bufferSize)
go func() {
for {
n, err := b.Read(buf[:cap(buf)])
buf = buf[:n]
if n == 0 {
if err == nil {
continue
}
if err == io.EOF {
close(resp_chan)
break
}
log.Fatal(err)
}
resp_chan <- buf
}
}()
return resp_chan
}
func ToJsonChannel(r *http.Response, dec interface{}) <-chan interface{} {
resp_chan := make(chan interface{})
decoder := json.NewDecoder(r.Body)
go func() {
for {
err := decoder.Decode(dec)
if err == nil {
resp_chan <- dec
} else if err == io.EOF {
defer close(resp_chan)
break
} else if err != nil {
fmt.Println(err)
defer close(resp_chan)
break
}
}
}()
return resp_chan
}
// Do should be called when the Request is fully configured.
func (r *Request) Do() (*http.Response, error) {
c := r.newClient()
res, err := doReq(r, c)
if err != nil {
op := r.operation(c)
err = backoff.Retry(op, r.backoff)
if err != nil {
return nil, err
}
}
return res, nil
}
func (r *Request) operation(c *http.Client) func() error {
return func() error {
_, err := doReq(r, c)
return err
}
}