-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
65 lines (57 loc) · 1.17 KB
/
request.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
package youtube
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
)
const (
BaseUrlV3 = "https://www.googleapis.com/youtube/v3"
YoutubePartnerV1 = "https://www.googleapis.com/youtube/partner/v1"
)
type RequestRunner interface {
Run(r *Request) (*http.Response, error)
}
type Request struct {
Method string
Url string
Params url.Values
Body io.Reader
}
func DecodeResponse(res *http.Response, out interface{}) error {
if res.StatusCode >= 400 {
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
var e Error
if err := json.NewDecoder(bytes.NewReader(body)).Decode(&e); err == nil {
e.StatusCode = res.StatusCode
return e
}
e.StatusCode = res.StatusCode
e.ErrorType = ErrTypeUnknown
e.Description = string(body)
return e
}
if out == nil {
return nil
}
if err := json.NewDecoder(res.Body).Decode(out); err != nil {
body, bodyErr := ioutil.ReadAll(res.Body)
if bodyErr != nil {
return Error{
ErrorType: ErrTypeBody,
Description: bodyErr.Error(),
}
}
return Error{
ErrorType: ErrTypeJSON,
Description: err.Error(),
Body: string(body),
}
}
return nil
}