@@ -25,25 +25,25 @@ const DEFAULT_PAGE_LENGTH = 10
2525const DEFAULT_MAX_DEPTH = 1
2626const DEFAULT_BITBUCKET_API_BASE_URL = "https://api.bitbucket.org/2.0"
2727
28- func apiBaseUrl () string {
28+ func apiBaseUrl () ( * url. URL , error ) {
2929 ev := os .Getenv ("BITBUCKET_API_BASE_URL" )
30- if ev ! = "" {
31- return ev
30+ if ev = = "" {
31+ ev = DEFAULT_BITBUCKET_API_BASE_URL
3232 }
3333
34- return DEFAULT_BITBUCKET_API_BASE_URL
34+ return url . Parse ( ev )
3535}
3636
3737type Client struct {
38- Auth * auth
39- Users users
40- User user
41- Teams teams
42- Repositories * Repositories
43- Workspaces * Workspace
44- Pagelen uint64
45- MaxDepth uint64
46- apiBaseURL string
38+ Auth * auth
39+ Users users
40+ User user
41+ Teams teams
42+ Repositories * Repositories
43+ Workspaces * Workspace
44+ Pagelen uint64
45+ MaxDepth uint64
46+ apiBaseURL * url. URL
4747
4848 HttpClient * http.Client
4949}
@@ -135,7 +135,11 @@ func NewBasicAuth(u, p string) *Client {
135135}
136136
137137func 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 }
139143 c .Repositories = & Repositories {
140144 c : c ,
141145 PullRequests : & PullRequests {c : c },
@@ -156,11 +160,15 @@ func injectClient(a *auth) *Client {
156160}
157161
158162func (c * Client ) GetApiBaseURL () string {
159- return c .apiBaseURL
163+ return fmt .Sprintf ("%s://%s%s" , c .apiBaseURL .Scheme , c .apiBaseURL .Host , c .apiBaseURL .Path )
164+ }
165+
166+ func (c * Client ) GetApiHostnameURL () string {
167+ return fmt .Sprintf ("%s://%s" , c .apiBaseURL .Scheme , c .apiBaseURL .Host )
160168}
161169
162- func (c * Client ) SetApiBaseURL (urlStr string ) {
163- c .apiBaseURL = urlStr
170+ func (c * Client ) SetApiBaseURL (urlStr url. URL ) {
171+ c .apiBaseURL = & urlStr
164172}
165173
166174func (c * Client ) executeRaw (method string , urlStr string , text string ) (io.ReadCloser , error ) {
@@ -386,7 +394,7 @@ func unexpectedHttpStatusCode(statusCode int) bool {
386394func (c * Client ) requestUrl (template string , args ... interface {}) string {
387395
388396 if len (args ) == 1 && args [0 ] == "" {
389- return c .apiBaseURL + template
397+ return c .GetApiBaseURL () + template
390398 }
391- return c .apiBaseURL + fmt .Sprintf (template , args ... )
399+ return c .GetApiBaseURL () + fmt .Sprintf (template , args ... )
392400}
0 commit comments