10// Builder allows users to construct a request and then execute the
12// Do returns a Result which allows the user to get the body,
13// unmarshal the body into an interface, or into a simplejson.Json.
14type Builder interface {
15 WithContext(context.Context) Builder
16 WithBody(io.Reader) Builder
17 WithMethod(string) Builder
18 WithHeaders(http.Header) Builder
19 SetHeader(key, value string) Builder
24 context context.Context
32// New provides a new Builder for the given endpoint.
33func New(endpoint string) Builder {
40// WithContext adds a context to the request.
41// If no context is provided, context.Background() is used instead.
42func (r *builder) WithContext(ctx context.Context) Builder {
47// WithBody adds a body to the request.
48func (r *builder) WithBody(body io.Reader) Builder {
53// WithMethod sets the request method. Defaults to "GET".
54func (r *builder) WithMethod(method string) Builder {
59// WithHeaders replaces the request header map with the given header map.
60func (r *builder) WithHeaders(header http.Header) Builder {
65// SetHeader sets a single header to the given value.
66// May be used to add multiple headers.
67func (r *builder) SetHeader(key, value string) Builder {
69 r.header = make(http.Header)
71 r.header.Set(key, value)
75// Do performs the request and returns the response in its raw form.
76// If the request has already been performed, returns the previous result.
77// This will not allow you to repeat a request.
78func (r *builder) Do() Result {
80 // Request has already been done
84 // Must provide a non-nil context to NewRequestWithContext
86 r.context = context.Background()
92// do creates the request, executes it with the default client and extracts the
93// the body into the response
94func (r *builder) do() Result {
95 req, err := http.NewRequestWithContext(r.context, r.method, r.endpoint, r.body)
97 r.result = &result{err: fmt.Errorf("error creating request: %v", err)}
100 req.Header = r.header
102 resp, err := http.DefaultClient.Do(req)
104 r.result = &result{err: fmt.Errorf("error performing request: %v", err)}
108 defer resp.Body.Close()
109 body, err := io.ReadAll(resp.Body)
111 r.result = &result{err: fmt.Errorf("error reading response body: %v", err)}
115 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