-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
101 lines (84 loc) · 3.44 KB
/
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
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
package httpx
import (
"context"
"fmt"
"io"
"net/http"
)
var NoBody = http.NoBody
// NewRequest returns a new http.Request.
func NewRequest(ctx context.Context, method, url string, body io.Reader) (*http.Request, error) {
return http.NewRequestWithContext(ctx, method, url, body)
}
// NewGetRequest returns a new http.Request with GET method.
func NewGetRequest(ctx context.Context, url string) (*http.Request, error) {
return NewRequest(ctx, http.MethodGet, url, http.NoBody)
}
// NewHeadRequest returns a new http.Request with HEAD method.
func NewHeadRequest(ctx context.Context, url string) (*http.Request, error) {
return NewRequest(ctx, http.MethodHead, url, http.NoBody)
}
// NewPostRequest returns a new http.Request with POST method.
func NewPostRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodPost, url, body)
}
// NewPutRequest returns a new http.Request with PUT method.
func NewPutRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodPut, url, body)
}
// NewPatchRequest returns a new http.Request with PATCH method.
func NewPatchRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodPatch, url, body)
}
// NewDeleteRequest returns a new http.Request with DELETE method.
func NewDeleteRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodDelete, url, body)
}
// NewConnectRequest returns a new http.Request with CONNECT method.
func NewConnectRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodConnect, url, body)
}
// NewOptionsRequest returns a new http.Request with OPTIONS method.
func NewOptionsRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodOptions, url, body)
}
// NewTraceRequest returns a new http.Request with TRACE method.
func NewTraceRequest(ctx context.Context, url string, body io.Reader) (*http.Request, error) {
return NewRequest(ctx, http.MethodTrace, url, body)
}
// MustNewRequest returns a new http.Request or panics on error.
func MustNewRequest(ctx context.Context, method, url string, body io.Reader) *http.Request {
req, err := NewRequest(ctx, method, url, body)
if err != nil {
panic(fmt.Sprintf("httpx: must create request: %v", err))
}
return req
}
// MustGetRequest returns a new http.Request with GET method or panics on error.
func MustGetRequest(ctx context.Context, url string) *http.Request {
req, err := NewGetRequest(ctx, url)
if err != nil {
panic(fmt.Sprintf("httpx: must create GET request: %v", err))
}
return req
}
// MustPostRequest returns a new http.Request with POST method or panics on error.
func MustPostRequest(ctx context.Context, url string, body io.Reader) *http.Request {
req, err := NewPostRequest(ctx, url, body)
if err != nil {
panic(fmt.Sprintf("httpx: must create POST request: %v", err))
}
return req
}
// MustPutRequest returns a new http.Request with PUT method or panics on error.
func MustPutRequest(ctx context.Context, url string, body io.Reader) *http.Request {
req, err := NewPutRequest(ctx, url, body)
if err != nil {
panic(fmt.Sprintf("httpx: must create PUT request: %v", err))
}
return req
}
// Bearer header with a give token.
func Bearer(token string) string {
return "Bearer " + token
}