2019-05-24 16:55:12 +01:00
|
|
|
package requests
|
2015-03-30 10:23:30 -04:00
|
|
|
|
|
|
|
import (
|
2016-06-23 08:29:44 -04:00
|
|
|
"encoding/json"
|
2015-06-06 14:15:43 -04:00
|
|
|
"fmt"
|
2015-03-30 10:23:30 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/bitly/go-simplejson"
|
2019-05-24 17:08:48 +01:00
|
|
|
"github.com/pusher/oauth2_proxy/pkg/logger"
|
2015-03-30 10:23:30 -04:00
|
|
|
)
|
|
|
|
|
2018-12-20 09:37:02 +00:00
|
|
|
// Request parses the request body into a simplejson.Json object
|
2015-03-30 10:23:30 -04:00
|
|
|
func Request(req *http.Request) (*simplejson.Json, error) {
|
2015-05-20 23:23:48 -04:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2015-03-30 10:23:30 -04:00
|
|
|
if err != nil {
|
2019-02-10 08:37:45 -08:00
|
|
|
logger.Printf("%s %s %s", req.Method, req.URL, err)
|
2015-03-30 10:23:30 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2019-10-23 17:55:34 +01:00
|
|
|
if body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
2019-02-10 08:37:45 -08:00
|
|
|
logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
|
2019-10-23 17:55:34 +01:00
|
|
|
|
2015-03-30 10:23:30 -04:00
|
|
|
if err != nil {
|
2019-10-23 17:55:34 +01:00
|
|
|
return nil, fmt.Errorf("problem reading http request body: %w", err)
|
2015-03-30 10:23:30 -04:00
|
|
|
}
|
2019-10-23 17:55:34 +01:00
|
|
|
|
2015-03-30 10:23:30 -04:00
|
|
|
if resp.StatusCode != 200 {
|
2015-06-06 14:15:43 -04:00
|
|
|
return nil, fmt.Errorf("got %d %s", resp.StatusCode, body)
|
2015-03-30 10:23:30 -04:00
|
|
|
}
|
2019-10-23 17:55:34 +01:00
|
|
|
|
2015-03-30 10:23:30 -04:00
|
|
|
data, err := simplejson.NewJson(body)
|
|
|
|
if err != nil {
|
2019-10-23 17:55:34 +01:00
|
|
|
return nil, fmt.Errorf("error unmarshalling json: %w", err)
|
2015-03-30 10:23:30 -04:00
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
2015-05-12 21:48:13 -04:00
|
|
|
|
2018-12-20 09:37:02 +00:00
|
|
|
// RequestJSON parses the request body into the given interface
|
2018-11-29 14:26:41 +00:00
|
|
|
func RequestJSON(req *http.Request, v interface{}) error {
|
2016-06-23 08:29:44 -04:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
2019-02-10 08:37:45 -08:00
|
|
|
logger.Printf("%s %s %s", req.Method, req.URL, err)
|
2016-06-23 08:29:44 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
2019-10-23 17:55:34 +01:00
|
|
|
if body != nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
2019-02-10 08:37:45 -08:00
|
|
|
logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
|
2016-06-23 08:29:44 -04:00
|
|
|
if err != nil {
|
2019-10-23 17:55:34 +01:00
|
|
|
return fmt.Errorf("error reading body from http response: %w", err)
|
2016-06-23 08:29:44 -04:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("got %d %s", resp.StatusCode, body)
|
|
|
|
}
|
|
|
|
return json.Unmarshal(body, v)
|
|
|
|
}
|
|
|
|
|
2018-12-20 09:37:02 +00:00
|
|
|
// RequestUnparsedResponse performs a GET and returns the raw response object
|
2015-06-06 14:15:43 -04:00
|
|
|
func RequestUnparsedResponse(url string, header http.Header) (resp *http.Response, err error) {
|
2015-05-12 21:48:13 -04:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
2019-10-23 17:55:34 +01:00
|
|
|
return nil, fmt.Errorf("error performing get request: %w", err)
|
2015-05-12 21:48:13 -04:00
|
|
|
}
|
|
|
|
req.Header = header
|
|
|
|
|
2015-06-06 14:15:43 -04:00
|
|
|
return http.DefaultClient.Do(req)
|
2015-05-12 21:48:13 -04:00
|
|
|
}
|