elastigo icon indicating copy to clipboard operation
elastigo copied to clipboard

Expose Http Response in api

Open araddon opened this issue 11 years ago • 3 comments

The current design of elastigo requests hide the http.Response (and its http status) returning json. This makes error handling problematic. https://github.com/mattbaird/elastigo/pull/21#issuecomment-20821385

Propose to: add a new DoResponse method https://github.com/mattbaird/elastigo/blob/master/api/request.go#L86

araddon avatar Jul 21 '13 16:07 araddon

Are you proposing something like this:

func (r *Request) Do(v interface{}) (int, []byte, error) {
    response, bodyBytes, e := r.DoResponse(v)
    return response.StatusCode, bodyBytes, e
}

func (r *Request) DoResponse(v interface{}) (http.Response, []byte, error) {
    res, err := http.DefaultClient.Do((*http.Request)(r))
    if err != nil {
        return *res, nil, err
    }

    defer res.Body.Close()
    bodyBytes, err := ioutil.ReadAll(res.Body)

    if res.StatusCode > 304 && v != nil {
        jsonErr := json.Unmarshal(bodyBytes, v)
        if jsonErr != nil {
            return *res, nil, err
        }
    }
    return *res, bodyBytes, err
}

mattbaird avatar Jul 22 '13 03:07 mattbaird

Something like that, let me go through my uses and verify, I think we also need one that doesn't do the unmarshalling.

araddon avatar Jul 22 '13 15:07 araddon

Yah, smart layering of the functions, I like it. Doesn't add much complexity, and gives you options for debugging and extension. I can do that.

On Mon, Jul 22, 2013 at 8:45 AM, Aaron Raddon [email protected]:

Something like that, let me go through my uses and verify, I think we also need one that doesn't do the unmarshalling.

— Reply to this email directly or view it on GitHubhttps://github.com/mattbaird/elastigo/issues/26#issuecomment-21353326 .

mattbaird avatar Jul 22 '13 16:07 mattbaird