This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
/
endpoint.go
68 lines (58 loc) · 1.46 KB
/
endpoint.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
package httpie
import (
"net/url"
"net/http"
"io/ioutil"
"bytes"
)
// Endpoint is implemented to provide delayed
// attachment of the URL/Method/Body of a request
type Endpoint interface {
ApplyTo(*http.Request)
}
// Represents an HTTP GET
type Get struct {
*url.URL
}
// ApplyTo sets the requests Method to GET and URL
func (g Get) ApplyTo(req *http.Request) {
req.Method = "GET"
req.URL = g.URL
}
// Represents an HTTP POST
type Post struct {
*url.URL
Body []byte
ContentType string
}
// ApplyTo sets the requests Method to POST, URL and Body
func (p Post) ApplyTo(req *http.Request) {
req.Method = "POST"
req.URL = p.URL
req.Body = ioutil.NopCloser(bytes.NewBuffer(p.Body))
req.ContentLength = int64(len(p.Body))
req.Header.Set("Content-Type", p.ContentType)
}
// Represents an HTTP PUT
type Put struct {
*url.URL
Body []byte
ContentType string
}
// ApplyTo sets the requests Method to PUT, URL and Body
func (p Put) ApplyTo(req *http.Request) {
req.Method = "PUT"
req.URL = p.URL
req.Body = ioutil.NopCloser(bytes.NewBuffer(p.Body))
req.ContentLength = int64(len(p.Body))
req.Header.Set("Content-Type", p.ContentType)
}
// Represents an HTTP DELETE
type Delete struct {
*url.URL
}
// ApplyTo sets the requests Method to DELETE and URL
func (d Delete) ApplyTo(req *http.Request) {
req.Method = "DELETE"
req.URL = d.URL
}