/
client.go
37 lines (33 loc) · 992 Bytes
/
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"gopkg.in/errgo.v1"
"gopkg.in/macaroon-bakery.v3/httpbakery"
)
// client represents a client of the target service.
// In this simple example, it just tries a GET
// request, which will fail unless the client
// has the required authorization.
func clientRequest(client *httpbakery.Client, serverEndpoint string) (string, error) {
// The Do function implements the mechanics
// of actually gathering discharge macaroons
// when required, and retrying the request
// when necessary.
req, err := http.NewRequest("GET", serverEndpoint, nil)
if err != nil {
return "", errgo.Notef(err, "cannot make new HTTP request")
}
resp, err := client.Do(req)
if err != nil {
return "", errgo.NoteMask(err, "GET failed", errgo.Any)
}
defer resp.Body.Close()
// TODO(rog) unmarshal error
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("cannot read response: %v", err)
}
return string(data), nil
}