git.iko.soy/typeable/oauth2-proxy
on reference-generator help sign in
118 lines 2.9 kB history raw
1package requests
3import (
4 "context"
5 "fmt"
6 "io"
7 "io/ioutil"
8 "net/http"
9)
11// Builder allows users to construct a request and then execute the
12// request via Do().
13// Do returns a Result which allows the user to get the body,
14// unmarshal the body into an interface, or into a simplejson.Json.
15type Builder interface {
16 WithContext(context.Context) Builder
17 WithBody(io.Reader) Builder
18 WithMethod(string) Builder
19 WithHeaders(http.Header) Builder
20 SetHeader(key, value string) Builder
21 Do() Result
24type builder struct {
25 context context.Context
26 method string
27 endpoint string
28 body io.Reader
29 header http.Header
30 result *result
33// New provides a new Builder for the given endpoint.
34func New(endpoint string) Builder {
35 return &builder{
36 endpoint: endpoint,
37 method: "GET",
38 }
41// WithContext adds a context to the request.
42// If no context is provided, context.Background() is used instead.
43func (r *builder) WithContext(ctx context.Context) Builder {
44 r.context = ctx
45 return r
48// WithBody adds a body to the request.
49func (r *builder) WithBody(body io.Reader) Builder {
50 r.body = body
51 return r
54// WithMethod sets the request method. Defaults to "GET".
55func (r *builder) WithMethod(method string) Builder {
56 r.method = method
57 return r
60// WithHeaders replaces the request header map with the given header map.
61func (r *builder) WithHeaders(header http.Header) Builder {
62 r.header = header
63 return r
66// SetHeader sets a single header to the given value.
67// May be used to add multiple headers.
68func (r *builder) SetHeader(key, value string) Builder {
69 if r.header == nil {
70 r.header = make(http.Header)
71 }
72 r.header.Set(key, value)
73 return r
76// Do performs the request and returns the response in its raw form.
77// If the request has already been performed, returns the previous result.
78// This will not allow you to repeat a request.
79func (r *builder) Do() Result {
80 if r.result != nil {
81 // Request has already been done
82 return r.result
83 }
85 // Must provide a non-nil context to NewRequestWithContext
86 if r.context == nil {
87 r.context = context.Background()
88 }
90 return r.do()
93// do creates the request, executes it with the default client and extracts the
94// the body into the response
95func (r *builder) do() Result {
96 req, err := http.NewRequestWithContext(r.context, r.method, r.endpoint, r.body)
97 if err != nil {
98 r.result = &result{err: fmt.Errorf("error creating request: %v", err)}
99 return r.result
100 }
101 req.Header = r.header
103 resp, err := http.DefaultClient.Do(req)
104 if err != nil {
105 r.result = &result{err: fmt.Errorf("error performing request: %v", err)}
106 return r.result
107 }
109 defer resp.Body.Close()
110 body, err := ioutil.ReadAll(resp.Body)
111 if err != nil {
112 r.result = &result{err: fmt.Errorf("error reading response body: %v", err)}
113 return r.result
114 }
116 r.result = &result{response: resp, body: body}
117 return r.result