提交 ef71254e 编写于 作者: J Jake Wharton

Re-arrange package structure.

上级 4f3798e6
......@@ -13,9 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import retrofit.http.client.Response;
import retrofit.client.Response;
/**
* Communicates responses to server or offline requests. Contains a callback method for each
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.util.concurrent.Executor;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
/** A {@link Server} whose URL and name can be changed at runtime. */
public class ChangeableServer extends Server {
......@@ -39,7 +39,7 @@ public class ChangeableServer extends Server {
this.url = url;
}
/** Update the URL and name returned by {@link #getUrl()} and {@link #getName()}, respetively. */
/** Update the URL and name returned by {@link #getUrl()} and {@link #getName()}, respectively. */
public void update(String url, String name) {
this.url = url;
this.name = name;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import android.os.Build;
import android.os.Process;
......@@ -22,16 +22,16 @@ import com.google.gson.Gson;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import retrofit.http.android.AndroidApacheClient;
import retrofit.http.android.MainThreadExecutor;
import retrofit.http.client.Client;
import retrofit.http.client.OkClient;
import retrofit.http.client.UrlConnectionClient;
import retrofit.android.AndroidApacheClient;
import retrofit.android.MainThreadExecutor;
import retrofit.client.Client;
import retrofit.client.OkClient;
import retrofit.client.UrlConnectionClient;
import retrofit.converter.Converter;
import retrofit.converter.GsonConverter;
import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
import static java.lang.Thread.MIN_PRIORITY;
import static retrofit.http.RestAdapter.IDLE_THREAD_NAME;
import static retrofit.http.Utils.SynchronousExecutor;
abstract class Platform {
private static final Platform PLATFORM = findPlatform();
......@@ -49,10 +49,7 @@ abstract class Platform {
}
}
Converter defaultConverter() {
return new GsonConverter(new Gson());
}
abstract Converter defaultConverter();
abstract Client.Provider defaultClient();
abstract Executor defaultHttpExecutor();
abstract Executor defaultCallbackExecutor();
......@@ -60,6 +57,10 @@ abstract class Platform {
/** Provides sane defaults for operation on the JVM. */
private static class Base extends Platform {
@Override Converter defaultConverter() {
return new GsonConverter(new Gson());
}
@Override Client.Provider defaultClient() {
final Client client;
if (hasOkHttpOnClasspath()) {
......@@ -82,13 +83,13 @@ abstract class Platform {
Thread.currentThread().setPriority(MIN_PRIORITY);
r.run();
}
}, IDLE_THREAD_NAME);
}, RestAdapter.IDLE_THREAD_NAME);
}
});
}
@Override Executor defaultCallbackExecutor() {
return new SynchronousExecutor();
return new Utils.SynchronousExecutor();
}
@Override RestAdapter.Log defaultLog() {
......@@ -102,6 +103,10 @@ abstract class Platform {
/** Provides sane defaults for operation on Android. */
private static class Android extends Platform {
@Override Converter defaultConverter() {
return new GsonConverter(new Gson());
}
@Override Client.Provider defaultClient() {
final Client client;
if (hasOkHttpOnClasspath()) {
......@@ -126,7 +131,7 @@ abstract class Platform {
Process.setThreadPriority(THREAD_PRIORITY_BACKGROUND);
r.run();
}
}, IDLE_THREAD_NAME);
}, RestAdapter.IDLE_THREAD_NAME);
}
});
}
......@@ -156,7 +161,7 @@ abstract class Platform {
/**
* Indirection for OkHttp class to prevent VerifyErrors on Android 2.0 and earlier when the
* dependency is not present..
* dependency is not present.
*/
private static class OkClientInstantiator {
static Client instantiate() {
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
/**
* A hook allowing clients to log HTTP method times and response status codes.
......@@ -26,7 +26,7 @@ public interface Profiler<T> {
* Invoked before an HTTP method call. The object returned by this method will be
* passed to {@link #afterCall} when the call returns.
* <p>
* This method gives implementors the opportunity to include information that may
* This method gives implementers the opportunity to include information that may
* change during the server call in {@code afterCall} logic.
*/
T beforeCall();
......
......@@ -13,19 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import retrofit.http.client.Header;
import retrofit.http.client.Request;
import retrofit.http.mime.FormUrlEncodedTypedOutput;
import retrofit.http.mime.MultipartTypedOutput;
import retrofit.http.mime.TypedOutput;
import static retrofit.http.RestMethodInfo.NO_BODY;
import retrofit.client.Header;
import retrofit.client.Request;
import retrofit.converter.Converter;
import retrofit.mime.FormUrlEncodedTypedOutput;
import retrofit.mime.MultipartTypedOutput;
import retrofit.mime.TypedOutput;
/** Builds HTTP requests from Java method invocations. */
final class RequestBuilder {
......@@ -34,7 +33,7 @@ final class RequestBuilder {
private RestMethodInfo methodInfo;
private Object[] args;
private String apiUrl;
private List<retrofit.http.client.Header> headers;
private List<retrofit.client.Header> headers;
RequestBuilder(Converter converter) {
this.converter = converter;
......@@ -59,7 +58,7 @@ final class RequestBuilder {
}
/** A list of custom headers. */
RequestBuilder headers(List<retrofit.http.client.Header> headers) {
RequestBuilder headers(List<retrofit.client.Header> headers) {
this.headers = headers;
return this;
}
......@@ -100,7 +99,7 @@ final class RequestBuilder {
}
}
List<retrofit.http.client.Header> headers = new ArrayList<retrofit.http.client.Header>();
List<retrofit.client.Header> headers = new ArrayList<retrofit.client.Header>();
if (this.headers != null) {
headers.addAll(this.headers);
}
......@@ -116,7 +115,7 @@ final class RequestBuilder {
if (name == null) continue;
Object arg = args[i];
if (arg != null) {
headers.add(new retrofit.http.client.Header(name, String.valueOf(arg)));
headers.add(new retrofit.client.Header(name, String.valueOf(arg)));
}
}
}
......@@ -143,7 +142,7 @@ final class RequestBuilder {
switch (methodInfo.requestType) {
case SIMPLE: {
int bodyIndex = methodInfo.bodyIndex;
if (bodyIndex == NO_BODY) {
if (bodyIndex == RestMethodInfo.NO_BODY) {
return null;
}
Object body = args[bodyIndex];
......
......@@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.util.Collections;
import java.util.List;
import retrofit.http.client.Header;
import retrofit.client.Header;
/** Manages headers for each request. */
public interface RequestHeaders {
......@@ -25,7 +25,7 @@ public interface RequestHeaders {
* Get a list of headers for a request. This method will be called once for each request allowing
* you to change the list as the state of your application changes.
*/
List<retrofit.http.client.Header> get();
List<Header> get();
/** Empty header list. */
RequestHeaders NONE = new RequestHeaders() {
......
......@@ -13,22 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import retrofit.http.client.Response;
import retrofit.client.Response;
/**
* A wrapper that holds the {@link Response} and {@link Converter} response to be used by the
* {@link CallbackRunnable} for success method calls on {@link Callback}.
* A wrapper that holds the {@link Response} and {@link retrofit.converter.Converter} response to
* be used by the {@link CallbackRunnable} for success method calls on {@link Callback}.
*
* @author JJ Ford (jj.n.ford@gmail.com)
*/
final class ResponseWrapper {
final Response response;
final Object responseBody;
public ResponseWrapper(Response response, Object responseBody) {
ResponseWrapper(Response response, Object responseBody) {
this.response = response;
this.responseBody = responseBody;
}
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -26,16 +26,17 @@ import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import retrofit.http.Profiler.RequestInformation;
import retrofit.http.client.Client;
import retrofit.http.client.Header;
import retrofit.http.client.Request;
import retrofit.http.client.Response;
import retrofit.http.mime.TypedByteArray;
import retrofit.http.mime.TypedInput;
import retrofit.http.mime.TypedOutput;
import static retrofit.http.Utils.SynchronousExecutor;
import retrofit.Profiler.RequestInformation;
import retrofit.client.Client;
import retrofit.client.Header;
import retrofit.client.Request;
import retrofit.client.Response;
import retrofit.converter.ConversionException;
import retrofit.converter.Converter;
import retrofit.mime.MimeUtil;
import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;
/**
* Adapts a Java interface to a REST API.
......@@ -44,42 +45,47 @@ import static retrofit.http.Utils.SynchronousExecutor;
* the form in which the HTTP call should be made.
* <p>
* The relative path for a given method is obtained from an annotation on the method describing
* the request type. The built-in methods are {@link GET}, {@link PUT}, {@link POST}, {@link HEAD},
* and {@link DELETE}. You can define your own HTTP method by creating an annotation that takes a
* {code String} value and itself is annotated with {@link RestMethod @RestMethod}.
* the request type. The built-in methods are {@link retrofit.http.GET GET},
* {@link retrofit.http.PUT PUT}, {@link retrofit.http.POST POST}, {@link retrofit.http.HEAD HEAD},
* and {@link retrofit.http.DELETE DELETE}. You can define your own HTTP method by creating an
* annotation that takes a {code String} value and itself is annotated with
* {@link retrofit.http.RestMethod @RestMethod}.
* <p>
* Method parameters can be used to replace parts of the URL by annotating them with
* {@link Path @Path}. Replacement sections are denoted by an identifier surrounded by curly braces
* (e.g., "{foo}"). To add items to the query string of a URL use {@link Query @Query}.
* {@link retrofit.http.Path @Path}. Replacement sections are denoted by an identifier surrounded
* by curly braces (e.g., "{foo}"). To add items to the query string of a URL use
* {@link retrofit.http.Query @Query}.
* <p>
* HTTP requests happen in one of two ways:
* <ul>
* <li>On the provided HTTP {@link Executor} with callbacks marshaled to the callback
* {@link Executor}. The last method parameter should be of type {@link Callback}. The HTTP
* response will be converted to the callback's parameter type using the specified
* {@link Converter}. If the callback parameter type uses a wildcard, the lower bound will be
* used as the conversion type.
* {@link retrofit.converter.Converter Converter}. If the callback parameter type uses a wildcard,
* the lower bound will be used as the conversion type.
* <li>On the current thread returning the response or throwing a {@link RetrofitError}. The HTTP
* response will be converted to the method's return type using the specified {@link Converter}.
* response will be converted to the method's return type using the specified
* {@link retrofit.converter.Converter Converter}.
* </ul>
* <p>
* The body of a request is denoted by the {@link Body @Body} annotation. The object will be
* converted to request representation by a call to {@link Converter#toBody(Object) toBody} on the
* supplied {@link Converter} for this instance. The body can also be a {@link TypedOutput} where
* it will be used directly.
* The body of a request is denoted by the {@link retrofit.http.Body @Body} annotation. The object
* will be converted to request representation by a call to
* {@link retrofit.converter.Converter#toBody(Object) toBody} on the supplied
* {@link retrofit.converter.Converter Converter} for this instance. The body can also be a
* {@link TypedOutput} where it will be used directly.
* <p>
* Alternative request body formats are supported by method annotations and corresponding parameter
* annotations:
* <ul>
* <li>{@link FormUrlEncoded @FormUrlEncoded} - Form-encoded data with pairs specified by the
* {@link Field @Field} parameter annotation.
* <li>{@link Multipart @Multipart} - RFC 2387-compliant multi-part data with parts specified by
* the {@link Part @Part} parameter annotation.
* <li>{@link retrofit.http.FormUrlEncoded @FormUrlEncoded} - Form-encoded data with pairs
* specified by the {@link retrofit.http.Field @Field} parameter annotation.
* <li>{@link retrofit.http.Multipart @Multipart} - RFC 2387-compliant multi-part data with parts
* specified by the {@link retrofit.http.Part @Part} parameter annotation.
* </ul>
* <p>
* Additional static headers can be added for an endpoint using the {@link Headers @Headers} method
* annotation. For per-request control over a header annotate a parameter with
* {@link Header @Header}.
* Additional static headers can be added for an endpoint using the
* {@link retrofit.http.Headers @Headers} method annotation. For per-request control over a header
* annotate a parameter with {@link Header @Header}.
* <p>
* For example:
* <pre>
......@@ -132,7 +138,7 @@ public class RestAdapter {
this.debug = debug;
}
/** Toggle debug logging on and off. */
/** Toggle debug logging on or off. */
public void setDebug(boolean debug) {
this.debug = debug;
}
......@@ -189,8 +195,8 @@ public class RestAdapter {
/**
* Execute an HTTP request.
*
* @return HTTP response object of specified {@code type}.
* @throws RetrofitError Thrown if any error occurs during the HTTP request.
* @return HTTP response object of specified {@code type} or {@code null}.
* @throws RetrofitError if any error occurs during the HTTP request.
*/
private Object invokeRequest(RestMethodInfo methodDetails, Object[] args) {
methodDetails.init(); // Ensure all relevant method information has been loaded.
......@@ -227,6 +233,7 @@ public class RestAdapter {
int statusCode = response.getStatus();
if (profiler != null) {
RequestInformation requestInfo = getRequestInfo(serverUrl, methodDetails, request);
//noinspection unchecked
profiler.afterCall(requestInfo, elapsedTime, statusCode, profilerObject);
}
......@@ -302,7 +309,7 @@ public class RestAdapter {
byte[] bodyBytes = baos.toByteArray();
bodySize = bodyBytes.length;
String bodyMime = body.mimeType();
String bodyString = new String(bodyBytes, Utils.parseCharset(bodyMime));
String bodyString = new String(bodyBytes, MimeUtil.parseCharset(bodyMime));
for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
log.log(bodyString.substring(i, end));
......@@ -342,7 +349,7 @@ public class RestAdapter {
byte[] bodyBytes = ((TypedByteArray) body).getBytes();
bodySize = bodyBytes.length;
String bodyMime = body.mimeType();
String bodyCharset = Utils.parseCharset(bodyMime);
String bodyCharset = MimeUtil.parseCharset(bodyMime);
String bodyString = new String(bodyBytes, bodyCharset);
for (int i = 0, len = bodyString.length(); i < len; i += LOG_CHUNK_SIZE) {
int end = Math.min(len, i + LOG_CHUNK_SIZE);
......@@ -397,17 +404,20 @@ public class RestAdapter {
private Log log;
private boolean debug;
/** API server base URL. */
public Builder setServer(String endpoint) {
if (endpoint == null) throw new NullPointerException("endpoint");
return setServer(new Server(endpoint));
}
/** API server. */
public Builder setServer(Server server) {
if (server == null) throw new NullPointerException("server");
this.server = server;
return this;
}
/** The HTTP client used for requests. */
public Builder setClient(final Client client) {
if (client == null) throw new NullPointerException("client");
return setClient(new Client.Provider() {
......@@ -417,6 +427,7 @@ public class RestAdapter {
});
}
/** The HTTP client used for requests. */
public Builder setClient(Client.Provider clientProvider) {
if (clientProvider == null) throw new NullPointerException("clientProvider");
this.clientProvider = clientProvider;
......@@ -433,41 +444,47 @@ public class RestAdapter {
*/
public Builder setExecutors(Executor httpExecutor, Executor callbackExecutor) {
if (httpExecutor == null) throw new NullPointerException("httpExecutor");
if (callbackExecutor == null) callbackExecutor = new SynchronousExecutor();
if (callbackExecutor == null) callbackExecutor = new Utils.SynchronousExecutor();
this.httpExecutor = httpExecutor;
this.callbackExecutor = callbackExecutor;
return this;
}
/** */
public Builder setRequestHeaders(RequestHeaders requestHeaders) {
if (requestHeaders == null) throw new NullPointerException("requestHeaders");
this.requestHeaders = requestHeaders;
return this;
}
/** The converter used for serialization and deserialization of objects. */
public Builder setConverter(Converter converter) {
if (converter == null) throw new NullPointerException("converter");
this.converter = converter;
return this;
}
/** Set the profiler used to measure requests. */
public Builder setProfiler(Profiler profiler) {
if (profiler == null) throw new NullPointerException("profiler");
this.profiler = profiler;
return this;
}
/** Configure debug logging mechanism. */
public Builder setLog(Log log) {
if (log == null) throw new NullPointerException("log");
this.log = log;
return this;
}
/** Enable debug logging. */
public Builder setDebug(boolean debug) {
this.debug = debug;
return this;
}
/** Create the {@link RestAdapter} instances. */
public RestAdapter build() {
if (server == null) {
throw new IllegalArgumentException("Server may not be null.");
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
......@@ -26,6 +26,16 @@ import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import retrofit.http.Body;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.Multipart;
import retrofit.http.Part;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit.http.RestMethod;
/** Request metadata about a service interface declaration. */
final class RestMethodInfo {
......@@ -57,7 +67,7 @@ final class RestMethodInfo {
String requestUrl;
Set<String> requestUrlParamNames;
String requestQuery;
List<retrofit.http.client.Header> headers;
List<retrofit.client.Header> headers;
// Parameter-level details
String[] requestUrlParam;
......@@ -198,14 +208,14 @@ final class RestMethodInfo {
requestQuery = query;
}
private List<retrofit.http.client.Header> parseHeaders(String[] headers) {
List<retrofit.http.client.Header> headerList = new ArrayList<retrofit.http.client.Header>();
private List<retrofit.client.Header> parseHeaders(String[] headers) {
List<retrofit.client.Header> headerList = new ArrayList<retrofit.client.Header>();
for (String header : headers) {
int colon = header.indexOf(':');
if (colon == -1 || colon == 0 || colon == headers.length - 1) {
throw new IllegalStateException("Header must be in the form 'Name: Value': " + header);
}
headerList.add(new retrofit.http.client.Header(header.substring(0, colon),
headerList.add(new retrofit.client.Header(header.substring(0, colon),
header.substring(colon + 1).trim()));
}
return headerList;
......
......@@ -13,12 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.io.IOException;
import java.lang.reflect.Type;
import retrofit.http.client.Response;
import retrofit.http.mime.TypedInput;
import retrofit.client.Response;
import retrofit.converter.ConversionException;
import retrofit.converter.Converter;
import retrofit.mime.TypedInput;
public class RetrofitError extends RuntimeException {
public static RetrofitError networkError(String url, IOException exception) {
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
/**
* Represents an API endpoint URL and associated name. Callers should always consult the instance
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.lang.reflect.Array;
import java.lang.reflect.GenericArrayType;
......
......@@ -14,22 +14,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Executor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import retrofit.http.client.Response;
import retrofit.http.mime.TypedByteArray;
import retrofit.http.mime.TypedInput;
import retrofit.client.Response;
import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedInput;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
public final class Utils {
private static final Pattern CHARSET = Pattern.compile("\\Wcharset=([^\\s;]+)", CASE_INSENSITIVE);
final class Utils {
private static final int BUFFER_SIZE = 0x1000;
/**
......@@ -71,14 +66,6 @@ public final class Utils {
return new Response(response.getStatus(), response.getReason(), response.getHeaders(), body);
}
public static String parseCharset(String mimeType) {
Matcher match = CHARSET.matcher(mimeType);
if (match.find()) {
return match.group(1).replaceAll("[\"\\\\]", "");
}
return "UTF-8";
}
static class SynchronousExecutor implements Executor {
@Override public void execute(Runnable runnable) {
runnable.run();
......
......@@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.android;
package retrofit.android;
import android.net.http.AndroidHttpClient;
import retrofit.http.client.ApacheClient;
import retrofit.client.ApacheClient;
/**
* Provides a {@link retrofit.http.client.Client} which uses the Android-specific version of
* Provides a {@link retrofit.client.Client} which uses the Android-specific version of
* {@link org.apache.http.client.HttpClient}, {@link AndroidHttpClient}.
* <p>
* If you need to provide a customized version of the {@link AndroidHttpClient} or a different
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.android;
package retrofit.android;
import android.os.Handler;
import android.os.Looper;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
......@@ -33,8 +33,8 @@ import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import retrofit.http.mime.TypedByteArray;
import retrofit.http.mime.TypedOutput;
import retrofit.mime.TypedByteArray;
import retrofit.mime.TypedOutput;
/** A {@link Client} which uses an implementation of Apache's {@link HttpClient}. */
public class ApacheClient implements Client {
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
import java.io.IOException;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
/** Represents an HTTP header name/value pair. */
public final class Header {
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
import com.squareup.okhttp.OkHttpClient;
import java.io.IOException;
......
......@@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit.http.mime.TypedOutput;
import retrofit.mime.TypedOutput;
/** Encapsulates all of the information necessary to make an HTTP request. */
public final class Request {
......
......@@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import retrofit.http.mime.TypedInput;
import retrofit.mime.TypedInput;
/** An HTTP response. */
public final class Response {
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.client;
package retrofit.client;
import java.io.IOException;
import java.io.InputStream;
......@@ -22,8 +22,8 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import retrofit.http.mime.TypedInput;
import retrofit.http.mime.TypedOutput;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;
/** Retrofit client that uses {@link HttpURLConnection} for communication. */
public class UrlConnectionClient implements Client {
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit.converter;
/** Indicate that conversion was unable to complete successfully. */
@SuppressWarnings("UnusedDeclaration")
......
......@@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit.converter;
import java.lang.reflect.Type;
import retrofit.http.mime.TypedInput;
import retrofit.http.mime.TypedOutput;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;
/**
* Arbiter for converting objects to and from their representation in HTTP.
......@@ -32,9 +32,9 @@ public interface Converter {
* @param type Target object type.
* @return Instance of {@code type} which will be cast by the caller.
* @throws ConversionException if conversion was unable to complete. This will trigger a call to
* {@link Callback#failure(RetrofitError)} or throw a {@link retrofit.http.RetrofitError}. The
* exception message should report all necessary information about its cause as the response body
* will be set to {@code null}.
* {@link retrofit.Callback#failure(retrofit.RetrofitError)} or throw a
* {@link retrofit.RetrofitError}. The exception message should report all necessary information
* about its cause as the response body will be set to {@code null}.
*/
Object fromBody(TypedInput body, Type type) throws ConversionException;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
package retrofit.converter;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
......@@ -22,8 +22,9 @@ import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import retrofit.http.mime.TypedInput;
import retrofit.http.mime.TypedOutput;
import retrofit.mime.MimeUtil;
import retrofit.mime.TypedInput;
import retrofit.mime.TypedOutput;
/**
* A {@link Converter} which uses GSON for serialization and deserialization of entities.
......@@ -38,7 +39,7 @@ public class GsonConverter implements Converter {
}
@Override public Object fromBody(TypedInput body, Type type) throws ConversionException {
String charset = Utils.parseCharset(body.mimeType());
String charset = MimeUtil.parseCharset(body.mimeType());
InputStreamReader isr = null;
try {
isr = new InputStreamReader(body.in(), charset);
......@@ -65,7 +66,7 @@ public class GsonConverter implements Converter {
}
}
static class JsonTypedOutput implements TypedOutput {
private static class JsonTypedOutput implements TypedOutput {
private final byte[] jsonBytes;
JsonTypedOutput(byte[] jsonBytes) {
......
/*
* Copyright (C) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http;
import java.lang.reflect.Type;
/**
* Represents a named parameter and its value.
* <p>
* This is used in one of three places in a request:
* <ul>
* <li>Named replacement in the relative URL path.
* <li>As a URL query parameter.
* <li>As a POST/PUT body.
* </ul>
*/
public final class Parameter {
private final String name;
private final Type type;
private final Object value;
public Parameter(String name, Object value, Type valueType) {
if (name == null) {
throw new NullPointerException("name == null");
}
this.name = name;
this.type = valueType;
this.value = value;
}
public String getName() {
return name;
}
public Object getValue() {
return value;
}
/** The instance type of {@link #getValue()}. */
public Type getValueType() {
return type;
}
@Override public String toString() {
return name + "=" + value;
}
@Override public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Parameter parameter = (Parameter) o;
if (!name.equals(parameter.name)) return false;
if (value != null ? !value.equals(parameter.value) : parameter.value != null) return false;
return true;
}
@Override public int hashCode() {
int result = name.hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
}
......@@ -26,10 +26,10 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* <p>
* The parameter type on which this annotation exists will be processed in one of two ways:
* <ul>
* <li>If the type implements {@link retrofit.http.mime.TypedOutput TypedOutput} the headers and
* <li>If the type implements {@link retrofit.mime.TypedOutput TypedOutput} the headers and
* body will be used directly.</li>
* <li>Other object types will be converted to an appropriate representation by calling {@link
* Converter#toBody(Object)}.</li>
* retrofit.converter.Converter#toBody(Object)}.</li>
* </ul>
* <p>
* <pre>
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......
/*
* Copyright (C) 2013 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.mime;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
public final class MimeUtil {
private static final Pattern CHARSET = Pattern.compile("\\Wcharset=([^\\s;]+)", CASE_INSENSITIVE);
/** Parse the MIME type from a {@code Content-Type} header value. */
public static String parseCharset(String mimeType) {
Matcher match = CHARSET.matcher(mimeType);
if (match.find()) {
return match.group(1).replaceAll("[\"\\\\]", "");
}
return "UTF-8";
}
private MimeUtil() {
// No instances.
}
}
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.ByteArrayInputStream;
import java.io.IOException;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.File;
import java.io.FileInputStream;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.IOException;
import java.io.InputStream;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.IOException;
import java.io.OutputStream;
......
......@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit.http.mime;
package retrofit.mime;
import java.io.UnsupportedEncodingException;
......
// Copyright 2013 Square, Inc.
package retrofit.http;
package retrofit;
import java.util.concurrent.Executor;
import org.junit.Before;
import org.junit.Test;
import retrofit.Callback;
import retrofit.CallbackRunnable;
import retrofit.ResponseWrapper;
import retrofit.RetrofitError;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.same;
......@@ -11,7 +15,7 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static retrofit.http.Utils.SynchronousExecutor;
import static retrofit.Utils.SynchronousExecutor;
public class CallbackRunnableTest {
private Executor executor = spy(new SynchronousExecutor());
......
// Copyright 2013 Square, Inc.
package retrofit.http;
package retrofit;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
......@@ -10,16 +10,20 @@ import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import retrofit.http.client.Header;
import retrofit.http.client.Request;
import retrofit.http.mime.MimeHelper;
import retrofit.http.mime.MultipartTypedOutput;
import retrofit.http.mime.TypedOutput;
import retrofit.http.mime.TypedString;
import retrofit.converter.Converter;
import retrofit.converter.GsonConverter;
import retrofit.RequestBuilder;
import retrofit.RestMethodInfo;
import retrofit.client.Header;
import retrofit.client.Request;
import retrofit.mime.MimeHelper;
import retrofit.mime.MultipartTypedOutput;
import retrofit.mime.TypedOutput;
import retrofit.mime.TypedString;
import static org.fest.assertions.api.Assertions.assertThat;
import static retrofit.http.RestMethodInfo.NO_BODY;
import static retrofit.http.RestMethodInfo.RequestType;
import static retrofit.RestMethodInfo.NO_BODY;
import static retrofit.RestMethodInfo.RequestType;
public class RequestBuilderTest {
@Test public void normalGet() throws Exception {
......
// Copyright 2013 Square, Inc.
package retrofit.http;
package retrofit;
import java.io.IOException;
import java.util.Collections;
......@@ -7,11 +7,13 @@ import java.util.List;
import java.util.concurrent.Executor;
import org.junit.Before;
import org.junit.Test;
import retrofit.http.client.Client;
import retrofit.http.client.Header;
import retrofit.http.client.Request;
import retrofit.http.client.Response;
import retrofit.http.mime.TypedString;
import retrofit.converter.ConversionException;
import retrofit.http.GET;
import retrofit.client.Client;
import retrofit.client.Header;
import retrofit.client.Request;
import retrofit.client.Response;
import retrofit.mime.TypedString;
import static org.fest.assertions.api.Assertions.assertThat;
import static org.fest.assertions.api.Assertions.fail;
......@@ -25,8 +27,8 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static retrofit.http.Profiler.RequestInformation;
import static retrofit.http.Utils.SynchronousExecutor;
import static retrofit.Profiler.RequestInformation;
import static retrofit.Utils.SynchronousExecutor;
public class RestAdapterTest {
private static List<Header> NO_HEADERS = Collections.emptyList();
......
// Copyright 2013 Square, Inc.
package retrofit.http;
package retrofit;
import com.google.gson.reflect.TypeToken;
import java.lang.annotation.Retention;
......@@ -11,14 +11,29 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;
import retrofit.http.mime.TypedOutput;
import retrofit.http.Body;
import retrofit.http.DELETE;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.HEAD;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.PUT;
import retrofit.http.Part;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit.http.RestMethod;
import retrofit.mime.TypedOutput;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.fest.assertions.api.Assertions.assertThat;
import static retrofit.http.RestMethodInfo.NO_BODY;
import static retrofit.http.RestMethodInfo.RequestType.MULTIPART;
import static retrofit.http.RestMethodInfo.RequestType.SIMPLE;
import static retrofit.RestMethodInfo.NO_BODY;
import static retrofit.RestMethodInfo.RequestType.MULTIPART;
import static retrofit.RestMethodInfo.RequestType.SIMPLE;
public class RestMethodInfoTest {
@Test public void pathParameterParsing() throws Exception {
......@@ -678,8 +693,8 @@ public class RestMethodInfoTest {
methodInfo.init();
assertThat(methodInfo.headers).isEqualTo(
Arrays.asList(new retrofit.http.client.Header("X-Foo", "Bar"),
new retrofit.http.client.Header("X-Ping", "Pong")));
Arrays.asList(new retrofit.client.Header("X-Foo", "Bar"),
new retrofit.client.Header("X-Ping", "Pong")));
}
@Test public void twoHeaderParams() {
......
// Copyright 2013 Square, Inc.
package retrofit.http;
package retrofit;
import java.lang.reflect.Method;
import java.util.Map;
import retrofit.http.mime.MultipartTypedOutput;
import retrofit.http.mime.TypedOutput;
import retrofit.mime.MultipartTypedOutput;
import retrofit.mime.TypedOutput;
import static org.fest.assertions.api.Assertions.assertThat;
......
// Copyright 2013 Square, Inc.
package retrofit.http.client;
package retrofit.client;
import com.google.common.io.ByteStreams;
import java.util.ArrayList;
......@@ -15,14 +15,14 @@ import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicStatusLine;
import org.junit.Test;
import retrofit.http.TestingUtils;
import retrofit.http.mime.TypedOutput;
import retrofit.http.mime.TypedString;
import retrofit.TestingUtils;
import retrofit.mime.TypedOutput;
import retrofit.mime.TypedString;
import static org.fest.assertions.api.Assertions.assertThat;
import static retrofit.http.TestingUtils.assertBytes;
import static retrofit.http.TestingUtils.assertMultipart;
import static retrofit.http.client.ApacheClient.TypedOutputEntity;
import static retrofit.TestingUtils.assertBytes;
import static retrofit.TestingUtils.assertMultipart;
import static retrofit.client.ApacheClient.TypedOutputEntity;
public class ApacheClientTest {
private static final String HOST = "http://example.com";
......
// Copyright 2013 Square, Inc.
package retrofit.http.client;
package retrofit.client;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -13,7 +13,7 @@ import java.util.List;
import java.util.Map;
/**
* Provides POJO behavior for all of the APIs {@link retrofit.http.client.UrlConnectionClient}
* Provides POJO behavior for all of the APIs {@link retrofit.client.UrlConnectionClient}
* interacts with.
*/
public class DummyHttpUrlConnection extends HttpURLConnection {
......
// Copyright 2013 Square, Inc.
package retrofit.http.client;
package retrofit.client;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
......@@ -10,12 +10,12 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import retrofit.http.TestingUtils;
import retrofit.http.mime.TypedOutput;
import retrofit.http.mime.TypedString;
import retrofit.TestingUtils;
import retrofit.mime.TypedOutput;
import retrofit.mime.TypedString;
import static org.fest.assertions.api.Assertions.assertThat;
import static retrofit.http.TestingUtils.assertBytes;
import static retrofit.TestingUtils.assertBytes;
public class UrlConnectionClientTest {
private static final String HOST = "http://example.com";
......
// Copyright 2013 Square, Inc.
package retrofit.http.mime;
package retrofit.mime;
import java.io.ByteArrayOutputStream;
import org.junit.Test;
......
// Copyright 2013 Square, Inc.
package retrofit.http.mime;
package retrofit.mime;
import java.util.List;
......
// Copyright 2012 Square, Inc.
package retrofit.http;
package retrofit.mime;
import org.junit.Test;
import static org.fest.assertions.api.Assertions.assertThat;
import static retrofit.http.Utils.parseCharset;
import static retrofit.mime.MimeUtil.parseCharset;
public class UtilsTest {
public class MimeUtilTest {
@Test public void charsetParsing() {
assertThat(parseCharset("text/plain;charset=utf-8")).isEqualToIgnoringCase("UTF-8");
assertThat(parseCharset("text/plain; charset=utf-8")).isEqualToIgnoringCase("UTF-8");
......
// Copyright 2013 Square, Inc.
package retrofit.http.mime;
package retrofit.mime;
import java.io.ByteArrayOutputStream;
import org.junit.Test;
......
// Copyright 2010 Square, Inc.
package retrofit.http.mime;
package retrofit.mime;
import org.junit.Test;
......
// Copyright 2010 Square, Inc.
package retrofit.http.mime;
package retrofit.mime;
import java.io.File;
import java.io.FileOutputStream;
......
......@@ -13,14 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.squareup.retrofit.sample.github;
package com.example.retrofit;
import java.util.List;
import retrofit.http.GET;
import retrofit.http.Path;
import retrofit.http.RestAdapter;
import retrofit.RestAdapter;
public class Client {
public class GitHubClient {
private static final String API_URL = "https://api.github.com";
class Contributor {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册