forked from saltbo/gopkg
/
resty.go
55 lines (41 loc) · 1.21 KB
/
resty.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
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package httputil
import (
"encoding/json"
"net/http"
"github.com/go-resty/resty/v2"
)
type Resty struct {
*resty.Client
baseURL string
}
func NewResty(baseURL string) *Resty {
return &Resty{Client: resty.New().SetDebug(true), baseURL: baseURL}
}
func (x *Resty) Get(resource string, v interface{}) error {
resp, err := x.R().Get(x.baseURL + resource)
if err := x.formatError(resp, err); err != nil {
return err
}
return json.Unmarshal(resp.Body(), v)
}
func (x *Resty) Create(resource string, body interface{}) error {
return x.formatError(x.R().SetBody(body).Post(x.baseURL + resource))
}
func (x *Resty) Update(resource string, body interface{}) error {
return x.formatError(x.R().SetBody(body).Put(x.baseURL + resource))
}
func (x *Resty) Delete(resource string) error {
return x.formatError(x.R().Delete(x.baseURL + resource))
}
func (x *Resty) formatError(response *resty.Response, err error) error {
if err != nil {
return err
}
if response.StatusCode() != http.StatusOK {
return BindJSONResponse(response.Body()).Error()
}
return nil
}