-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.go
More file actions
126 lines (114 loc) · 2.85 KB
/
Copy pathclient.go
File metadata and controls
126 lines (114 loc) · 2.85 KB
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
package http
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/Aize-Public/forego/api"
"github.com/Aize-Public/forego/ctx"
"github.com/Aize-Public/forego/ctx/log"
)
// TODO circuit breaker!
type Client struct {
cli http.Client
BaseUrl *url.URL
// TODO do we need a "proxy" host which will be used instead of the url host?
}
var DefaultClient = Client{}
func (this Client) Get(c ctx.C, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(c, "GET", url, nil)
if err != nil {
return nil, ctx.NewErrorf(c, "can't create request: %w", err)
}
res, err := this.Do(req)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
return io.ReadAll(res.Body)
}
return nil, nil
}
func NewRequest(c ctx.C, method string, url string, body io.Reader) (*http.Request, error) {
return http.NewRequestWithContext(c, method, url, body)
}
func (this Client) Post(c ctx.C, url string, body []byte) ([]byte, error) {
req, err := http.NewRequestWithContext(c, "POST", url, bytes.NewBuffer(body))
if err != nil {
return nil, ctx.NewErrorf(c, "can't create request: %w", err)
}
req.Header.Set(`Content-Type`, `application/json`)
res, err := this.Do(req)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
return io.ReadAll(res.Body)
}
return nil, nil
}
func (this Client) Do(r *http.Request) (*http.Response, error) {
res, err := this.cli.Do(r)
if err != nil {
return res, ctx.NewErrorf(r.Context(), "http.Client.Do: %w", err)
}
switch res.StatusCode {
case 200, 204:
return res, nil
default:
return res, Error{res.StatusCode, fmt.Errorf("%s", res.Status)}
}
}
func (this Client) API(c ctx.C, obj Doable) error {
c = ctx.WithTag(c, "api", fmt.Sprintf("client %T", obj))
h, err := api.NewClient(c, obj)
if err != nil {
return err
}
{
data := &api.JSON{}
err = h.Send(c, obj, data)
if err != nil {
return err
}
j, err := json.Marshal(data.Data)
if err != nil {
return ctx.NewErrorf(c, "can't marshal %T: %w", obj, err)
}
u := h.URL()
if this.BaseUrl != nil {
u, err = this.BaseUrl.Parse(u.String())
if err != nil {
return ctx.NewErrorf(c, "can't rebase url: %w", err)
}
}
req, err := NewRequest(c, "POST", u.String(), bytes.NewBuffer(j))
if err != nil {
return err
}
log.Debugf(c, "client[%T].Send() %s", obj, j)
res, err := this.Do(req)
if err != nil {
return ctx.NewErrorf(c, "can't send request: %w", err)
}
switch res.StatusCode {
case 204:
log.Debugf(c, "204 no response")
return nil
case 200:
err := data.ReadFrom(c, res.Body)
if err != nil {
return ctx.NewErrorf(c, "can't read response: %w", err)
}
res.Body.Close()
log.Debugf(c, "client[%T].Recv() %s", obj, j)
return h.Recv(c, data, obj)
default:
return ctx.NewErrorf(c, "can't connect: %s", res.Status)
}
}
}