11// Builder allows users to construct a request and then execute the
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
25 context context.Context
33// New provides a new Builder for the given endpoint.
34func New(endpoint string) Builder {
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 {
48// WithBody adds a body to the request.
49func (r *builder) WithBody(body io.Reader) Builder {
54// WithMethod sets the request method. Defaults to "GET".
55func (r *builder) WithMethod(method string) Builder {
60// WithHeaders replaces the request header map with the given header map.
61func (r *builder) WithHeaders(header http.Header) Builder {
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 {
70 r.header = make(http.Header)
72 r.header.Set(key, value)
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 {
81 // Request has already been done
85 // Must provide a non-nil context to NewRequestWithContext
87 r.context = context.Background()
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)
98 r.result = &result{err: fmt.Errorf("error creating request: %v", err)}
101 req.Header = r.header
103 resp, err := http.DefaultClient.Do(req)
105 r.result = &result{err: fmt.Errorf("error performing request: %v", err)}
109 defer resp.Body.Close()
110 body, err := ioutil.ReadAll(resp.Body)
112 r.result = &result{err: fmt.Errorf("error reading response body: %v", err)}
116 r.result = &result{response: resp, body: body}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list