2727import com .google .api .client .json .JsonFactory ;
2828import com .google .api .client .json .JsonObjectParser ;
2929import com .google .api .client .json .gson .GsonFactory ;
30+ import com .google .common .annotations .VisibleForTesting ;
3031import com .google .common .base .Preconditions ;
3132import com .google .common .collect .ImmutableListMultimap ;
3233import com .google .common .collect .Iterables ;
@@ -55,9 +56,11 @@ public class GitHubApiTransportImpl implements GitHubApiTransport {
5556 private static final FluentLogger logger = FluentLogger .forEnclosingClass ();
5657
5758 private static final JsonFactory JSON_FACTORY = new GsonFactory ();
58- private static final String API_URL = "https://api.github.com" ;
59- private static final String GITHUB_WEB_URL = "https://github.com" ;
59+ private static final String GITHUB_DOT_COM_API_URL = "https://api.github.com" ;
60+ private static final String GITHUB_DOT_COM_WEB_URL = "https://github.com" ;
6061
62+ private final String apiUrl ;
63+ private final String webUrl ;
6164 private final GitRepository repo ;
6265 private final HttpTransport httpTransport ;
6366 private final String storePath ;
@@ -71,6 +74,24 @@ public GitHubApiTransportImpl(GitRepository repo, HttpTransport httpTransport,
7174 this .storePath = storePath ;
7275 this .console = Preconditions .checkNotNull (console );
7376 this .bearerAuth = bearerAuth ;
77+ this .apiUrl = GITHUB_DOT_COM_API_URL ;
78+ this .webUrl = GITHUB_DOT_COM_WEB_URL ;
79+ }
80+
81+ public GitHubApiTransportImpl (
82+ GitRepository repo ,
83+ HttpTransport httpTransport ,
84+ String storePath ,
85+ boolean bearerAuth ,
86+ Console console ,
87+ String webUrl ) {
88+ this .repo = Preconditions .checkNotNull (repo );
89+ this .httpTransport = Preconditions .checkNotNull (httpTransport );
90+ this .storePath = storePath ;
91+ this .console = Preconditions .checkNotNull (console );
92+ this .bearerAuth = bearerAuth ;
93+ this .webUrl = buildGhesWebUrl (Preconditions .checkNotNull (webUrl ));
94+ this .apiUrl = buildGhesApiUrl (this .webUrl );
7495 }
7596
7697 @ SuppressWarnings ("unchecked" )
@@ -87,7 +108,7 @@ public <T> T get(String path, Type responseType, ImmutableListMultimap<String, S
87108 Object responseObj = GsonParserUtil .parseHttpResponse (response , responseType , false );
88109 if (responseObj instanceof PaginatedPayload ) {
89110 return (T )
90- ((PaginatedPayload ) responseObj ).annotatePayload (API_URL , maybeGetLinkHeader (response ));
111+ ((PaginatedPayload ) responseObj ).annotatePayload (apiUrl , maybeGetLinkHeader (response ));
91112 }
92113 return (T ) responseObj ;
93114 } catch (HttpResponseException e ) {
@@ -152,7 +173,7 @@ public <T> T post(String path, Object request, Type responseType, String request
152173 response , responseType , false );
153174 if (responseObj instanceof PaginatedPayload ) {
154175 return (T )
155- ((PaginatedPayload ) responseObj ).annotatePayload (API_URL , maybeGetLinkHeader (response ));
176+ ((PaginatedPayload ) responseObj ).annotatePayload (apiUrl , maybeGetLinkHeader (response ));
156177 }
157178 return (T ) responseObj ;
158179
@@ -221,7 +242,7 @@ private HttpRequestFactory getHttpRequestFactory(
221242
222243 private GenericUrl getFullEndpointURL (String path ) {
223244 String maybePrefix = path .startsWith ("/" ) ? "" : "/" ;
224- return new GenericUrl (URI .create (API_URL + maybePrefix + path ));
245+ return new GenericUrl (URI .create (apiUrl + maybePrefix + path ));
225246 }
226247
227248 /**
@@ -231,10 +252,10 @@ private GenericUrl getFullEndpointURL(String path) {
231252 */
232253 private UserPassword getCredentials () throws RepoException , ValidationException {
233254 try {
234- return repo .credentialFill (API_URL );
255+ return repo .credentialFill (apiUrl );
235256 } catch (ValidationException e ) {
236257 try {
237- return repo .credentialFill (GITHUB_WEB_URL );
258+ return repo .credentialFill (webUrl );
238259 } catch (ValidationException e1 ) {
239260 // Ugly, but helpful...
240261 throw new ValidationException (String .format (
@@ -252,4 +273,22 @@ private UserPassword getCredentials() throws RepoException, ValidationException
252273 }
253274 }
254275 }
276+
277+ private static String buildGhesWebUrl (String hostName ) {
278+ return "https://" + hostName ;
279+ }
280+
281+ private static String buildGhesApiUrl (String hostName ) {
282+ return hostName + "/api/v3" ;
283+ }
284+
285+ @ VisibleForTesting
286+ public String getApiUrl () {
287+ return apiUrl ;
288+ }
289+
290+ @ VisibleForTesting
291+ public String getWebUrl () {
292+ return webUrl ;
293+ }
255294}
0 commit comments