-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrequest.go
51 lines (42 loc) · 910 Bytes
/
request.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
package jsonapi
import (
"io/ioutil"
"net/http"
)
// NewRequest builds and returns a *Request based on r and schema.
//
// schema can be nil, in which case no checks will be done to insure that the
// request respects a specific schema.
func NewRequest(r *http.Request, schema *Schema) (*Request, error) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
su, err := NewSimpleURL(r.URL)
if err != nil {
return nil, err
}
url, err := NewURL(schema, su)
if err != nil {
return nil, err
}
var doc *Document
if r.Method == http.MethodPost || r.Method == http.MethodPatch {
doc, err = UnmarshalDocument(body, schema)
if err != nil {
return nil, err
}
}
req := &Request{
Method: r.Method,
URL: url,
Doc: doc,
}
return req, nil
}
// A Request represents a JSON:API request.
type Request struct {
Method string
URL *URL
Doc *Document
}