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

Refactor Requester into multiple smaller classes #697

Merged
merged 17 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotbugs-maven-plugin.version>3.1.12.2</spotbugs-maven-plugin.version>
<spotbugs.version>3.1.12</spotbugs.version>
<spotbugs.version>4.0.0-RC3</spotbugs.version>
<spotbugs-maven-plugin.failOnError>true</spotbugs-maven-plugin.failOnError>
<hamcrest.version>2.2</hamcrest.version>
<okhttp3.version>4.3.1</okhttp3.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public GHAppCreateTokenBuilder repositoryIds(List<Long> repositoryIds) {
public GHAppCreateTokenBuilder permissions(Map<String, GHPermissionType> permissions) {
Map<String, String> retMap = new HashMap<>();
for (Map.Entry<String, GHPermissionType> entry : permissions.entrySet()) {
retMap.put(entry.getKey(), Requester.transformEnum(entry.getValue()));
retMap.put(entry.getKey(), GitHubRequest.transformEnum(entry.getValue()));
}
builder.with("permissions", retMap);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static String getRepoName(String commitUrl) {
if (StringUtils.isBlank(commitUrl)) {
return null;
}
int indexOfUsername = (GitHub.GITHUB_URL + "/repos/").length();
int indexOfUsername = (GitHubClient.GITHUB_URL + "/repos/").length();
String[] tokens = commitUrl.substring(indexOfUsername).split("/", 3);
return tokens[0] + '/' + tokens[1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/GHEventInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public GHOrganization getOrganization() throws IOException {
* if payload cannot be parsed
*/
public <T extends GHEventPayload> T getPayload(Class<T> type) throws IOException {
T v = GitHub.MAPPER.readValue(payload.traverse(), type);
T v = GitHubClient.MAPPER.readValue(payload.traverse(), type);
v.wrapUp(root);
return v;
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/kohsuke/github/GHFileNotFoundException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.kohsuke.github;

import java.io.FileNotFoundException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Request/responce contains useful metadata. Custom exception allows store info for next diagnostics.
Expand Down Expand Up @@ -54,8 +54,8 @@ public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}

GHFileNotFoundException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
GHFileNotFoundException withResponseHeaderFields(@Nonnull Map<String, List<String>> headerFields) {
this.responseHeaderFields = headerFields;
return this;
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/kohsuke/github/GHIOException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.kohsuke.github;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Request/responce contains useful metadata. Custom exception allows store info for next diagnostics.
Expand Down Expand Up @@ -55,8 +55,8 @@ public Map<String, List<String>> getResponseHeaderFields() {
return responseHeaderFields;
}

GHIOException withResponseHeaderFields(HttpURLConnection urlConnection) {
this.responseHeaderFields = urlConnection.getHeaderFields();
GHIOException withResponseHeaderFields(@Nonnull Map<String, List<String>> headerFields) {
this.responseHeaderFields = headerFields;
return this;
}
}
18 changes: 10 additions & 8 deletions src/main/java/org/kohsuke/github/GHNotificationStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ GHThread fetch() {

req.setHeader("If-Modified-Since", lastModified);

threads = req.withUrlPath(apiUrl).fetchArray(GHThread[].class);
Requester requester = req.withUrlPath(apiUrl);
GitHubResponse<GHThread[]> response = ((GitHubPageContentsIterable<GHThread>) requester
.toIterable(requester.client, GHThread[].class, null)).toResponse();
threads = response.body();

if (threads == null) {
threads = EMPTY_ARRAY; // if unmodified, we get empty array
} else {
Expand All @@ -189,18 +193,16 @@ GHThread fetch() {
}
idx = threads.length - 1;

nextCheckTime = calcNextCheckTime();
lastModified = req.getResponseHeader("Last-Modified");
nextCheckTime = calcNextCheckTime(response);
lastModified = response.headerField("Last-Modified");
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}

private long calcNextCheckTime() {
String v = req.getResponseHeader("X-Poll-Interval");
private long calcNextCheckTime(GitHubResponse<GHThread[]> response) {
String v = response.headerField("X-Poll-Interval");
if (v == null)
v = "60";
long seconds = Integer.parseInt(v);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/kohsuke/github/GHPerson.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public PagedIterable<GHRepository> listRepositories(final int pageSize) {
public synchronized Iterable<List<GHRepository>> iterateRepositories(final int pageSize) {
return new Iterable<List<GHRepository>>() {
public Iterator<List<GHRepository>> iterator() {
final Iterator<GHRepository[]> pager = root.createRequest()
.withUrlPath("users", login, "repos")
.asIterator(GHRepository[].class, pageSize);
final Iterator<GHRepository[]> pager = GitHubPageIterator.create(root.getClient(),
GHRepository[].class,
root.createRequest().withUrlPath("users", login, "repos").withPageSize(pageSize));

return new Iterator<List<GHRepository>>() {
public boolean hasNext() {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/kohsuke/github/GHSearchBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nonnull;

/**
* Base class for various search builders.
*
Expand Down Expand Up @@ -43,9 +45,11 @@ public GHQueryBuilder<T> q(String term) {
@Override
public PagedSearchIterable<T> list() {
return new PagedSearchIterable<T>(root) {
@Nonnull
public PagedIterator<T> _iterator(int pageSize) {
req.set("q", StringUtils.join(terms, " "));
return new PagedIterator<T>(adapt(req.withUrlPath(getApiUrl()).asIterator(receiverType, pageSize))) {
return new PagedIterator<T>(adapt(GitHubPageIterator
.create(req.client, receiverType, req.withUrlPath(getApiUrl()).withPageSize(pageSize)))) {
protected void wrapUp(T[] page) {
// SearchResult.getItems() should do it
}
Expand Down
Loading