Skip to content

Commit f385f27

Browse files
chriskingnetdmitshur
authored andcommitted
Add NewEnterpriseClient method. (#763)
This is a convenience helper for creating an enterprise client with custom URLs. Previous code that uses NewClient and sets its BaseURL, UploadURL fields is still valid. This method is for convenice and better discoverability within godoc. It also aligns with a similar method in githubql package (for GitHub API v4). Resolves #756.
1 parent 621edea commit f385f27

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎github/github.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,37 @@ func NewClient(httpClient *http.Client) *Client {
235235
return c
236236
}
237237

238+
// NewEnterpriseClient returns a new GitHub API client with provided
239+
// base URL and upload URL (often the same URL).
240+
// If either URL does not have a trailing slash, one is added automatically.
241+
// If a nil httpClient is provided, http.DefaultClient will be used.
242+
//
243+
// Note that NewEnterpriseClient is a convenience helper only;
244+
// its behavior is equivalent to using NewClient, followed by setting
245+
// the BaseURL and UploadURL fields.
246+
func NewEnterpriseClient(baseURL, uploadURL string, httpClient *http.Client) (*Client, error) {
247+
baseEndpoint, err := url.Parse(baseURL)
248+
if err != nil {
249+
return nil, err
250+
}
251+
if !strings.HasSuffix(baseEndpoint.Path, "/") {
252+
baseEndpoint.Path += "/"
253+
}
254+
255+
uploadEndpoint, err := url.Parse(uploadURL)
256+
if err != nil {
257+
return nil, err
258+
}
259+
if !strings.HasSuffix(uploadEndpoint.Path, "/") {
260+
uploadEndpoint.Path += "/"
261+
}
262+
263+
c := NewClient(httpClient)
264+
c.BaseURL = baseEndpoint
265+
c.UploadURL = uploadEndpoint
266+
return c, nil
267+
}
268+
238269
// NewRequest creates an API request. A relative URL can be provided in urlStr,
239270
// in which case it is resolved relative to the BaseURL of the Client.
240271
// Relative URLs should always be specified without a preceding slash. If

‎github/github_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,41 @@ func TestNewClient(t *testing.T) {
186186
}
187187
}
188188

189+
func TestNewEnterpriseClient(t *testing.T) {
190+
baseURL := "https://custom-url/"
191+
uploadURL := "https://custom-upload-url/"
192+
c, err := NewEnterpriseClient(baseURL, uploadURL, nil)
193+
if err != nil {
194+
t.Fatalf("NewEnterpriseClient returned unexpected error: %v", err)
195+
}
196+
197+
if got, want := c.BaseURL.String(), baseURL; got != want {
198+
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
199+
}
200+
if got, want := c.UploadURL.String(), uploadURL; got != want {
201+
t.Errorf("NewClient UploadURL is %v, want %v", got, want)
202+
}
203+
}
204+
205+
func TestNewEnterpriseClient_addsTrailingSlashToURLs(t *testing.T) {
206+
baseURL := "https://custom-url"
207+
uploadURL := "https://custom-upload-url"
208+
formattedBaseURL := baseURL + "/"
209+
formattedUploadURL := uploadURL + "/"
210+
211+
c, err := NewEnterpriseClient(baseURL, uploadURL, nil)
212+
if err != nil {
213+
t.Fatalf("NewEnterpriseClient returned unexpected error: %v", err)
214+
}
215+
216+
if got, want := c.BaseURL.String(), formattedBaseURL; got != want {
217+
t.Errorf("NewClient BaseURL is %v, want %v", got, want)
218+
}
219+
if got, want := c.UploadURL.String(), formattedUploadURL; got != want {
220+
t.Errorf("NewClient UploadURL is %v, want %v", got, want)
221+
}
222+
}
223+
189224
// Ensure that length of Client.rateLimits is the same as number of fields in RateLimits struct.
190225
func TestClient_rateLimits(t *testing.T) {
191226
if got, want := len(Client{}.rateLimits), reflect.TypeOf(RateLimits{}).NumField(); got != want {

0 commit comments

Comments
 (0)