git.iko.soy/typeable/oauth2-proxy
on f435fa6 help sign in
64 lines 1.6 kB history raw
1package api
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "net/http"
9 "github.com/bitly/go-simplejson"
10 "github.com/pusher/oauth2_proxy/logger"
13// Request parses the request body into a simplejson.Json object
14func Request(req *http.Request) (*simplejson.Json, error) {
15 resp, err := http.DefaultClient.Do(req)
16 if err != nil {
17 logger.Printf("%s %s %s", req.Method, req.URL, err)
18 return nil, err
19 }
20 body, err := ioutil.ReadAll(resp.Body)
21 resp.Body.Close()
22 logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
23 if err != nil {
24 return nil, err
25 }
26 if resp.StatusCode != 200 {
27 return nil, fmt.Errorf("got %d %s", resp.StatusCode, body)
28 }
29 data, err := simplejson.NewJson(body)
30 if err != nil {
31 return nil, err
32 }
33 return data, nil
36// RequestJSON parses the request body into the given interface
37func RequestJSON(req *http.Request, v interface{}) error {
38 resp, err := http.DefaultClient.Do(req)
39 if err != nil {
40 logger.Printf("%s %s %s", req.Method, req.URL, err)
41 return err
42 }
43 body, err := ioutil.ReadAll(resp.Body)
44 resp.Body.Close()
45 logger.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
46 if err != nil {
47 return err
48 }
49 if resp.StatusCode != 200 {
50 return fmt.Errorf("got %d %s", resp.StatusCode, body)
51 }
52 return json.Unmarshal(body, v)
55// RequestUnparsedResponse performs a GET and returns the raw response object
56func RequestUnparsedResponse(url string, header http.Header) (resp *http.Response, err error) {
57 req, err := http.NewRequest("GET", url, nil)
58 if err != nil {
59 return nil, err
60 }
61 req.Header = header
63 return http.DefaultClient.Do(req)