forked from nathankerr/rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
162 lines (127 loc) · 3.38 KB
/
client.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
package rest
import (
"bytes"
"io"
"net"
"net/http"
"net/http/httputil"
"net/url"
)
type Client struct {
conn *httputil.ClientConn
resource *url.URL
}
// Creates a client for the specified resource...doesn't work with weekly
//
// The resource is the url to the base of the resource (i.e.,
// http://127.0.0.1:3000/snips/)
func NewClient(resource string) (*Client, error) {
var client = new(Client)
var err error
// setup host
if client.resource, err = url.Parse(resource); err != nil {
return nil, err
}
// Setup conn
var tcpConn net.Conn
if tcpConn, err = net.Dial("tcp", client.resource.Host); err != nil {
return nil, err
}
client.conn = httputil.NewClientConn(tcpConn, nil)
return client, nil
}
// Closes the clients connection
func (client *Client) Close() {
client.conn.Close()
}
// General Request method used by the specialized request methods to create a request
func (client *Client) newRequest(method string, id string) (*http.Request, error) {
request := new(http.Request)
var err error
request.ProtoMajor = 1
request.ProtoMinor = 1
request.TransferEncoding = []string{"chunked"}
request.Method = method
// Generate Resource-URI and parse it
targeturl := client.resource.String() + id
if request.URL, err = url.Parse(targeturl); err != nil {
return nil, err
}
return request, nil
}
// Send a request
func (client *Client) Request(request *http.Request) (*http.Response, error) {
var err error
var response *http.Response
// Send the request
if err = client.conn.Write(request); err != nil {
return nil, err
}
// Read the response
if response, err = client.conn.Read(request); err != nil {
return nil, err
}
return response, nil
}
// GET /resource/
func (client *Client) Index() (*http.Response, error) {
var request *http.Request
var err error
if request, err = client.newRequest("GET", ""); err != nil {
return nil, err
}
return client.Request(request)
}
// GET /resource/id
func (client *Client) Find(id string) (*http.Response, error) {
var request *http.Request
var err error
if request, err = client.newRequest("GET", id); err != nil {
return nil, err
}
return client.Request(request)
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error {
return nil
}
// POST /resource
func (client *Client) Create(body string) (*http.Response, error) {
var request *http.Request
var err error
if request, err = client.newRequest("POST", ""); err != nil {
return nil, err
}
request.Body = nopCloser{bytes.NewBufferString(body)}
return client.Request(request)
}
// PUT /resource/id
func (client *Client) Update(id string, body string) (*http.Response, error) {
var request *http.Request
var err error
if request, err = client.newRequest("PUT", id); err != nil {
return nil, err
}
request.Body = nopCloser{bytes.NewBufferString(body)}
return client.Request(request)
}
// Parse a response-Location-URI to get the ID of the worked-on snip
func (client *Client) IdFromURL(urlString string) (string, error) {
var url *url.URL
var err error
if url, err = url.Parse(urlString); err != nil {
return "", err
}
return string(url.Path[len(client.resource.Path):]), nil
}
// DELETE /resource/id
func (client *Client) Delete(id string) (*http.Response, error) {
var request *http.Request
var err error
if request, err = client.newRequest("DELETE", id); err != nil {
return nil, err
}
return client.Request(request)
}