public class GitHubClient
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
protected java.lang.String |
baseUri
Base URI
|
protected com.google.gson.Gson |
gson
Gson instance |
protected static java.lang.String |
HEADER_ACCEPT
Accept header
|
protected static java.lang.String |
HEADER_AUTHORIZATION
Authorization header
|
protected static java.lang.String |
HEADER_CONTENT_TYPE
Content-Type header
|
protected static java.lang.String |
HEADER_USER_AGENT
User-Agent header
|
protected static int |
HTTP_UNPROCESSABLE_ENTITY
422 status code for unprocessable entity
|
protected static java.lang.String |
METHOD_DELETE
METHOD_DELETE
|
protected static java.lang.String |
METHOD_GET
METHOD_GET
|
protected static java.lang.String |
METHOD_POST
METHOD_POST
|
protected static java.lang.String |
METHOD_PUT
METHOD_PUT
|
protected java.lang.String |
prefix
Prefix to apply to base URI
|
protected static java.lang.String |
USER_AGENT
Default user agent request header value
|
| Constructor and Description |
|---|
GitHubClient()
Create default client
|
GitHubClient(java.lang.String hostname)
Create client for host name
|
GitHubClient(java.lang.String hostname,
int port,
java.lang.String scheme)
Create client for host, port, and scheme
|
| Modifier and Type | Method and Description |
|---|---|
protected java.net.HttpURLConnection |
configureRequest(java.net.HttpURLConnection request)
Configure request with standard headers
|
protected java.lang.String |
configureUri(java.lang.String uri)
Configure request URI
|
static GitHubClient |
createClient(java.lang.String url)
Create API v3 client from URL.
|
protected java.net.HttpURLConnection |
createConnection(java.lang.String uri)
Create connection to URI
|
protected java.net.HttpURLConnection |
createConnection(java.lang.String uri,
java.lang.String method)
Create connection to URI
|
protected java.net.HttpURLConnection |
createDelete(java.lang.String uri)
Create a DELETE request connection to the URI
|
protected java.io.IOException |
createException(java.io.InputStream response,
int code,
java.lang.String status)
Create error exception from response and throw it
|
protected java.net.HttpURLConnection |
createGet(java.lang.String uri)
Create a GET request connection to the URI
|
protected java.net.HttpURLConnection |
createPost(java.lang.String uri)
Create a POST request connection to the URI
|
protected java.net.HttpURLConnection |
createPut(java.lang.String uri)
Create a PUT request connection to the URI
|
protected java.lang.String |
createUri(java.lang.String path)
Create full URI from path
|
void |
delete(java.lang.String uri)
Delete resource at URI.
|
void |
delete(java.lang.String uri,
java.lang.Object params)
Delete resource at URI.
|
GitHubResponse |
get(GitHubRequest request)
Get response from URI and bind to specified type
|
protected java.lang.Object |
getBody(GitHubRequest request,
java.io.InputStream stream)
Get body from response inputs stream
|
java.lang.String |
getHeaderAccept()
Returns the accept header currently used for all requests.
|
int |
getRemainingRequests()
Get number of requests remaining before rate limiting occurs
|
int |
getRequestLimit()
Get number of requests that
getRemainingRequests() counts down
from as each request is made |
protected java.io.InputStream |
getResponseStream(java.net.HttpURLConnection request)
Get response stream for request
|
java.io.InputStream |
getStream(GitHubRequest request)
Get response stream from GET to URI.
|
protected java.io.InputStream |
getStream(java.net.HttpURLConnection request)
Get stream from request
|
java.lang.String |
getUser()
Get the user that this client is currently authenticating as
|
protected boolean |
isEmpty(int code)
Is the response empty?
|
protected boolean |
isError(int code)
Does status code denote an error
|
protected boolean |
isOk(int code)
Does status code denote a non-error response?
|
protected RequestError |
parseError(java.io.InputStream response)
Parse error from response
|
protected <V> V |
parseJson(java.io.InputStream stream,
java.lang.reflect.Type type)
Parse JSON to specified type
|
protected <V> V |
parseJson(java.io.InputStream stream,
java.lang.reflect.Type type,
java.lang.reflect.Type listType)
Parse JSON to specified type
|
void |
post(java.lang.String uri)
Post to URI
|
<V> V |
post(java.lang.String uri,
java.lang.Object params,
java.lang.reflect.Type type)
Post data to URI
|
java.io.InputStream |
postStream(java.lang.String uri,
java.lang.Object params)
Get response stream from POST to URI.
|
void |
put(java.lang.String uri)
Put to URI
|
<V> V |
put(java.lang.String uri,
java.lang.Object params,
java.lang.reflect.Type type)
Put data to URI
|
protected void |
sendParams(java.net.HttpURLConnection request,
java.lang.Object params)
Send parameters to output stream of request
|
GitHubClient |
setBufferSize(int bufferSize)
Set buffer size used to send the request and read the response
|
GitHubClient |
setCredentials(java.lang.String user,
java.lang.String password)
Set credentials
|
GitHubClient |
setHeaderAccept(java.lang.String header)
Set the value to set as the accept header on every request created.
|
GitHubClient |
setOAuth2Token(java.lang.String token)
Set OAuth2 token
|
GitHubClient |
setSerializeNulls(boolean serializeNulls)
Set whether or not serialized data should include fields that are null.
|
GitHubClient |
setUserAgent(java.lang.String agent)
Set the value to set as the user agent header on every request created.
|
protected java.lang.String |
toJson(java.lang.Object object)
Convert object to a JSON string
|
protected GitHubClient |
updateRateLimits(java.net.HttpURLConnection request)
Update rate limits present in response headers
|
protected static final java.lang.String HEADER_CONTENT_TYPE
protected static final java.lang.String HEADER_ACCEPT
protected static final java.lang.String HEADER_AUTHORIZATION
protected static final java.lang.String HEADER_USER_AGENT
protected static final java.lang.String METHOD_GET
protected static final java.lang.String METHOD_PUT
protected static final java.lang.String METHOD_POST
protected static final java.lang.String METHOD_DELETE
protected static final java.lang.String USER_AGENT
protected static final int HTTP_UNPROCESSABLE_ENTITY
protected final java.lang.String baseUri
protected final java.lang.String prefix
protected com.google.gson.Gson gson
Gson instancepublic GitHubClient()
public GitHubClient(java.lang.String hostname)
hostname - public GitHubClient(java.lang.String hostname,
int port,
java.lang.String scheme)
hostname - port - scheme - public static GitHubClient createClient(java.lang.String url)
This creates an HTTPS-based client with a host that contains the host value of the given URL prefixed with 'api' if the given URL is github.com or gist.github.com
url - public GitHubClient setSerializeNulls(boolean serializeNulls)
serializeNulls - public GitHubClient setUserAgent(java.lang.String agent)
agent - public GitHubClient setHeaderAccept(java.lang.String header)
header - public java.lang.String getHeaderAccept()
protected java.net.HttpURLConnection configureRequest(java.net.HttpURLConnection request)
request - protected java.lang.String configureUri(java.lang.String uri)
uri - protected java.net.HttpURLConnection createConnection(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionprotected java.net.HttpURLConnection createConnection(java.lang.String uri,
java.lang.String method)
throws java.io.IOException
uri - method - java.io.IOExceptionprotected java.net.HttpURLConnection createGet(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionprotected java.net.HttpURLConnection createPost(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionprotected java.net.HttpURLConnection createPut(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionprotected java.net.HttpURLConnection createDelete(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionpublic GitHubClient setCredentials(java.lang.String user, java.lang.String password)
user - password - public GitHubClient setOAuth2Token(java.lang.String token)
token - public GitHubClient setBufferSize(int bufferSize)
bufferSize - public java.lang.String getUser()
protected java.lang.String toJson(java.lang.Object object)
throws java.io.IOException
object - java.io.IOExceptionprotected <V> V parseJson(java.io.InputStream stream,
java.lang.reflect.Type type)
throws java.io.IOException
V - stream - type - java.io.IOExceptionprotected <V> V parseJson(java.io.InputStream stream,
java.lang.reflect.Type type,
java.lang.reflect.Type listType)
throws java.io.IOException
V - stream - type - listType - java.io.IOExceptionprotected boolean isError(int code)
code - protected boolean isOk(int code)
code - protected boolean isEmpty(int code)
code - protected RequestError parseError(java.io.InputStream response) throws java.io.IOException
response - java.io.IOExceptionprotected java.lang.Object getBody(GitHubRequest request, java.io.InputStream stream) throws java.io.IOException
request - stream - java.io.IOExceptionprotected java.io.IOException createException(java.io.InputStream response,
int code,
java.lang.String status)
response - code - status - IOExceptionpublic void post(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionpublic void put(java.lang.String uri)
throws java.io.IOException
uri - java.io.IOExceptionpublic void delete(java.lang.String uri)
throws java.io.IOException
IOException
when the response status is not a 204 (No Content).uri - java.io.IOExceptionprotected void sendParams(java.net.HttpURLConnection request,
java.lang.Object params)
throws java.io.IOException
request - params - java.io.IOExceptionprotected java.lang.String createUri(java.lang.String path)
path - public java.io.InputStream getStream(GitHubRequest request) throws java.io.IOException
request - java.io.IOExceptionpublic java.io.InputStream postStream(java.lang.String uri,
java.lang.Object params)
throws java.io.IOException
uri - params - java.io.IOExceptionprotected java.io.InputStream getResponseStream(java.net.HttpURLConnection request)
throws java.io.IOException
request - java.io.IOExceptionprotected java.io.InputStream getStream(java.net.HttpURLConnection request)
throws java.io.IOException
request - java.io.IOExceptionpublic GitHubResponse get(GitHubRequest request) throws java.io.IOException
request - java.io.IOExceptionpublic <V> V post(java.lang.String uri,
java.lang.Object params,
java.lang.reflect.Type type)
throws java.io.IOException
V - uri - params - type - java.io.IOExceptionpublic <V> V put(java.lang.String uri,
java.lang.Object params,
java.lang.reflect.Type type)
throws java.io.IOException
V - uri - params - type - java.io.IOExceptionpublic void delete(java.lang.String uri,
java.lang.Object params)
throws java.io.IOException
IOException
when the response status is not a 204 (No Content).uri - params - java.io.IOExceptionprotected GitHubClient updateRateLimits(java.net.HttpURLConnection request)
request - public int getRemainingRequests()
This will be the value of the 'X-RateLimit-Remaining' header from the last request made
public int getRequestLimit()
getRemainingRequests() counts down
from as each request is made
This will be the value of the 'X-RateLimit-Limit' header from the last request made