diff options
author | Chris Aniszczyk | 2011-05-10 20:07:33 +0000 |
---|---|---|
committer | Code Review | 2011-05-10 20:07:33 +0000 |
commit | 6e1c81318c09779b3e924a8b74a0d064df767615 (patch) | |
tree | cba676fdadc15ad9b9242afa73154382d3024b2c /org.eclipse.mylyn.github.core/src | |
parent | 6d072766314c5371500062459ab41d844e2f86dd (diff) | |
parent | 0a52dc80a7f0263d43a7ac2f0a5d4803e7e963a0 (diff) | |
download | egit-github-6e1c81318c09779b3e924a8b74a0d064df767615.tar.gz egit-github-6e1c81318c09779b3e924a8b74a0d064df767615.tar.xz egit-github-6e1c81318c09779b3e924a8b74a0d064df767615.zip |
Merge "Migrate services to support pagination"
Diffstat (limited to 'org.eclipse.mylyn.github.core/src')
9 files changed, 182 insertions, 105 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java index 9ed8c69d..7ed5925c 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GistService.java @@ -10,6 +10,8 @@ *******************************************************************************/ package org.eclipse.mylyn.github.internal; +import com.google.gson.reflect.TypeToken; + import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -17,14 +19,10 @@ import java.util.Map; import org.eclipse.core.runtime.Assert; -import com.google.gson.reflect.TypeToken; - /** * Service class for getting and list gists. */ -public class GistService { - - private GitHubClient client; +public class GistService extends GitHubService { /** * Create gist service @@ -32,8 +30,7 @@ public class GistService { * @param client */ public GistService(GitHubClient client) { - Assert.isNotNull(client, "Client cannot be null"); - this.client = client; + super(client); } /** @@ -47,7 +44,10 @@ public class GistService { Assert.isNotNull(id, "Gist id cannot be null"); StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_GISTS); uri.append('/').append(id).append(IGitHubConstants.SUFFIX_JSON); - return this.client.get(uri.toString(), Gist.class); + GitHubRequest request = new GitHubRequest(); + request.setUri(uri); + request.setType(Gist.class); + return (Gist) this.client.get(request).getBody(); } /** @@ -63,9 +63,12 @@ public class GistService { uri.append('/').append(user); uri.append(IGitHubConstants.SEGMENT_GISTS).append( IGitHubConstants.SUFFIX_JSON); - TypeToken<List<Gist>> gistToken = new TypeToken<List<Gist>>() { - }; - return this.client.get(uri.toString(), gistToken.getType()); + ListResourceCollector<Gist> collector = new ListResourceCollector<Gist>(); + PagedRequest<Gist> request = new PagedRequest<Gist>(collector); + request.setUri(uri).setType(new TypeToken<List<Gist>>() { + }.getType()); + getAll(request); + return collector.getResources(); } /** @@ -141,10 +144,12 @@ public class GistService { uri.append('/').append(gistId); uri.append(IGitHubConstants.SEGMENT_COMMENTS).append( IGitHubConstants.SUFFIX_JSON); - - TypeToken<List<Comment>> commentToken = new TypeToken<List<Comment>>() { - }; - return this.client.get(uri.toString(), commentToken.getType()); + ListResourceCollector<Comment> collector = new ListResourceCollector<Comment>(); + PagedRequest<Comment> request = new PagedRequest<Comment>(collector); + request.setUri(uri).setType(new TypeToken<List<Comment>>() { + }.getType()); + getAll(request); + return collector.getResources(); } } diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java index 8507280d..ad5cd005 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubClient.java @@ -169,11 +169,14 @@ public class GitHubClient { * Get name value pairs for data map. * * @param data + * @param page * @return name value pair array */ - protected NameValuePair[] getPairs(Map<String, String> data) { + protected NameValuePair[] getPairs(Map<String, String> data, int page) { if (data == null || data.isEmpty()) - return new NameValuePair[] { PER_PAGE_PARAM }; + return new NameValuePair[] { + new NameValuePair(IGitHubConstants.PARAM_PAGE, + Integer.toString(page)), PER_PAGE_PARAM }; int size = data.containsKey(IGitHubConstants.PARAM_PER_PAGE) ? data .size() : data.size() + 1; @@ -187,32 +190,18 @@ public class GitHubClient { } /** - * Get response from uri and bind to specified type - * - * @param <V> - * @param uri - * @param type - * @return V - * @throws IOException - */ - public <V> V get(String uri, Type type) throws IOException { - return get(uri, null, type); - } - - /** * Get response stream from uri. It is the responsibility of the calling * method to close the returned stream. * - * @param uri - * @param params - * @return V + * @param request + * @return stream * @throws IOException */ - public InputStream getStream(String uri, Map<String, String> params) - throws IOException { - GetMethod method = createGet(uri); - method.setQueryString(getPairs(params)); - + public InputStream getStream(GitHubRequest request) throws IOException { + GetMethod method = createGet(request.getUri()); + if (method.getQueryString() == null) + method.setQueryString(getPairs(request.getParams(), + request.getPage())); try { int status = this.client.executeMethod(this.hostConfig, method); switch (status) { @@ -222,6 +211,7 @@ public class GitHubClient { case 401: case 403: case 404: + case 422: case 500: RequestError error = parseJson(method, RequestError.class); throw new RequestException(error, status); @@ -236,27 +226,26 @@ public class GitHubClient { /** * Get response from uri and bind to specified type * - * @param <V> - * @param uri - * @param params - * @param type - * @return V + * @param request + * @return response * @throws IOException */ - public <V> V get(String uri, Map<String, String> params, Type type) - throws IOException { - GetMethod method = createGet(uri); - method.setQueryString(getPairs(params)); - + public GitHubResponse get(GitHubRequest request) throws IOException { + GetMethod method = createGet(request.getUri()); + if (method.getQueryString() == null) + method.setQueryString(getPairs(request.getParams(), + request.getPage())); try { int status = this.client.executeMethod(this.hostConfig, method); switch (status) { case 200: - return parseJson(method, type); + return new GitHubResponse(method, parseJson(method, + request.getType())); case 400: case 401: case 403: case 404: + case 422: case 500: RequestError error = parseJson(method, RequestError.class); throw new RequestException(error, status); @@ -303,6 +292,7 @@ public class GitHubClient { case 401: case 403: case 404: + case 422: case 500: RequestError error = parseJson(method, RequestError.class); throw new RequestException(error, status); diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java new file mode 100644 index 00000000..5c78aee0 --- /dev/null +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubService.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Sawicki (GitHub Inc.) - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.github.internal; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +import org.eclipse.core.runtime.Assert; + +/** + * Base GitHub service class. + */ +public abstract class GitHubService { + + /** + * Client field + */ + protected GitHubClient client; + + /** + * Create service for client + * + * @param client + */ + public GitHubService(GitHubClient client) { + Assert.isNotNull(client, "Client cannot be null"); //$NON-NLS-1$ + this.client = client; + } + + /** + * Get paged request by performing multiple requests until no more pages are + * available of the request collector no longer accepts resource results. + * + * @param <V> + * @param request + * @throws IOException + */ + @SuppressWarnings("unchecked") + protected <V> void getAll(PagedRequest<V> request) throws IOException { + IResourceCollector<V> collector = request.getCollector(); + String next = null; + int page = 1; + do { + GitHubResponse response = client.get(request); + Collection<V> resources = null; + Object body = response.getBody(); + if (body instanceof Collection) + resources = (Collection<V>) body; + else if (body instanceof IResourceProvider) + resources = ((IResourceProvider<V>) body).getResources(); + else + resources = (Collection<V>) Collections.singletonList(body); + + if (!collector.accept(page, resources)) + break; + next = response.getNext(); + if (next != null) + request.setUri(next); + page++; + } while (next != null); + } +} diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/IssueService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/IssueService.java index c623c9fd..b3ceb1e9 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/IssueService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/IssueService.java @@ -25,7 +25,7 @@ import org.eclipse.core.runtime.Assert; * * @author Kevin Sawicki (kevin@github.com) */ -public class IssueService { +public class IssueService extends GitHubService { /** * Filter by issue assignee @@ -72,8 +72,6 @@ public class IssueService { */ public static final String FIELD_TITLE = "title"; //$NON-NLS-1$ - private GitHubClient client; - /** * Create issue service * @@ -81,8 +79,7 @@ public class IssueService { * cannot be null */ public IssueService(GitHubClient client) { - Assert.isNotNull(client, "Client cannot be null"); //$NON-NLS-1$ - this.client = client; + super(client); } /** @@ -104,7 +101,10 @@ public class IssueService { builder.append('/').append(user).append('/').append(repository); builder.append(IGitHubConstants.SEGMENT_ISSUES); builder.append('/').append(id).append(IGitHubConstants.SUFFIX_JSON); - return this.client.get(builder.toString(), Issue.class); + GitHubRequest request = new GitHubRequest(); + request.setUri(builder.toString()); + request.setType(Issue.class); + return (Issue) client.get(request).getBody(); } /** @@ -128,9 +128,13 @@ public class IssueService { builder.append('/').append(id); builder.append(IGitHubConstants.SEGMENT_COMMENTS).append( IGitHubConstants.SUFFIX_JSON); - TypeToken<List<Comment>> commentToken = new TypeToken<List<Comment>>() { - }; - return this.client.get(builder.toString(), commentToken.getType()); + ListResourceCollector<Comment> collector = new ListResourceCollector<Comment>(); + PagedRequest<Comment> request = new PagedRequest<Comment>(collector); + request.setUri(builder.toString()).setType( + new TypeToken<List<Comment>>() { + }.getType()); + getAll(request); + return collector.getResources(); } /** @@ -151,10 +155,13 @@ public class IssueService { builder.append('/').append(user).append('/').append(repository); builder.append(IGitHubConstants.SEGMENT_ISSUES).append( IGitHubConstants.SUFFIX_JSON); - TypeToken<List<Issue>> issueToken = new TypeToken<List<Issue>>() { - }; - return this.client.get(builder.toString(), filterData, - issueToken.getType()); + ListResourceCollector<Issue> collector = new ListResourceCollector<Issue>(); + PagedRequest<Issue> request = new PagedRequest<Issue>(collector); + request.setParams(filterData).setUri(builder.toString()); + request.setType(new TypeToken<List<Issue>>() { + }.getType()); + getAll(request); + return collector.getResources(); } /** diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java index 6b243713..f1e5ceed 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/LabelService.java @@ -21,9 +21,7 @@ import org.eclipse.core.runtime.Assert; * Label service class for listing {@link Label} objects in use for a given user * and repository. */ -public class LabelService { - - private GitHubClient client; +public class LabelService extends GitHubService { /** * Create label service for client @@ -31,8 +29,7 @@ public class LabelService { * @param client */ public LabelService(GitHubClient client) { - Assert.isNotNull(client, "Client cannot be null"); //$NON-NLS-1$ - this.client = client; + super(client); } /** @@ -51,9 +48,12 @@ public class LabelService { uri.append('/').append(user).append('/').append(repository); uri.append(IGitHubConstants.SEGMENT_LABELS).append( IGitHubConstants.SUFFIX_JSON); - TypeToken<List<Label>> labelToken = new TypeToken<List<Label>>() { - }; - return this.client.get(uri.toString(), labelToken.getType()); + ListResourceCollector<Label> collector = new ListResourceCollector<Label>(); + PagedRequest<Label> request = new PagedRequest<Label>(collector); + request.setUri(uri).setType(new TypeToken<List<Label>>() { + }.getType()); + getAll(request); + return collector.getResources(); } /** diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/MilestoneService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/MilestoneService.java index f61422ef..cfe3751c 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/MilestoneService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/MilestoneService.java @@ -15,7 +15,6 @@ import com.google.gson.reflect.TypeToken; import java.io.IOException; import java.util.Collections; import java.util.List; -import java.util.Map; import org.eclipse.core.runtime.Assert; @@ -23,9 +22,7 @@ import org.eclipse.core.runtime.Assert; * Milestone service class for listing the {@link Milestone} objects in use by a * repository and user accessed via a {@link GitHubClient}. */ -public class MilestoneService { - - private GitHubClient client; +public class MilestoneService extends GitHubService { /** * Create milestone service @@ -34,8 +31,7 @@ public class MilestoneService { * cannot be null */ public MilestoneService(GitHubClient client) { - Assert.isNotNull(client, "Client cannot be null"); //$NON-NLS-1$ - this.client = client; + super(client); } /** @@ -55,14 +51,14 @@ public class MilestoneService { uri.append('/').append(user).append('/').append(repository); uri.append(IGitHubConstants.SEGMENT_MILESTONES).append( IGitHubConstants.SUFFIX_JSON); - Map<String, String> params = null; - if (state != null) { - params = Collections.singletonMap(IssueService.FILTER_STATE, state); - } - TypeToken<List<Milestone>> milestoneToken = new TypeToken<List<Milestone>>() { - }; - return this.client - .get(uri.toString(), params, milestoneToken.getType()); + ListResourceCollector<Milestone> collector = new ListResourceCollector<Milestone>(); + PagedRequest<Milestone> request = new PagedRequest<Milestone>(collector); + if (state != null) + request.setParams(Collections.singletonMap( + IssueService.FILTER_STATE, state)); + request.setUri(uri).setType(new TypeToken<List<Milestone>>() { + }.getType()); + getAll(request); + return collector.getResources(); } - } diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PullRequestService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PullRequestService.java index 496a21f6..0ada6ccf 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PullRequestService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PullRequestService.java @@ -77,9 +77,11 @@ public class PullRequestService { uri.append(IGitHubConstants.SEGMENT_PULLS); uri.append('/').append(repositoryId); uri.append('/').append(id); - PullRequestWrapper wrapper = this.client.get(uri.toString(), - PullRequestWrapper.class); - return wrapper.getPull(); + GitHubRequest request = new GitHubRequest(); + request.setUri(uri); + request.setType(PullRequestWrapper.class); + GitHubResponse response = this.client.get(request); + return ((PullRequestWrapper) response.getBody()).getPull(); } /** @@ -100,8 +102,10 @@ public class PullRequestService { uri.append(IGitHubConstants.SEGMENT_PULLS); uri.append('/').append(repositoryId); uri.append('/').append(state); - PullRequestsWrapper wrapper = this.client.get(uri.toString(), - PullRequestsWrapper.class); - return wrapper.getPulls(); + GitHubRequest request = new GitHubRequest(); + request.setUri(uri); + request.setType(PullRequestsWrapper.class); + GitHubResponse response = this.client.get(request); + return ((PullRequestsWrapper) response.getBody()).getPulls(); } } diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/RepositoryService.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/RepositoryService.java index 4d24de16..6c3c9871 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/RepositoryService.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/RepositoryService.java @@ -13,16 +13,12 @@ package org.eclipse.mylyn.github.internal; import java.io.IOException; import java.util.List; -import org.eclipse.core.runtime.Assert; - /** * Repository service class. * * @author Kevin Sawicki (kevin@github.com) */ -public class RepositoryService { - - private GitHubClient client; +public class RepositoryService extends GitHubService { /** * Create repository service @@ -31,8 +27,7 @@ public class RepositoryService { * cannot be null */ public RepositoryService(GitHubClient client) { - Assert.isNotNull(client, "Client cannot be null"); //$NON-NLS-1$ - this.client = client; + super(client); } /** @@ -47,9 +42,12 @@ public class RepositoryService { uri.append(IGitHubConstants.SEGMENT_ORGANIZATIONS).append( IGitHubConstants.SEGMENT_REPOSITORIES); - RepositoryContainer container = client.get(uri.toString(), - RepositoryContainer.class); - return container.getResources(); + ListResourceCollector<Repository> collector = new ListResourceCollector<Repository>(); + PagedRequest<Repository> request = new PagedRequest<Repository>( + collector); + request.setUri(uri); + getAll(request); + return collector.getResources(); } /** @@ -64,8 +62,11 @@ public class RepositoryService { uri.append(IGitHubConstants.SEGMENT_REPOS) .append(IGitHubConstants.SEGMENT_SHOW).append('/').append(user); - RepositoryContainer container = client.get(uri.toString(), - RepositoryContainer.class); - return container.getResources(); + ListResourceCollector<Repository> collector = new ListResourceCollector<Repository>(); + PagedRequest<Repository> request = new PagedRequest<Repository>( + collector); + request.setUri(uri); + getAll(request); + return collector.getResources(); } } diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java index 15bb525a..9f5bdc84 100644 --- a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/internal/github/core/gist/GistAttachmentHandler.java @@ -24,6 +24,7 @@ import org.eclipse.mylyn.github.internal.GistFile; import org.eclipse.mylyn.github.internal.GistService; import org.eclipse.mylyn.github.internal.GitHub; import org.eclipse.mylyn.github.internal.GitHubClient; +import org.eclipse.mylyn.github.internal.GitHubRequest; import org.eclipse.mylyn.tasks.core.ITask; import org.eclipse.mylyn.tasks.core.TaskRepository; import org.eclipse.mylyn.tasks.core.data.AbstractTaskAttachmentHandler; @@ -71,7 +72,9 @@ public class GistAttachmentHandler extends AbstractTaskAttachmentHandler { TaskAttribute urlAttribute = attachmentAttribute .getAttribute(GistAttribute.RAW_FILE_URL.getId()); try { - return client.getStream(urlAttribute.getValue(), null); + GitHubRequest request = new GitHubRequest(); + request.setUri(urlAttribute.getValue()); + return client.getStream(request); } catch (IOException e) { throw new CoreException(GitHub.createErrorStatus(e)); } |