|  | @@ -0,0 +1,58 @@
 | 
	
		
			
			|  | 1 | +package grafana
 | 
	
		
			
			|  | 2 | +
 | 
	
		
			
			|  | 3 | +import (
 | 
	
		
			
			|  | 4 | +	"bytes"
 | 
	
		
			
			|  | 5 | +	"fmt"
 | 
	
		
			
			|  | 6 | +	"io/ioutil"
 | 
	
		
			
			|  | 7 | +	"net/http"
 | 
	
		
			
			|  | 8 | +)
 | 
	
		
			
			|  | 9 | +
 | 
	
		
			
			|  | 10 | +type Client struct {
 | 
	
		
			
			|  | 11 | +	BaseURL    string
 | 
	
		
			
			|  | 12 | +	APIKey     string
 | 
	
		
			
			|  | 13 | +	httpClient *http.Client
 | 
	
		
			
			|  | 14 | +}
 | 
	
		
			
			|  | 15 | +
 | 
	
		
			
			|  | 16 | +func NewClient(baseURL string, apiKey string) (c *Client) {
 | 
	
		
			
			|  | 17 | +	return &Client{
 | 
	
		
			
			|  | 18 | +		BaseURL:    baseURL,
 | 
	
		
			
			|  | 19 | +		APIKey:     apiKey,
 | 
	
		
			
			|  | 20 | +		httpClient: new(http.Client),
 | 
	
		
			
			|  | 21 | +	}
 | 
	
		
			
			|  | 22 | +}
 | 
	
		
			
			|  | 23 | +
 | 
	
		
			
			|  | 24 | +func (c *Client) request(method string, endpoint string, body []byte) ([]byte, error) {
 | 
	
		
			
			|  | 25 | +	url := c.BaseURL + "/api/" + endpoint
 | 
	
		
			
			|  | 26 | +
 | 
	
		
			
			|  | 27 | +	req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
 | 
	
		
			
			|  | 28 | +	if err != nil {
 | 
	
		
			
			|  | 29 | +		return nil, err
 | 
	
		
			
			|  | 30 | +	}
 | 
	
		
			
			|  | 31 | +
 | 
	
		
			
			|  | 32 | +	authHeader := fmt.Sprintf("Bearer %s", c.APIKey)
 | 
	
		
			
			|  | 33 | +	req.Header.Add("Authorization", authHeader)
 | 
	
		
			
			|  | 34 | +
 | 
	
		
			
			|  | 35 | +	resp, err := c.httpClient.Do(req)
 | 
	
		
			
			|  | 36 | +	if err != nil {
 | 
	
		
			
			|  | 37 | +		return nil, err
 | 
	
		
			
			|  | 38 | +	}
 | 
	
		
			
			|  | 39 | +
 | 
	
		
			
			|  | 40 | +	respBody, err := ioutil.ReadAll(resp.Body)
 | 
	
		
			
			|  | 41 | +	if err != nil {
 | 
	
		
			
			|  | 42 | +		return nil, err
 | 
	
		
			
			|  | 43 | +	}
 | 
	
		
			
			|  | 44 | +
 | 
	
		
			
			|  | 45 | +	if resp.StatusCode != http.StatusOK {
 | 
	
		
			
			|  | 46 | +		if resp.StatusCode == http.StatusNotFound {
 | 
	
		
			
			|  | 47 | +			err = fmt.Errorf("%s not found (404)", url)
 | 
	
		
			
			|  | 48 | +		} else {
 | 
	
		
			
			|  | 49 | +			err = fmt.Errorf(
 | 
	
		
			
			|  | 50 | +				"Unknown error: %d; body: %s",
 | 
	
		
			
			|  | 51 | +				resp.StatusCode,
 | 
	
		
			
			|  | 52 | +				string(respBody),
 | 
	
		
			
			|  | 53 | +			)
 | 
	
		
			
			|  | 54 | +		}
 | 
	
		
			
			|  | 55 | +	}
 | 
	
		
			
			|  | 56 | +
 | 
	
		
			
			|  | 57 | +	return respBody, err
 | 
	
		
			
			|  | 58 | +}
 |