github.com/m-lab/locate@v0.17.6/proxy/proxy.go (about) 1 // Package proxy issues requests to the legacy mlab-ns service and parses responses. 2 package proxy 3 4 import ( 5 "encoding/json" 6 "errors" 7 "io" 8 "net/http" 9 ) 10 11 // TODO(soltesz): move proxy to another package. 12 13 // ErrNoContent is returned when mlab-ns returns http.StatusNoContent. 14 var ErrNoContent = errors.New("no content from server") 15 16 // UnmarshalResponse reads the response from the given request and unmarshals 17 // the value into the given result. 18 func UnmarshalResponse(req *http.Request, result interface{}) (*http.Response, error) { 19 resp, err := http.DefaultClient.Do(req) 20 if err != nil { 21 return resp, err 22 } 23 if resp.StatusCode == http.StatusNoContent { 24 // Cannot unmarshal empty content. 25 return resp, ErrNoContent 26 } 27 defer resp.Body.Close() 28 b, err := io.ReadAll(resp.Body) 29 if err != nil { 30 return resp, err 31 } 32 return resp, json.Unmarshal(b, result) 33 }