forked from RobertWHurst/Zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
113 lines (94 loc) · 3.29 KB
/
client.go
File metadata and controls
113 lines (94 loc) · 3.29 KB
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
package zephyr
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"github.com/RobertWHurst/navaros"
"github.com/telemetrytv/trace"
)
var (
clientRequestDebug = trace.Bind("zephyr:client:request")
clientServeDebug = trace.Bind("zephyr:client:serve")
)
// Client can make requests to services.
type Client struct {
Transport Transport
}
// NewClient creates a new client with the given transport.
func NewClient(transport Transport) *Client {
return &Client{
Transport: transport,
}
}
// Service returns a ServiceClient for the given service name.
func (c *Client) Service(name string) *ServiceClient {
return &ServiceClient{
Client: c,
Name: name,
}
}
// ServiceClient is a client that can make requests to a specific service.
// To get a ServiceClient for a specific service, call Service on a Client.
type ServiceClient struct {
*Client
Name string
}
// Do sends an HTTP request to the service.
func (c *ServiceClient) Do(req *http.Request) (*http.Response, error) {
clientRequestDebug.Tracef("Request to %s: %s %s", c.Name, req.Method, req.URL.Path)
responseRecorder := httptest.NewRecorder()
if err := c.Transport.Dispatch(c.Name, responseRecorder, req); err != nil {
clientRequestDebug.Tracef("Request to %s failed: %v", c.Name, err)
return nil, err
}
response := responseRecorder.Result()
clientRequestDebug.Tracef("Response from %s: status=%d, contentLength=%d",
c.Name, response.StatusCode, response.ContentLength)
return response, nil
}
// Get sends a GET request to the service.
func (c *ServiceClient) Get(servicePath string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodGet, servicePath, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// Head sends a HEAD request to the service.
func (c *ServiceClient) Head(servicePath string) (*http.Response, error) {
req, err := http.NewRequest(http.MethodHead, servicePath, nil)
if err != nil {
return nil, err
}
return c.Do(req)
}
// Post sends a POST request to the service.
func (c *ServiceClient) Post(servicePath string, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequest(http.MethodPost, servicePath, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}
// PostForm sends a POST request with form data to the service.
func (c *ServiceClient) PostForm(servicePath string, data url.Values) (*http.Response, error) {
return c.Post(servicePath, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
// ServeHTTP implements http.Handler. It allows a ServiceClient to proxy
// requests to a service.
func (c *ServiceClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
clientServeDebug.Tracef("ServiceClient %s handling HTTP request: %s %s", c.Name, r.Method, r.URL.Path)
if err := c.Transport.Dispatch(c.Name, w, r); err != nil {
clientServeDebug.Tracef("Error dispatching request to %s: %v", c.Name, err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
clientServeDebug.Tracef("Completed handling request to %s: %s %s", c.Name, r.Method, r.URL.Path)
}
// Handle implements navaros.Handler. It allows a ServiceClient to proxy
// requests to a service.
func (c *ServiceClient) Handle(ctx *navaros.Context) {
c.ServeHTTP(ctx.ResponseWriter(), ctx.Request())
}