Advertisement
Guest User

client.go

a guest
Dec 23rd, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  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 = "http://localhost:7990/"
  30. )
  31.  
  32. // Client is the base internal Client to talk to bitbucket API. This should be a username and password
  33. // the password should be a app-password.
  34. type Client struct {
  35. Username string
  36. Password string
  37. HTTPClient *http.Client
  38. }
  39.  
  40. // Do Will just call the bitbucket api but also add auth to it and some extra headers
  41. func (c *Client) Do(method, endpoint string, payload *bytes.Buffer) (*http.Response, error) {
  42.  
  43. absoluteendpoint := BitbucketEndpoint + endpoint
  44. log.Printf("[DEBUG] Sending request to %s %s", method, absoluteendpoint)
  45.  
  46. var bodyreader io.Reader
  47.  
  48. if payload != nil {
  49. log.Printf("[DEBUG] With payload %s", payload.String())
  50. bodyreader = payload
  51. }
  52.  
  53. req, err := http.NewRequest(method, absoluteendpoint, bodyreader)
  54. if err != nil {
  55. return nil, err
  56. }
  57.  
  58. log.Printf("[DEBUG] Adding basic auth to request for user %s", c.Username)
  59. req.SetBasicAuth(c.Username, c.Password)
  60.  
  61. if payload != nil {
  62. // Can cause bad request when putting default reviews if set.
  63. req.Header.Add("Content-Type", "application/json")
  64. }
  65.  
  66. req.Close = true
  67.  
  68. resp, err := c.HTTPClient.Do(req)
  69. log.Printf("[DEBUG] Resp: %v Err: %v", resp, err)
  70. if resp.StatusCode >= 400 || resp.StatusCode < 200 {
  71. apiError := Error{
  72. StatusCode: resp.StatusCode,
  73. Endpoint: endpoint,
  74. }
  75.  
  76. body, err := ioutil.ReadAll(resp.Body)
  77. if err != nil {
  78. return nil, err
  79. }
  80.  
  81. log.Printf("[DEBUG] Resp Body: %s", string(body))
  82.  
  83. err = json.Unmarshal(body, &apiError)
  84. if err != nil {
  85. apiError.APIError.Message = string(body)
  86. }
  87.  
  88. return resp, error(apiError)
  89.  
  90. }
  91. return resp, err
  92. }
  93.  
  94. // Get is just a helper method to do but with a GET verb
  95. func (c *Client) Get(endpoint string) (*http.Response, error) {
  96. return c.Do("GET", endpoint, nil)
  97. }
  98.  
  99. // Post is just a helper method to do but with a POST verb
  100. func (c *Client) Post(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
  101. return c.Do("POST", endpoint, jsonpayload)
  102. }
  103.  
  104. // Put is just a helper method to do but with a PUT verb
  105. func (c *Client) Put(endpoint string, jsonpayload *bytes.Buffer) (*http.Response, error) {
  106. return c.Do("PUT", endpoint, jsonpayload)
  107. }
  108.  
  109. // PutOnly is just a helper method to do but with a PUT verb and a nil body
  110. func (c *Client) PutOnly(endpoint string) (*http.Response, error) {
  111. return c.Do("PUT", endpoint, nil)
  112. }
  113.  
  114. // Delete is just a helper to Do but with a DELETE verb
  115. func (c *Client) Delete(endpoint string) (*http.Response, error) {
  116. return c.Do("DELETE", endpoint, nil)
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement