Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RawRequest to Response object #62

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func escapeQuotes(s string) string {
}

// buildRequest is where most of the magic happens for request processing
func buildRequest(httpMethod, url string, ro *RequestOptions, httpClient *http.Client) (*http.Response, error) {
func buildRequest(httpMethod, url string, ro *RequestOptions, httpClient *http.Client) (req *http.Request, resp *http.Response, err error) {
if ro == nil {
ro = &RequestOptions{}
}
Expand All @@ -162,23 +162,22 @@ func buildRequest(httpMethod, url string, ro *RequestOptions, httpClient *http.C
httpClient = BuildHTTPClient(*ro)
}

var err error // we don't want to shadow url so we won't use :=
switch {
case len(ro.Params) != 0:
if url, err = buildURLParams(url, ro.Params); err != nil {
return nil, err
return
}
case ro.QueryStruct != nil:
if url, err = buildURLStruct(url, ro.QueryStruct); err != nil {
return nil, err
return
}
}

// Build the request
req, err := buildHTTPRequest(httpMethod, url, ro)
req, err = buildHTTPRequest(httpMethod, url, ro)

if err != nil {
return nil, err
return
}

// Do we need to add any HTTP headers or Basic Auth?
Expand All @@ -191,7 +190,8 @@ func buildRequest(httpMethod, url string, ro *RequestOptions, httpClient *http.C
req = req.WithContext(ro.Context)
}

return httpClient.Do(req)
resp, err = httpClient.Do(req)
return
}

func buildHTTPRequest(httpMethod, userURL string, ro *RequestOptions) (*http.Request, error) {
Expand Down
8 changes: 6 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Response struct {
// This is the Go error flag – if something went wrong within the request, this flag will be set.
Error error

// RawRequest is the http.Request that was sent to obtain this Response.
RawRequest *http.Request

// We want to abstract (at least at the moment) the Go http.Response object away. So we are going to make use of it
// internal but not give the user access
RawResponse *http.Response
Expand All @@ -32,16 +35,17 @@ type Response struct {
internalByteBuffer *bytes.Buffer
}

func buildResponse(resp *http.Response, err error) (*Response, error) {
func buildResponse(req *http.Request, resp *http.Response, err error) (*Response, error) {
// If the connection didn't succeed we just return a blank response
if err != nil {
return &Response{Error: err}, err
return &Response{Error: err, RawRequest: req}, err
}

goodResp := &Response{
// If your code is within the 2xx range – the response is considered `Ok`
Ok: resp.StatusCode >= 200 && resp.StatusCode < 300,
Error: nil,
RawRequest: req,
RawResponse: resp,
StatusCode: resp.StatusCode,
Header: resp.Header,
Expand Down