43 lines
		
	
	
		
			887 B
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			887 B
		
	
	
	
		
			Go
		
	
	
	
| package api
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"io/ioutil"
 | |
| 	"log"
 | |
| 	"net/http"
 | |
| 
 | |
| 	"github.com/bitly/go-simplejson"
 | |
| )
 | |
| 
 | |
| func Request(req *http.Request) (*simplejson.Json, error) {
 | |
| 	resp, err := http.DefaultClient.Do(req)
 | |
| 	if err != nil {
 | |
| 		log.Printf("%s %s %s", req.Method, req.URL, err)
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	body, err := ioutil.ReadAll(resp.Body)
 | |
| 	resp.Body.Close()
 | |
| 	log.Printf("%d %s %s %s", resp.StatusCode, req.Method, req.URL, body)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	if resp.StatusCode != 200 {
 | |
| 		return nil, fmt.Errorf("got %d %s", resp.StatusCode, body)
 | |
| 	}
 | |
| 	data, err := simplejson.NewJson(body)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return data, nil
 | |
| }
 | |
| 
 | |
| func RequestUnparsedResponse(url string, header http.Header) (resp *http.Response, err error) {
 | |
| 	req, err := http.NewRequest("GET", url, nil)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	req.Header = header
 | |
| 
 | |
| 	return http.DefaultClient.Do(req)
 | |
| }
 |