-
Notifications
You must be signed in to change notification settings - Fork 0
/
superagent.go
128 lines (103 loc) · 2.66 KB
/
superagent.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
package pkg
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/go-errors/errors"
"github.com/ory-am/hydra/pkg/helper"
)
type SuperAgent struct {
Client *http.Client
URL string
Dry bool
}
func NewSuperAgent(rawurl string) *SuperAgent {
return &SuperAgent{
URL: rawurl,
Client: http.DefaultClient,
}
}
func (s *SuperAgent) doDry(req *http.Request) error {
return helper.DoDryRequest(s.Dry, req)
}
func (s *SuperAgent) Delete() error {
req, err := http.NewRequest("DELETE", s.URL, nil)
if err != nil {
return errors.New(err)
}
if err := s.doDry(req); err != nil {
return err
}
resp, err := s.Client.Do(req)
if err != nil {
return errors.New(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
body, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("Expected status code %d, got %d.\n%s\n", http.StatusNoContent, resp.StatusCode, body)
}
return nil
}
func (s *SuperAgent) Get(o interface{}) error {
req, err := http.NewRequest("GET", s.URL, nil)
if err != nil {
return errors.New(err)
} else if o == nil {
return errors.New("Can not pass nil")
}
if err := s.doDry(req); err != nil {
return err
}
resp, err := s.Client.Do(req)
if err != nil {
return errors.New(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("Expected status code %d, got %d.\n%s\n", http.StatusOK, resp.StatusCode, body)
} else if err := json.NewDecoder(resp.Body).Decode(o); err != nil {
return errors.New(err)
}
return nil
}
func (s *SuperAgent) Create(o interface{}) error {
return s.send("POST", o, o)
}
func (s *SuperAgent) POST(in, out interface{}) error {
return s.send("POST", in, out)
}
func (s *SuperAgent) Update(o interface{}) error {
return s.send("PUT", o, o)
}
func (s *SuperAgent) send(method string, in interface{}, out interface{}) error {
if s.Client == nil {
s.Client = http.DefaultClient
}
data, err := json.Marshal(in)
if err != nil {
return errors.New(err)
}
req, err := http.NewRequest(method, s.URL, bytes.NewReader(data))
if err != nil {
return errors.New(err)
}
req.Header.Set("Content-Type", "application/json")
if err := s.doDry(req); err != nil {
return err
}
resp, err := s.Client.Do(req)
if err != nil {
return errors.New(err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("Expected 2xx status code but got %d.\n%s", resp.StatusCode, body)
} else if err := json.NewDecoder(resp.Body).Decode(out); err != nil {
body, _ := ioutil.ReadAll(resp.Body)
return errors.Errorf("%s: %s", err, body)
}
return nil
}