-
Notifications
You must be signed in to change notification settings - Fork 0
/
payload.go
165 lines (139 loc) · 3.89 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
154
155
156
157
158
159
160
161
162
163
164
165
package client
import (
"bytes"
"encoding/json"
"io"
"net/http"
"strconv"
// Packages
"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
}
///////////////////////////////////////////////////////////////////////////////
// GLOBALS
var (
MethodGet = NewRequestEx(http.MethodGet, ContentTypeAny)
MethodDelete = NewRequestEx(http.MethodDelete, ContentTypeAny)
MethodPut = NewRequestEx(http.MethodPut, ContentTypeAny)
)
///////////////////////////////////////////////////////////////////////////////
// LIFECYCLE
// Return a new empty request which defaults to GET
func NewRequest() Payload {
return NewRequestEx(http.MethodGet, ContentTypeAny)
}
// Return a new empty request. The accept parameter is the accepted mime-type
// of the response.
func NewRequestEx(method, accept string) Payload {
this := new(request)
this.method = method
this.accept = accept
return this
}
// Return a new request with a JSON payload which defaults to POST.
func NewJSONRequest(payload any) (Payload, error) {
return NewJSONRequestEx(http.MethodPost, payload, ContentTypeAny)
}
// Return a new request with a JSON payload with method. The accept
// parameter is the accepted mime-type of the response.
func NewJSONRequestEx(method string, payload any, accept string) (Payload, error) {
this := new(request)
this.method = method
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) (Payload, 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) (Payload, 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.Type())
}
return str + ">"
}
///////////////////////////////////////////////////////////////////////////////
// PAYLOAD METHODS
// 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 {
if req.accept == "" {
return ContentTypeAny
} else {
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
}
}