From 7304c1eec993ca9bdb602929a28752c813f1b8c7 Mon Sep 17 00:00:00 2001 From: izumin5210 Date: Thu, 3 May 2018 13:50:55 +0900 Subject: [PATCH] Add RawRequest to Response object --- request.go | 14 +++++++------- response.go | 8 ++++++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/request.go b/request.go index f17566c..93d7e1c 100644 --- a/request.go +++ b/request.go @@ -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{} } @@ -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? @@ -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) { diff --git a/response.go b/response.go index e2fce42..2c74187 100644 --- a/response.go +++ b/response.go @@ -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 @@ -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,