github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/bitbucket/client.go (about)

     1  package bitbucket
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"io/ioutil"
     9  	"log"
    10  	"net/http"
    11  )
    12  
    13  // Error represents a error from the bitbucket api.
    14  type Error struct {
    15  	APIError struct {
    16  		Message string `json:"message,omitempty"`
    17  	} `json:"error,omitempty"`
    18  	Type       string `json:"type,omitempty"`
    19  	StatusCode int
    20  	Endpoint   string
    21  }
    22  
    23  func (e Error) Error() string {
    24  	return fmt.Sprintf("API Error: %d %s %s", e.StatusCode, e.Endpoint, e.APIError.Message)
    25  }
    26  
    27  const (
    28  	// BitbucketEndpoint is the fqdn used to talk to bitbucket
    29  	BitbucketEndpoint string = "https://api.bitbucket.org/"
    30  )
    31  
    32  type BitbucketClient struct {
    33  	Username   string
    34  	Password   string
    35  	HTTPClient *http.Client
    36  }
    37  
    38  func (c *BitbucketClient) Do(method, endpoint string, payload *bytes.Buffer) (*http.Response, error) {
    39  
    40  	absoluteendpoint := BitbucketEndpoint + endpoint
    41  	log.Printf("[DEBUG] Sending request to %s %s", method, absoluteendpoint)
    42  
    43  	var bodyreader io.Reader
    44  
    45  	if payload != nil {
    46  		log.Printf("[DEBUG] With payload %s", payload.String())
    47  		bodyreader = payload
    48  	}
    49  
    50  	req, err := http.NewRequest(method, absoluteendpoint, bodyreader)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  
    55  	req.SetBasicAuth(c.Username, c.Password)
    56  
    57  	if payload != nil {
    58  		// Can cause bad request when putting default reviews if set.
    59  		req.Header.Add("Content-Type", "application/json")
    60  	}
    61  
    62  	req.Close = true
    63  
    64  	resp, err := c.HTTPClient.Do(req)
    65  	log.Printf("[DEBUG] Resp: %v Err: %v", resp, err)
    66  	if resp.StatusCode >= 400 || resp.StatusCode < 200 {
    67  		apiError := Error{
    68  			StatusCode: resp.StatusCode,
    69  			Endpoint:   endpoint,
    70  		}
    71  
    72  		body, err := ioutil.ReadAll(resp.Body)
    73  		if err != nil {
    74  			return nil, err
    75  		}
    76  
    77  		log.Printf("[DEBUG] Resp Body: %s", string(body))
    78  
    79  		err = json.Unmarshal(body, &apiError)
    80  		if err != nil {
    81  			apiError.APIError.Message = string(body)
    82  		}
    83  
    84  		return resp, error(apiError)
    85  
    86  	}
    87  	return resp, err
    88  }
    89  
    90  func (c *BitbucketClient) Get(endpoint string) (*http.Response, error) {
    91  	return c.Do("GET", endpoint, nil)
    92  }
    93  
    94  func (c *BitbucketClient) Post(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
    95  	return c.Do("POST", endpoint, jsonpayload)
    96  }
    97  
    98  func (c *BitbucketClient) Put(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
    99  	return c.Do("PUT", endpoint, jsonpayload)
   100  }
   101  
   102  func (c *BitbucketClient) PutOnly(endpoint string) (*http.Response, error) {
   103  	return c.Do("PUT", endpoint, nil)
   104  }
   105  
   106  func (c *BitbucketClient) Delete(endpoint string) (*http.Response, error) {
   107  	return c.Do("DELETE", endpoint, nil)
   108  }