-
Notifications
You must be signed in to change notification settings - Fork 64
/
client.go
108 lines (95 loc) · 2.89 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
package link_manager_client
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
import (
"context"
httptransport "github.com/go-kit/kit/transport/http"
om "github.com/the-gigi/delinkcious/pkg/object_model"
)
func NewClient(baseURL string) (om.LinkManager, error) {
// Quickly sanitize the instance string.
if !strings.HasPrefix(baseURL, "http") {
baseURL = "http://" + baseURL
}
u, err := url.Parse(baseURL)
if err != nil {
return nil, err
}
getLinksEndpoint := httptransport.NewClient(
"GET",
copyURL(u, "/links"),
encodeGetLinksRequest,
decodeGetLinksResponse).Endpoint()
addLinkEndpoint := httptransport.NewClient(
"POST",
copyURL(u, "/links"),
encodeHTTPGenericRequest,
decodeSimpleResponse).Endpoint()
updateLinkEndpoint := httptransport.NewClient(
"PUT",
copyURL(u, "/links"),
encodeHTTPGenericRequest,
decodeSimpleResponse).Endpoint()
deleteLinkEndpoint := httptransport.NewClient(
"DELETE",
copyURL(u, "/links"),
encodeDeleteLinkRequest,
decodeSimpleResponse).Endpoint()
// Returning the EndpointSet as an interface relies on the
// EndpointSet implementing the Service methods. That's just a simple bit
// of glue code.
return EndpointSet{
GetLinksEndpoint: getLinksEndpoint,
AddLinkEndpoint: addLinkEndpoint,
UpdateLinkEndpoint: updateLinkEndpoint,
DeleteLinkEndpoint: deleteLinkEndpoint,
}, nil
}
func copyURL(base *url.URL, path string) *url.URL {
next := *base
next.Path = path
return &next
}
// encodeHTTPGenericRequest is a transport/http.EncodeRequestFunc that
// JSON-encodes any request to the request body. Primarily useful in a client.
func encodeHTTPGenericRequest(_ context.Context, r *http.Request, request interface{}) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(request); err != nil {
return err
}
r.Body = ioutil.NopCloser(&buf)
return nil
}
// Extract the request details from the incoming request and add them as query arguments
func encodeGetLinksRequest(ctx context.Context, req *http.Request, request interface{}) error {
r := request.(om.GetLinksRequest)
urlRegex := url.QueryEscape(r.UrlRegex)
titleRegex := url.QueryEscape(r.TitleRegex)
descriptionRegex := url.QueryEscape(r.DescriptionRegex)
username := url.QueryEscape(r.Username)
tag := url.QueryEscape(r.Tag)
startToken := url.QueryEscape(r.StartToken)
q := req.URL.Query()
q.Add("url", urlRegex)
q.Add("title", titleRegex)
q.Add("description", descriptionRegex)
q.Add("username", username)
q.Add("tag", tag)
q.Add("start", startToken)
req.URL.RawQuery = q.Encode()
return encodeHTTPGenericRequest(ctx, req, request)
}
func encodeDeleteLinkRequest(ctx context.Context, req *http.Request, request interface{}) error {
r := request.(*deleteLinkRequest)
q := req.URL.Query()
q.Add("username", r.Username)
q.Add("url", r.Url)
req.URL.RawQuery = q.Encode()
return encodeHTTPGenericRequest(ctx, req, request)
}