@@ -25,13 +25,13 @@ const DEFAULT_PAGE_LENGTH = 10
25
25
const DEFAULT_MAX_DEPTH = 1
26
26
const DEFAULT_BITBUCKET_API_BASE_URL = "https://api.bitbucket.org/2.0"
27
27
28
- func apiBaseUrl () string {
28
+ func apiBaseUrl () ( * url. URL , error ) {
29
29
ev := os .Getenv ("BITBUCKET_API_BASE_URL" )
30
- if ev ! = "" {
31
- return ev
30
+ if ev = = "" {
31
+ ev = DEFAULT_BITBUCKET_API_BASE_URL
32
32
}
33
33
34
- return DEFAULT_BITBUCKET_API_BASE_URL
34
+ return url . Parse ( ev )
35
35
}
36
36
37
37
type Client struct {
@@ -43,7 +43,7 @@ type Client struct {
43
43
Workspaces * Workspace
44
44
Pagelen uint64
45
45
MaxDepth uint64
46
- apiBaseURL string
46
+ apiBaseURL * url. URL
47
47
48
48
HttpClient * http.Client
49
49
}
@@ -135,7 +135,11 @@ func NewBasicAuth(u, p string) *Client {
135
135
}
136
136
137
137
func injectClient (a * auth ) * Client {
138
- c := & Client {Auth : a , Pagelen : DEFAULT_PAGE_LENGTH , MaxDepth : DEFAULT_MAX_DEPTH , apiBaseURL : apiBaseUrl ()}
138
+ bitbucketUrl , err := apiBaseUrl ()
139
+ if err != nil {
140
+ log .Fatalf ("invalid bitbucket url" )
141
+ }
142
+ c := & Client {Auth : a , Pagelen : DEFAULT_PAGE_LENGTH , MaxDepth : DEFAULT_MAX_DEPTH , apiBaseURL : bitbucketUrl }
139
143
c .Repositories = & Repositories {
140
144
c : c ,
141
145
PullRequests : & PullRequests {c : c },
@@ -157,11 +161,15 @@ func injectClient(a *auth) *Client {
157
161
}
158
162
159
163
func (c * Client ) GetApiBaseURL () string {
160
- return c .apiBaseURL
164
+ return fmt .Sprintf ("%s%s" , c .apiBaseURL .GetApiHostnameURL (), c .apiBaseURL .Path )
165
+ }
166
+
167
+ func (c * Client ) GetApiHostnameURL () string {
168
+ return fmt .Sprintf ("%s://%s" , c .apiBaseURL .Scheme , c .apiBaseURL .Host )
161
169
}
162
170
163
- func (c * Client ) SetApiBaseURL (urlStr string ) {
164
- c .apiBaseURL = urlStr
171
+ func (c * Client ) SetApiBaseURL (urlStr url. URL ) {
172
+ c .apiBaseURL = & urlStr
165
173
}
166
174
167
175
func (c * Client ) executeRaw (method string , urlStr string , text string ) (io.ReadCloser , error ) {
@@ -379,7 +387,7 @@ func unexpectedHttpStatusCode(statusCode int) bool {
379
387
func (c * Client ) requestUrl (template string , args ... interface {}) string {
380
388
381
389
if len (args ) == 1 && args [0 ] == "" {
382
- return c .apiBaseURL + template
390
+ return c .GetApiBaseURL () + template
383
391
}
384
- return c .apiBaseURL + fmt .Sprintf (template , args ... )
392
+ return c .GetApiBaseURL () + fmt .Sprintf (template , args ... )
385
393
}
0 commit comments