com.jakewharton.apibuilder
Class ApiService

java.lang.Object
  extended by com.jakewharton.apibuilder.ApiService

public class ApiService
extends Object

HTTP method execution.

Parts of the code for this class are from Nabeel Mukhtar's github-java-sdk library.

Author:
Jake Wharton

Field Summary
protected static String CONTENT_ENCODING
           
 
Constructor Summary
ApiService()
           
 
Method Summary
protected  void acceptGzip()
          Add the HTTP header denoting that we accept GZIP.
 void addRequestHeader(String name, String value)
          Add an HTTP request header.
protected static void closeConnection(HttpURLConnection connection)
          Close the specified connection.
protected static void closeStream(InputStream is)
          Close the specified stream.
protected static String convertStreamToString(InputStream is)
          Read an entire stream to end and assemble in a string.
protected  InputStream executeDelete(String apiUrl)
          Execute the URL using HTTP DELETE.
protected  InputStream executeDelete(String apiUrl, int expected)
          Execute the URL using HTTP DELETE.
protected  InputStream executeGet(String apiUrl)
          Execute the URL using HTTP GET.
protected  InputStream executeGet(String apiUrl, int expected)
          Execute the URL using HTTP GET.
protected  InputStream executeMethod(String apiUrl, String content, String contentType, String method, int expected)
          Execute URL using the specified HTTP method name.
protected  InputStream executePost(String apiUrl, Map<String,String> parameters)
          Execute the URL using HTTP POST.
protected  InputStream executePost(String apiUrl, Map<String,String> parameters, int expected)
          Execute the URL using HTTP POST
 int getConnectTimeout()
          Get connection timeout value.
protected static String getParametersString(Map<String,String> parameters)
          Assemble a parameter string from a mapping.
 int getReadTimeout()
          Get read timeout value.
protected static InputStream getWrappedInputStream(InputStream is, boolean gzip)
          Properly wrap the stream accounting for GZIP.
 void removeRequestHeader(String name)
          Remove an HTTP request header.
 void setConnectTimeout(int connectTimeout)
          Set the connection timeout value.
 void setReadTimeout(int readTimeout)
          Set read timeout value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONTENT_ENCODING

protected static final String CONTENT_ENCODING
See Also:
Constant Field Values
Constructor Detail

ApiService

public ApiService()
Method Detail

acceptGzip

protected void acceptGzip()
Add the HTTP header denoting that we accept GZIP.


addRequestHeader

public void addRequestHeader(String name,
                             String value)
Add an HTTP request header.

Parameters:
name - Header name.
value - Header value.

removeRequestHeader

public void removeRequestHeader(String name)
Remove an HTTP request header.

Parameters:
name - Header name.

getConnectTimeout

public int getConnectTimeout()
Get connection timeout value.

Returns:
Timeout (in milliseconds).

setConnectTimeout

public void setConnectTimeout(int connectTimeout)
Set the connection timeout value.

Parameters:
connectTimeout - Timeout (in milliseconds).

getReadTimeout

public int getReadTimeout()
Get read timeout value.

Returns:
Timout (in milliseconds).

setReadTimeout

public void setReadTimeout(int readTimeout)
Set read timeout value.

Parameters:
readTimeout - Timeout (in milliseconds).

executeGet

protected InputStream executeGet(String apiUrl)
Execute the URL using HTTP GET.

Parameters:
apiUrl - URL.
Returns:
Response stream.

executeGet

protected InputStream executeGet(String apiUrl,
                                 int expected)
Execute the URL using HTTP GET.

Parameters:
apiUrl - URL.
expected - Expected HTTP response code.
Returns:
Response stream.

executePost

protected InputStream executePost(String apiUrl,
                                  Map<String,String> parameters)
Execute the URL using HTTP POST.

Parameters:
apiUrl - URL.
parameters - POST body parameters.
Returns:
Response stream.

executePost

protected InputStream executePost(String apiUrl,
                                  Map<String,String> parameters,
                                  int expected)
Execute the URL using HTTP POST

Parameters:
apiUrl - URL.
parameters - POST body parameters.
expected - Excepted HTTP response code.
Returns:
Response stream.

executeDelete

protected InputStream executeDelete(String apiUrl)
Execute the URL using HTTP DELETE.

Parameters:
apiUrl - URL.
Returns:
Response stream.

executeDelete

protected InputStream executeDelete(String apiUrl,
                                    int expected)
Execute the URL using HTTP DELETE.

Parameters:
apiUrl - URL.
expected - Expected HTTP response code.
Returns:
Response stream.

executeMethod

protected InputStream executeMethod(String apiUrl,
                                    String content,
                                    String contentType,
                                    String method,
                                    int expected)
Execute URL using the specified HTTP method name.

Parameters:
apiUrl - URL.
content - Request body content.
contentType - Request body content type.
method - HTTP method name.
expected - Expected HTTP response code.
Returns:
Response stream.

getParametersString

protected static String getParametersString(Map<String,String> parameters)
Assemble a parameter string from a mapping.

Parameters:
parameters - Mapping of parameter names to values.
Returns:
String representation.

closeStream

protected static void closeStream(InputStream is)
Close the specified stream.

Parameters:
is - Stream to close.

closeConnection

protected static void closeConnection(HttpURLConnection connection)
Close the specified connection.

Parameters:
connection - Connection to close.

getWrappedInputStream

protected static InputStream getWrappedInputStream(InputStream is,
                                                   boolean gzip)
                                            throws IOException
Properly wrap the stream accounting for GZIP.

Parameters:
is - Stream to wrap.
gzip - Whether or not to include a GZIP wrapper.
Returns:
Wrapped stream.
Throws:
IOException

convertStreamToString

protected static String convertStreamToString(InputStream is)
Read an entire stream to end and assemble in a string.

Parameters:
is - Stream to read.
Returns:
Entire stream contents.


Copyright © 2011 Jake Wharton. All Rights Reserved.