diff options
author | Kevin Sawicki | 2011-10-27 20:54:09 +0000 |
---|---|---|
committer | Kevin Sawicki | 2011-10-27 20:54:09 +0000 |
commit | 22f22afb77dd76f302c55970a09a96cab145d782 (patch) | |
tree | 89fcdf414d2fbb7204d0df0754e54276b095bd46 | |
parent | e45976737a7e42f11511d1b275d941bdb1e98575 (diff) | |
download | egit-github-22f22afb77dd76f302c55970a09a96cab145d782.tar.gz egit-github-22f22afb77dd76f302c55970a09a96cab145d782.tar.xz egit-github-22f22afb77dd76f302c55970a09a96cab145d782.zip |
Support non-github.com host names.
A URI prefix of '/api/v3' will be prepended to all request
URIs when the configured client host is not github.com or
gist.github.com
Change-Id: I71549652c321b2ead7b702abfc2d9113f457956f
Signed-off-by: Kevin Sawicki <kevin@github.com>
2 files changed, 40 insertions, 11 deletions
diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java index 377e2f5e..25bf681a 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/GitHubClient.java @@ -25,8 +25,11 @@ import static org.eclipse.egit.github.core.client.IGitHubConstants.AUTH_TOKEN; import static org.eclipse.egit.github.core.client.IGitHubConstants.CHARSET_UTF8; import static org.eclipse.egit.github.core.client.IGitHubConstants.CONTENT_TYPE_JSON; import static org.eclipse.egit.github.core.client.IGitHubConstants.HOST_API; +import static org.eclipse.egit.github.core.client.IGitHubConstants.HOST_DEFAULT; +import static org.eclipse.egit.github.core.client.IGitHubConstants.HOST_GISTS; import static org.eclipse.egit.github.core.client.IGitHubConstants.PROTOCOL_HTTPS; -import static org.eclipse.egit.github.core.client.IGitHubConstants.SUBDOMAIN_API; +import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_V2_API; +import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_V3_API; import com.google.gson.Gson; import com.google.gson.JsonParseException; @@ -73,9 +76,10 @@ public class GitHubClient { /** * Create API client from URL. - * + * <p> * This creates an HTTPS-based client with a host that contains the host - * value of the given URL prefixed with 'api'. + * value of the given URL prefixed with 'api' if the given URL is github.com + * or gist.github.com * * @param url * @return client @@ -83,7 +87,8 @@ public class GitHubClient { public static GitHubClient createClient(String url) { try { String host = new URL(url).getHost(); - host = SUBDOMAIN_API + "." + host; //$NON-NLS-1$ + if (HOST_DEFAULT.equals(host) || HOST_GISTS.equals(host)) + host = HOST_API; return new GitHubClient(host); } catch (IOException e) { throw new IllegalArgumentException(e); @@ -103,6 +108,8 @@ public class GitHubClient { private final Header accept = new BasicHeader("Accept", "application/json"); //$NON-NLS-1$ //$NON-NLS-2$ + private final String prefix; + private Gson gson = GsonUtils.getGson(); /** @@ -143,6 +150,12 @@ public class GitHubClient { this.httpHost = httpHost; + // Use URI prefix on non-standard host names + if (!HOST_API.equals(httpHost.getHostName())) + prefix = SEGMENT_V3_API; + else + prefix = null; + // Support JVM configured proxy servers client.setRoutePlanner(new ProxySelectorRoutePlanner(client .getConnectionManager().getSchemeRegistry(), ProxySelector @@ -195,13 +208,25 @@ public class GitHubClient { } /** + * Configure request URI + * + * @param uri + * @return configured URI + */ + protected String configureUri(final String uri) { + if (prefix == null || uri.startsWith(SEGMENT_V2_API)) + return uri; + return prefix + uri; + } + + /** * Create standard post method * * @param uri * @return post */ - protected HttpPost createPost(String uri) { - return configureRequest(new HttpPost(uri)); + protected HttpPost createPost(final String uri) { + return configureRequest(new HttpPost(configureUri(uri))); } /** @@ -210,8 +235,8 @@ public class GitHubClient { * @param uri * @return post */ - protected HttpPut createPut(String uri) { - return configureRequest(new HttpPut(uri)); + protected HttpPut createPut(final String uri) { + return configureRequest(new HttpPut(configureUri(uri))); } /** @@ -220,8 +245,8 @@ public class GitHubClient { * @param uri * @return get method */ - protected HttpGet createGet(String uri) { - return configureRequest(new HttpGet(uri)); + protected HttpGet createGet(final String uri) { + return configureRequest(new HttpGet(configureUri(uri))); } /** @@ -231,7 +256,7 @@ public class GitHubClient { * @return get method */ protected HttpDelete createDelete(String uri) { - return configureRequest(new HttpDelete(uri)); + return configureRequest(new HttpDelete(configureUri(uri))); } /** diff --git a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java index 8224ad1d..4c69af3b 100644 --- a/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java +++ b/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/IGitHubConstants.java @@ -43,6 +43,8 @@ public interface IGitHubConstants { String HOST_API_V2 = "github.com"; //$NON-NLS-1$ /** */ String HOST_DEFAULT = "github.com"; //$NON-NLS-1$ + /** */ + String HOST_GISTS = "gist.github.com"; //$NON-NLS-1$ /** */ String META_REL = "rel"; //$NON-NLS-1$ @@ -160,6 +162,8 @@ public interface IGitHubConstants { String SEGMENT_WATCHERS = "/watchers"; //$NON-NLS-1$ /** */ String SEGMENT_V2_API = "/api/v2/json"; //$NON-NLS-1$ + /** */ + String SEGMENT_V3_API = "/api/v3"; //$NON-NLS-1$ /** */ String SUBDOMAIN_API = "api"; //$NON-NLS-1$ |