Skip to content

Commit

Permalink
Reworked this change a bit.
Browse files Browse the repository at this point in the history
- GHApiInfo need not be public because it's not publicly exposed.
- Throwing an exception is better IMO as it allows richer error message,
  including the differentiation between unreachable host name vs wrong
  URL, and reporting the API endpoint URL that was actually tried.
  • Loading branch information
kohsuke committed Dec 1, 2015
1 parent acbafee commit 0f45d03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
14 changes: 0 additions & 14 deletions src/main/java/org/kohsuke/github/GHApiInfo.java

This file was deleted.

30 changes: 18 additions & 12 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,21 +455,27 @@ public boolean isCredentialValid() throws IOException {
}
}

private static class GHApiInfo {
private String rate_limit_url;

void check(String apiUrl) throws IOException {
if (rate_limit_url==null)
throw new IOException(apiUrl+" doesn't look like GitHub API URL");

// make sure that the URL is legitimate
new URL(rate_limit_url);
}
}

/**
* Ensures that the API URL is valid.
*
* <p>
* This method returns normally if the endpoint is reachable and verified to be GitHub API URL.
* Otherwise this method throws {@link IOException} to indicate the problem.
*/
public boolean isApiUrlValid() throws IOException {
try {
GHApiInfo apiInfo = retrieve().to("/", GHApiInfo.class);
try {
new URL(apiInfo.getRateLimitUrl());
return true;
} catch (MalformedURLException mue) {
return false;
}
} catch (IOException e) {
return false;
}
public void checkApiUrlValidity() throws IOException {
retrieve().to("/", GHApiInfo.class).check(apiUrl);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException {
@Test
public void testGitHubIsApiUrlValid() throws IOException {
GitHub github = GitHub.connectAnonymously();
assertTrue(github.isApiUrlValid());
github.checkApiUrlValidity();
}
}

0 comments on commit 0f45d03

Please sign in to comment.