-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
153 lines (129 loc) · 3.47 KB
/
payload.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package client
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/mutablelogic/go-client/pkg/multipart"
)
///////////////////////////////////////////////////////////////////////////////
// TYPES
type Request struct {
method string
accept string
mimetype string
buffer *bytes.Buffer
}
type Payload interface {
io.Reader
Method() string
Accept() string
Type() string
}
///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
// Return a new empty request which defaults to GET. The accept parameter is the
// accepted mime-type of the response.
func NewRequest(accept string) *Request {
this := new(Request)
this.method = http.MethodGet
this.accept = accept
return this
}
// Return a new request with a JSON payload which defaults to GET. The accept
// parameter is the accepted mime-type of the response.
func NewJSONRequest(payload any, accept string) (*Request, error) {
this := new(Request)
this.method = http.MethodGet
this.mimetype = ContentTypeJson
this.accept = accept
this.buffer = new(bytes.Buffer)
if err := json.NewEncoder(this.buffer).Encode(payload); err != nil {
return nil, err
}
return this, nil
}
// Return a new request with a Multipart Form data payload which defaults to POST. The accept
// parameter is the accepted mime-type of the response.
func NewMultipartRequest(payload any, accept string) (*Request, error) {
this := new(Request)
this.method = http.MethodPost
this.accept = accept
this.buffer = new(bytes.Buffer)
// Encode the payload
enc := multipart.NewMultipartEncoder(this.buffer)
defer enc.Close()
if err := enc.Encode(payload); err != nil {
return nil, err
} else {
this.mimetype = enc.ContentType()
}
// Return success
return this, nil
}
// Return a new request with a Form data payload which defaults to POST. The accept
// parameter is the accepted mime-type of the response.
func NewFormRequest(payload any, accept string) (*Request, error) {
this := new(Request)
this.method = http.MethodPost
this.accept = accept
this.buffer = new(bytes.Buffer)
// Encode the payload
enc := multipart.NewFormEncoder(this.buffer)
defer enc.Close()
if err := enc.Encode(payload); err != nil {
return nil, err
} else {
this.mimetype = enc.ContentType()
}
// Return success
return this, nil
}
///////////////////////////////////////////////////////////////////////////////
// STRINGIFY
func (req *Request) String() string {
str := "<payload"
if req.method != "" {
str += " method=" + strconv.Quote(req.method)
}
if req.accept != "" {
str += " accept=" + strconv.Quote(req.accept)
}
if req.mimetype != "" {
str += " mimetype=" + strconv.Quote(req.mimetype)
}
return str + ">"
}
///////////////////////////////////////////////////////////////////////////////
// PAYLOAD METHODS
// Set the HTTP method to POST
func (req *Request) Post() *Request {
req.method = http.MethodPost
return req
}
// Set the HTTP method to DELETE
func (req *Request) Delete() *Request {
req.method = http.MethodDelete
return req
}
// Return the HTTP method
func (req *Request) Method() string {
return req.method
}
// Set the request mimetype
func (req *Request) Type() string {
return req.mimetype
}
// Return the acceptable mimetype responses
func (req *Request) Accept() string {
return req.accept
}
// Implements the io.Reader interface for a payload
func (req *Request) Read(b []byte) (n int, err error) {
if req.buffer != nil {
return req.buffer.Read(b)
} else {
return 0, io.EOF
}
}