Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0f45d03

Browse filesBrowse files
committed
Reworked this change a bit.
- 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.
1 parent acbafee commit 0f45d03
Copy full SHA for 0f45d03

File tree

Expand file treeCollapse file tree

3 files changed

+19
-27
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-27
lines changed

‎src/main/java/org/kohsuke/github/GHApiInfo.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GHApiInfo.java
-14Lines changed: 0 additions & 14 deletions
This file was deleted.

‎src/main/java/org/kohsuke/github/GitHub.java

Copy file name to clipboardExpand all lines: src/main/java/org/kohsuke/github/GitHub.java
+18-12Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -455,21 +455,27 @@ public boolean isCredentialValid() throws IOException {
455455
}
456456
}
457457

458+
private static class GHApiInfo {
459+
private String rate_limit_url;
460+
461+
void check(String apiUrl) throws IOException {
462+
if (rate_limit_url==null)
463+
throw new IOException(apiUrl+" doesn't look like GitHub API URL");
464+
465+
// make sure that the URL is legitimate
466+
new URL(rate_limit_url);
467+
}
468+
}
469+
458470
/**
459471
* Ensures that the API URL is valid.
472+
*
473+
* <p>
474+
* This method returns normally if the endpoint is reachable and verified to be GitHub API URL.
475+
* Otherwise this method throws {@link IOException} to indicate the problem.
460476
*/
461-
public boolean isApiUrlValid() throws IOException {
462-
try {
463-
GHApiInfo apiInfo = retrieve().to("/", GHApiInfo.class);
464-
try {
465-
new URL(apiInfo.getRateLimitUrl());
466-
return true;
467-
} catch (MalformedURLException mue) {
468-
return false;
469-
}
470-
} catch (IOException e) {
471-
return false;
472-
}
477+
public void checkApiUrlValidity() throws IOException {
478+
retrieve().to("/", GHApiInfo.class).check(apiUrl);
473479
}
474480

475481
/**

‎src/test/java/org/kohsuke/github/GitHubTest.java

Copy file name to clipboardExpand all lines: src/test/java/org/kohsuke/github/GitHubTest.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@ public void testGitHubEnterpriseDoesNotHaveRateLimit() throws IOException {
124124
@Test
125125
public void testGitHubIsApiUrlValid() throws IOException {
126126
GitHub github = GitHub.connectAnonymously();
127-
assertTrue(github.isApiUrlValid());
127+
github.checkApiUrlValidity();
128128
}
129129
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.