elastigo
elastigo copied to clipboard
Expose Http Response in api
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
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
}
Something like that, let me go through my uses and verify, I think we also need one that doesn't do the unmarshalling.
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 .