diff options
| author | Kevin Sawicki | 2011-05-02 23:40:50 +0000 |
|---|---|---|
| committer | Chris Aniszczyk | 2011-05-03 14:06:56 +0000 |
| commit | f6a43aac89499faa78a2c30fb2d0415dc37b800a (patch) | |
| tree | ab70f672d7f761ac359b5543b90f6feed38c1b15 | |
| parent | 6180fab51874473a6dc4f423f1d278d528dd5978 (diff) | |
| download | egit-github-f6a43aac89499faa78a2c30fb2d0415dc37b800a.tar.gz egit-github-f6a43aac89499faa78a2c30fb2d0415dc37b800a.tar.xz egit-github-f6a43aac89499faa78a2c30fb2d0415dc37b800a.zip | |
Add response class and link header support
Responses that contain paginated results contain a
Link header to request uris relative to the current
response.
Change-Id: I372bb065b6fa2937c0039491851a03547f7a0b90
Signed-off-by: Kevin Sawicki <kevin@github.com>
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
3 files changed, 234 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java new file mode 100644 index 00000000..e10d39af --- /dev/null +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/GitHubResponse.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * 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 org.apache.commons.httpclient.HttpMethod; + +/** + * GitHub API response class. + */ +public class GitHubResponse { + + private PageLinks links; + + private Object body; + + /** + * Create response + * + * @param method + * @param body + */ + public GitHubResponse(HttpMethod method, Object body) { + links = new PageLinks(method); + this.body = body; + } + + /** + * Get link uri to first page + * + * @return possibly null uri + */ + public String getFirst() { + return this.links.getFirst(); + } + + /** + * Get link uri to previous page + * + * @return possibly null uri + */ + public String getPrevious() { + return this.links.getPrev(); + } + + /** + * Get link uri to next page + * + * @return possibly null uri + */ + public String getNext() { + return this.links.getNext(); + } + + /** + * Get link uri to last page + * + * @return possibly null uri + */ + public String getLast() { + return this.links.getLast(); + } + + /** + * Parsed response body + * + * @return body + */ + public Object getBody() { + return this.body; + } + +} diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java new file mode 100644 index 00000000..0d5a030a --- /dev/null +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PageLinks.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * 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 org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpMethod; + +/** + * Page link class to be used to determine the links to other pages of request + * responses encoded in the current response. These will be present if the + * result set size exceeds the per page limit. + */ +public class PageLinks { + + private static final String DELIM_LINKS = ","; //$NON-NLS-1$ + + private static final String DELIM_LINK_PARAM = ";"; //$NON-NLS-1;$ + + private String first; + private String last; + private String next; + private String prev; + + /** + * Parse links from executed method + * + * @param method + */ + public PageLinks(HttpMethod method) { + Header[] linkHeaders = method + .getResponseHeaders(IGitHubConstants.HEADER_LINK); + if (linkHeaders.length > 0) { + String[] links = linkHeaders[0].getValue().split(DELIM_LINKS); + for (String link : links) { + String[] segments = link.split(DELIM_LINK_PARAM); + if (segments.length < 2) + continue; + + String linkPart = segments[0].trim(); + if (!linkPart.startsWith("<") || !linkPart.endsWith(">")) //$NON-NLS-1$ //$NON-NLS-2$ + continue; + linkPart = linkPart.substring(1, linkPart.length() - 1); + + for (int i = 1; i < segments.length; i++) { + String[] rel = segments[i].trim().split("="); //$NON-NLS-1$ + if (rel.length < 2 + || !IGitHubConstants.META_REL.equals(rel[0])) + continue; + + String relValue = rel[1]; + if (relValue.startsWith("\"") && relValue.endsWith("\"")) //$NON-NLS-1$ //$NON-NLS-2$ + relValue = relValue.substring(1, relValue.length() - 1); + + if (IGitHubConstants.META_FIRST.equals(relValue)) + first = linkPart; + else if (IGitHubConstants.META_LAST.equals(relValue)) + last = linkPart; + else if (IGitHubConstants.META_NEXT.equals(relValue)) + next = linkPart; + else if (IGitHubConstants.META_PREV.equals(relValue)) + prev = linkPart; + } + } + } else { + Header[] nextHeaders = method + .getResponseHeaders(IGitHubConstants.HEADER_NEXT); + if (nextHeaders.length > 0) + next = nextHeaders[0].getValue(); + + Header[] lastHeaders = method + .getResponseHeaders(IGitHubConstants.HEADER_LAST); + if (lastHeaders.length > 0) + last = lastHeaders[0].getValue(); + } + } + + /** + * @return first + */ + public String getFirst() { + return this.first; + } + + /** + * @return last + */ + public String getLast() { + return this.last; + } + + /** + * @return next + */ + public String getNext() { + return this.next; + } + + /** + * @return prev + */ + public String getPrev() { + return this.prev; + } +} diff --git a/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java new file mode 100644 index 00000000..b61f0e07 --- /dev/null +++ b/org.eclipse.mylyn.github.core/src/org/eclipse/mylyn/github/internal/PagedRequest.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * 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 org.eclipse.core.runtime.Assert; + +/** + * Pages request class that contains a collector for accept resources page by + * page. + * + * @param <V> + */ +public class PagedRequest<V> extends GitHubRequest { + + private IResourceCollector<V> collector; + + /** + * Create paged request with non-null collector + * + * @param collector + */ + public PagedRequest(IResourceCollector<V> collector) { + Assert.isNotNull(collector, "Collecto cannot be null"); //$NON-NLS-1$ + this.collector = collector; + } + + /** + * @return collector + */ + public IResourceCollector<V> getCollector() { + return this.collector; + } + +} |
