-
Notifications
You must be signed in to change notification settings - Fork 6
/
collection.go
102 lines (90 loc) · 2.65 KB
/
collection.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
package restli
import (
"context"
"net/http"
"github.com/PapaCharlie/go-restli/restlicodec"
"github.com/PapaCharlie/go-restli/restlidata"
)
// Create executes a rest.li create request with the given object. The X-RestLi-Id header field will be parsed into id
// (though a CreateResponseHasNoEntityHeaderError will be returned if the header is not set). The response body will
// always be ignored.
func Create[K any, V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
create V,
query QueryParamsEncoder,
readOnlyFields restlicodec.PathSpec,
) (*restlidata.CreatedEntity[K], error) {
req, err := NewCreateRequest(c, ctx, rp, query, Method_create, create, readOnlyFields)
if err != nil {
return nil, err
}
res, err := DoAndIgnore(c, req)
if err != nil {
return nil, err
}
return unmarshalReturnEntityKey[K](c, res)
}
// CreateWithReturnEntity is like CollectionClient.Create, except it parses the returned entity from the response.
func CreateWithReturnEntity[K any, V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
create V,
query QueryParamsEncoder,
readOnlyFields restlicodec.PathSpec,
) (*restlidata.CreatedAndReturnedEntity[K, V], error) {
req, err := NewCreateRequest(c, ctx, rp, query, Method_create, create, readOnlyFields)
if err != nil {
return nil, err
}
v, res, err := DoAndUnmarshal(c, req, restlicodec.UnmarshalRestLi[V])
if err != nil {
return nil, err
}
k, err := unmarshalReturnEntityKey[K](c, res)
if err != nil {
return nil, err
}
return &restlidata.CreatedAndReturnedEntity[K, V]{
CreatedEntity: *k,
Entity: v,
}, nil
}
func unmarshalReturnEntityKey[K any](c *Client, res *http.Response) (result *restlidata.CreatedEntity[K], err error) {
if h := res.Header.Get(IDHeader); len(h) > 0 {
var reader restlicodec.Reader
reader, err = restlicodec.NewRor2Reader(h)
if err != nil {
return nil, err
}
var k K
k, err = restlicodec.UnmarshalRestLi[K](reader)
if _, mfe := err.(*restlicodec.MissingRequiredFieldsError); mfe && !c.StrictResponseDeserialization {
err = nil
}
if err != nil {
return nil, err
}
return &restlidata.CreatedEntity[K]{
Id: k,
Status: res.StatusCode,
}, nil
} else {
return nil, &CreateResponseHasNoEntityHeaderError{Response: res}
}
}
func GetAll[V restlicodec.Marshaler](
c *Client,
ctx context.Context,
rp ResourcePath,
query QueryParamsEncoder,
) (results *restlidata.Elements[V], err error) {
req, err := NewGetRequest(c, ctx, rp, query, Method_get_all)
if err != nil {
return nil, err
}
results, _, err = DoAndUnmarshal(c, req, restlicodec.UnmarshalRestLi[*restlidata.Elements[V]])
return results, err
}