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