@@ -2,64 +2,107 @@ package bitbucket
22
33import (
44 "bytes"
5+ "encoding/json"
6+ "fmt"
7+ "io"
8+ "io/ioutil"
9+ "log"
510 "net/http"
611)
712
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+
832type BitbucketClient struct {
9- Username string
10- Password string
33+ Username string
34+ Password string
35+ HTTPClient * http.Client
1136}
1237
13- func (c * BitbucketClient ) Get (endpoint string ) (* http.Response , error ) {
14- client := & http.Client {}
15- req , err := http .NewRequest ("GET" , "https://api.bitbucket.org/" + endpoint , nil )
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 )
1651 if err != nil {
1752 return nil , err
1853 }
1954
2055 req .SetBasicAuth (c .Username , c .Password )
21- return client .Do (req )
2256
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 )
2392}
2493
2594func (c * BitbucketClient ) Post (endpoint string , jsonpayload * bytes.Buffer ) (* http.Response , error ) {
26- client := & http.Client {}
27- req , err := http .NewRequest ("POST" , "https://api.bitbucket.org/" + endpoint , jsonpayload )
28- if err != nil {
29- return nil , err
30- }
31- req .SetBasicAuth (c .Username , c .Password )
32- req .Header .Add ("content-type" , "application/json" )
33- return client .Do (req )
95+ return c .Do ("POST" , endpoint , jsonpayload )
3496}
3597
3698func (c * BitbucketClient ) Put (endpoint string , jsonpayload * bytes.Buffer ) (* http.Response , error ) {
37- client := & http.Client {}
38- req , err := http .NewRequest ("PUT" , "https://api.bitbucket.org/" + endpoint , jsonpayload )
39- if err != nil {
40- return nil , err
41- }
42- req .SetBasicAuth (c .Username , c .Password )
43- req .Header .Add ("content-type" , "application/json" )
44- return client .Do (req )
99+ return c .Do ("PUT" , endpoint , jsonpayload )
45100}
46101
47102func (c * BitbucketClient ) PutOnly (endpoint string ) (* http.Response , error ) {
48- client := & http.Client {}
49- req , err := http .NewRequest ("PUT" , "https://api.bitbucket.org/" + endpoint , nil )
50- if err != nil {
51- return nil , err
52- }
53- req .SetBasicAuth (c .Username , c .Password )
54- return client .Do (req )
103+ return c .Do ("PUT" , endpoint , nil )
55104}
56105
57106func (c * BitbucketClient ) Delete (endpoint string ) (* http.Response , error ) {
58- client := & http.Client {}
59- req , err := http .NewRequest ("DELETE" , "https://api.bitbucket.org/" + endpoint , nil )
60- if err != nil {
61- return nil , err
62- }
63- req .SetBasicAuth (c .Username , c .Password )
64- return client .Do (req )
107+ return c .Do ("DELETE" , endpoint , nil )
65108}
0 commit comments