Skip to content
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

Merged
merged 8 commits into from
Dec 17, 2019

Conversation

bitwiseman
Copy link
Member

This removes a second code path through pagination.

@bitwiseman
Copy link
Member Author

@PauloMigAlmeida
Any thoughts on this? I don't have a lot of folks to ask for code review, so you get the honors. 😄

@@ -425,7 +425,7 @@ URL getApiURL(String tailApiUrl) throws IOException {
}

Requester retrieve() {
return new Requester(this).method("GET");
return new Requester(this);
Copy link
Member Author

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;
Copy link
Member Author

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++) {
Copy link
Member Author

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) {
Copy link
Member Author

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 {
Copy link
Member Author

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");
Copy link
Member Author

@bitwiseman bitwiseman Dec 13, 2019

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",
Copy link
Member Author

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!

Copy link
Contributor

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");
Copy link
Contributor

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();
Copy link
Contributor

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");
Copy link
Contributor

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);
Copy link
Contributor

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;
Copy link
Contributor

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")
Copy link
Contributor

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");
Copy link
Contributor

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"));
Copy link
Contributor

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);
Copy link
Contributor

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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

@PauloMigAlmeida
Copy link
Contributor

@PauloMigAlmeida
Any thoughts on this? I don't have a lot of folks to ask for code review, so you get the honors. 😄

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 to method. Now that is doesn't take the URL path anymore, it sounds a bit funny. For instance:

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 parseTo/fromJson kind of thing, while in this other below:

root.retrieve().method("PATCH").withUrlPath("/user/repository_invitations/" + id).to();

This looks like it's performing a execute/sendRequest.

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 to() method that can read nicely in both scenarios then it's perfect 👍

@bitwiseman
Copy link
Member Author

@PauloMigAlmeida
Okay, I'll go through and clean up all the redundant method() calls. Thanks!

Naming: Agreed, it could be better. The to() method should really be something like send().

@bitwiseman bitwiseman force-pushed the task/iterator branch 2 times, most recently from 6ba9a3d to a6945bb Compare December 17, 2019 17:49
@bitwiseman bitwiseman merged commit 6bc617c into hub4j:master Dec 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants