-
Notifications
You must be signed in to change notification settings - Fork 736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move array population to single iterator code path #623
Conversation
9123e95
to
f4bc591
Compare
This remove a second code path throught pagination.
f4bc591
to
3296cef
Compare
@PauloMigAlmeida |
@@ -425,7 +425,7 @@ URL getApiURL(String tailApiUrl) throws IOException { | |||
} | |||
|
|||
Requester retrieve() { | |||
return new Requester(this).method("GET"); | |||
return new Requester(this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requester
defaults to GET
now.
import java.util.function.Consumer; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Matcher; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No more regex-ing needed.
for (Entry e : args) { | ||
if (e.key.equals(key)) { | ||
e.value = value; | ||
for (int index = 0; index < args.size(); index++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With final
Entry fields, we now replace the instance rather than changing the values.
* the content type | ||
* @return the requester | ||
*/ | ||
Requester setRawUrlPath(String urlOrPath) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually, we'll want to call this method directly where it is needed, but for now withUrlPath
has logic to decide when to use this.
* @throws IOException | ||
* if the server returns 4xx/5xx responses. | ||
*/ | ||
public <T> T[] toArray(@Nonnull Class<T[]> type) throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overloading to()
doesn't work with T[]
and T
. The compiler can't handle it. Instead moved this logic to this new method.
This results in cleaner and clearer code - there is no case where the caller doesn't already know whether they want an array or an instance returned so why call one method and then immediately have an if
statement.
try { | ||
T result = parse(type, instance); | ||
if (type != null && type.isArray()) { // we might have to loop for pagination - done through recursion | ||
final String links = uc.getHeaderField("link"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This same work as done by the PagingIterator<T>
- see toArray()
.
@@ -2,7 +2,7 @@ | |||
"id": "a979348d-c6be-4cb7-8877-7c42a6f013ae", | |||
"name": "notifications", | |||
"request": { | |||
"url": "/notifications?all=true&page=9&all=true", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And by switching to one consistent path we find little bugs like this. Yay!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
@@ -23,7 +23,7 @@ | |||
GHAppCreateTokenBuilder(GitHub root, String apiUrlTail, Map<String, GHPermissionType> permissions) { | |||
this.root = root; | |||
this.apiUrlTail = apiUrlTail; | |||
this.builder = new Requester(root); | |||
this.builder = root.retrieve().method("POST"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't that be just root.retrieve()
as we are setting the method on the create()
method?
@@ -135,7 +135,7 @@ public String getBrowserDownloadUrl() { | |||
} | |||
|
|||
private void edit(String key, Object value) throws IOException { | |||
new Requester(root).with(key, value).method("PATCH").to(getApiRoute()); | |||
root.retrieve().method("POST").with(key, value).method("PATCH").withUrlPath(getApiRoute()).to(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove the method("POST")
as we are setting method("PATCH")
almost right after it
@@ -13,7 +13,7 @@ | |||
|
|||
GHBlobBuilder(GHRepository repo) { | |||
this.repo = repo; | |||
req = new Requester(repo.root); | |||
req = repo.root.retrieve().method("POST"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
@@ -123,7 +123,7 @@ GHBranchProtection wrap(GHBranch branch) { | |||
} | |||
|
|||
private Requester requester() { | |||
return new Requester(root).withPreview(ZZZAX); | |||
return root.retrieve().method("POST").withPreview(ZZZAX); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe replace this with return root.retrieve()..withPreview(ZZZAX);
@@ -87,7 +87,7 @@ public RequiredReviews getRequiredReviews() { | |||
@Preview | |||
@Deprecated | |||
public boolean getRequiredSignatures() throws IOException { | |||
return requester().method("GET").to(url + REQUIRE_SIGNATURES_URI, RequiredSignatures.class).enabled; | |||
return requester().method("GET").withUrlPath(url + REQUIRE_SIGNATURES_URI).to(RequiredSignatures.class).enabled; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you implement the suggestion of the private Requester requester()
then you can get rid of .method("GET")
and take advantage of the default HTTP method
@@ -176,10 +176,13 @@ public void markAsRead() throws IOException { | |||
* the io exception | |||
*/ | |||
public GHSubscription subscribe(boolean subscribed, boolean ignored) throws IOException { | |||
return new Requester(root).with("subscribed", subscribed) | |||
return root.retrieve() | |||
.method("POST") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant method("POST")
@@ -33,7 +33,7 @@ private TreeEntry(String path, String mode, String type) { | |||
|
|||
GHTreeBuilder(GHRepository repo) { | |||
this.repo = repo; | |||
req = new Requester(repo.root); | |||
req = repo.root.retrieve().method("POST"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could remove method("POST")
as you set the HTTP method on create
|
||
for (Iterator<Entry> it = args.listIterator(); it.hasNext();) { | ||
Entry arg = it.next(); | ||
argString.append(URLEncoder.encode(arg.key, "UTF-8")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe using this one https://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html#UTF_8 instead of the hardcoded value? (no biggie tbh)
throw new AssertionError(e); // UTF-8 is mandatory | ||
} | ||
} | ||
String tailApiUrl = buildTailApiUrl(urlPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🥇
@@ -2,7 +2,7 @@ | |||
"id": "a979348d-c6be-4cb7-8877-7c42a6f013ae", | |||
"name": "notifications", | |||
"request": { | |||
"url": "/notifications?all=true&page=9&all=true", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:)
My pleasure ;) @bitwiseman looks great! This should open the possibilities for a lot of improvements that we always wanted to do 🍾 The only thing which I still struggling is the name of the GHHook hook = root.retrieve()
.method("POST")
.with("name", name)
.with("active", active)
.with("config", config)
.with("events", ea)
.withUrlPath(collection())
.to(clazz()); In the case above it looks like it's performing a root.retrieve().method("PATCH").withUrlPath("/user/repository_invitations/" + id).to(); This looks like it's performing a Like any other developer, I too struggle with coming up with variable/method names 😄 but if you could think of a name for the former |
@PauloMigAlmeida Naming: Agreed, it could be better. The |
6ba9a3d
to
a6945bb
Compare
a6945bb
to
40f05e4
Compare
This removes a second code path through pagination.