Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5c78aee02dcffec4af94a5e31c7357ac71082a06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
	}
}

Back to the top