-
Notifications
You must be signed in to change notification settings - Fork 8
/
client_http.go
136 lines (116 loc) · 2.89 KB
/
client_http.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
129
130
131
132
133
134
135
136
package allsrv
import (
"context"
"net/http"
"time"
"github.com/jsteenb2/errors"
"github.com/jsteenb2/allsrvc"
)
type ClientHTTP struct {
c *allsrvc.ClientHTTP
}
var _ SVC = (*ClientHTTP)(nil)
func NewClientHTTP(addr, origin string, c *http.Client, opts ...func(*allsrvc.ClientHTTP)) *ClientHTTP {
return &ClientHTTP{
c: allsrvc.NewClientHTTP(addr, origin, c, opts...),
}
}
func (c *ClientHTTP) CreateFoo(ctx context.Context, f Foo) (Foo, error) {
resp, err := c.c.CreateFoo(ctx, allsrvc.FooCreateAttrs{
Name: f.Name,
Note: f.Note,
})
if err != nil {
return Foo{}, InternalErr(err.Error())
}
newFoo, err := takeRespFoo(resp)
return newFoo, errors.Wrap(err)
}
func (c *ClientHTTP) ReadFoo(ctx context.Context, id string) (Foo, error) {
resp, err := c.c.ReadFoo(ctx, id)
if err != nil {
if errors.Is(err, allsrvc.ErrIDRequired) {
return Foo{}, errIDRequired
}
}
newFoo, err := takeRespFoo(resp)
return newFoo, errors.Wrap(err)
}
func (c *ClientHTTP) UpdateFoo(ctx context.Context, f FooUpd) (Foo, error) {
resp, err := c.c.UpdateFoo(ctx, f.ID, allsrvc.FooUpdAttrs{
Name: f.Name,
Note: f.Note,
})
if err != nil {
return Foo{}, InternalErr(err.Error())
}
newFoo, err := takeRespFoo(resp)
return newFoo, errors.Wrap(err)
}
func (c *ClientHTTP) DelFoo(ctx context.Context, id string) error {
resp, err := c.c.DelFoo(ctx, id)
if err != nil {
if errors.Is(err, allsrvc.ErrIDRequired) {
return errIDRequired
}
}
return errors.Wrap(convertSDKErrors(resp.Errs))
}
func DataToFoo(data allsrvc.Data[allsrvc.ResourceFooAttrs]) Foo {
return Foo{
ID: data.ID,
Name: data.Attrs.Name,
Note: data.Attrs.Note,
CreatedAt: toTime(data.Attrs.CreatedAt),
UpdatedAt: toTime(data.Attrs.UpdatedAt),
}
}
func takeRespFoo(respBody allsrvc.RespBody[allsrvc.ResourceFooAttrs]) (Foo, error) {
if err := convertSDKErrors(respBody.Errs); err != nil {
return Foo{}, errors.Wrap(err)
}
if respBody.Data == nil {
return Foo{}, nil
}
return DataToFoo(*respBody.Data), nil
}
func convertSDKErrors(errs []allsrvc.RespErr) error {
// TODO(@berg): update this to slices pkg when 1.23 lands
switch out := toSlc(errs, toErr); {
case len(out) == 1:
return out[0]
case len(out) > 1:
return errors.Join(out)
default:
return nil
}
}
func toErr(respErr allsrvc.RespErr) error {
errFn := InternalErr
switch respErr.Code {
case errCodeExist:
errFn = ExistsErr
case errCodeInvalid:
errFn = InvalidErr
case errCodeNotFound:
errFn = NotFoundErr
case errCodeUnAuthed:
errFn = unauthedErr
}
var fields []any
if respErr.Source != nil {
fields = append(fields, "err_source", *respErr.Source)
}
return errFn(respErr.Msg, fields...)
}
func toTime(in string) time.Time {
t, _ := time.Parse(time.RFC3339, in)
return t
}
func toSlc[In, Out any](in []In, to func(In) Out) []Out {
out := make([]Out, len(in))
for _, v := range in {
out = append(out, to(v))
}
return out
}