Query(String name, Converter<T, String> valueConverter, boolean encoded) { this.name = checkNotNull(name, "name == null"); this.valueConverter = valueConverter; this.encoded = encoded; }
Header(String name, Converter<T, String> valueConverter) { this.name = checkNotNull(name, "name == null"); this.valueConverter = valueConverter; }
Field(String name, Converter<T, String> valueConverter, boolean encoded) { this.name = checkNotNull(name, "name == null"); this.valueConverter = valueConverter; this.encoded = encoded; }
/** * The executor on which {@link Callback} methods are invoked when returning {@link Call} from * your service method. * <p> * Note: {@code executor} is not used for {@linkplain #addCallAdapterFactory custom method * return types}. */ public Builder callbackExecutor(Executor executor) { this.callbackExecutor = checkNotNull(executor, "executor == null"); return this; }
/** * Specify a custom call factory for creating {@link Call} instances. * <p> * Note: Calling {@link #client} automatically sets this value. */ public Builder callFactory(okhttp3.Call.Factory factory) { this.callFactory = checkNotNull(factory, "factory == null"); return this; }
Path(Method method, int p, String name, Converter<T, String> valueConverter, boolean encoded) { this.method = method; this.p = p; this.name = checkNotNull(name, "name == null"); this.valueConverter = valueConverter; this.encoded = encoded; }
/** Add converter factory for serialization and deserialization of objects. */ public Builder addConverterFactory(Converter.Factory factory) { converterFactories.add(checkNotNull(factory, "factory == null")); return this; }
/** * Add a call adapter factory for supporting service method return types other than {@link * Call}. */ public Builder addCallAdapterFactory(CallAdapter.Factory factory) { callAdapterFactories.add(checkNotNull(factory, "factory == null")); return this; }
/** Create an error response from {@code rawResponse} with {@code body} as the error body. */ public static <T> Response<T> error(ResponseBody body, okhttp3.Response rawResponse) { checkNotNull(body, "body == null"); checkNotNull(rawResponse, "rawResponse == null"); if (rawResponse.isSuccessful()) { throw new IllegalArgumentException("rawResponse should not be successful response"); } return new Response<>(rawResponse, null, body); }
public static Invocation of(Method method, List<?> arguments) { checkNotNull(method, "method == null"); checkNotNull(arguments, "arguments == null"); return new Invocation(method, new ArrayList<>(arguments)); // Defensive copy. }
@Override public void enqueue(final Callback<T> callback) { checkNotNull(callback, "callback == null"); delegate.enqueue(new Callback<T>() { @Override public void onResponse(Call<T> call, final Response<T> response) { callbackExecutor.execute(new Runnable() { @Override public void run() { if (delegate.isCanceled()) { // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation. callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled")); } else { callback.onResponse(ExecutorCallbackCall.this, response); } } }); } @Override public void onFailure(Call<T> call, final Throwable t) { callbackExecutor.execute(new Runnable() { @Override public void run() { callback.onFailure(ExecutorCallbackCall.this, t); } }); } }); }
/** * Returns a {@link Converter} for {@code type} to {@link String} from the available * {@linkplain #converterFactories() factories}. */ public <T> Converter<T, String> stringConverter(Type type, Annotation[] annotations) { checkNotNull(type, "type == null"); checkNotNull(annotations, "annotations == null"); for (int i = 0, count = converterFactories.size(); i < count; i++) { Converter<?, String> converter = converterFactories.get(i).stringConverter(type, annotations, this); if (converter != null) { //noinspection unchecked return (Converter<T, String>) converter; } } // Nothing matched. Resort to default converter which just calls toString(). //noinspection unchecked return (Converter<T, String>) BuiltInConverters.ToStringConverter.INSTANCE; }
/** * Create a successful response from {@code rawResponse} with {@code body} as the deserialized * body. */ public static <T> Response<T> success(@Nullable T body, okhttp3.Response rawResponse) { checkNotNull(rawResponse, "rawResponse == null"); if (!rawResponse.isSuccessful()) { throw new IllegalArgumentException("rawResponse must be successful response"); } return new Response<>(rawResponse, body, null); }
/** * The HTTP client used for requests. * <p> * This is a convenience method for calling {@link #callFactory}. */ public Builder client(OkHttpClient client) { return callFactory(checkNotNull(client, "client == null")); }
/** * Set the API base URL. * * @see #baseUrl(HttpUrl) */ public Builder baseUrl(String baseUrl) { checkNotNull(baseUrl, "baseUrl == null"); return baseUrl(HttpUrl.get(baseUrl)); }
ParameterizedTypeImpl(@Nullable Type ownerType, Type rawType, Type... typeArguments) { // Require an owner type if the raw type needs it. if (rawType instanceof Class<?> && (ownerType == null) != (((Class<?>) rawType).getEnclosingClass() == null)) { throw new IllegalArgumentException(); } for (Type typeArgument : typeArguments) { checkNotNull(typeArgument, "typeArgument == null"); checkNotPrimitive(typeArgument); } this.ownerType = ownerType; this.rawType = rawType; this.typeArguments = typeArguments.clone(); }
checkNotNull(returnType, "returnType == null"); checkNotNull(annotations, "annotations == null");
private static String getMessage(Response<?> response) { checkNotNull(response, "response == null"); return "HTTP " + response.code() + " " + response.message(); }
static Class<?> getRawType(Type type) { checkNotNull(type, "type == null"); if (type instanceof Class<?>) { // Type is a normal class. return (Class<?>) type; } if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; // I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but // suspects some pathological case related to nested classes exists. Type rawType = parameterizedType.getRawType(); if (!(rawType instanceof Class)) throw new IllegalArgumentException(); return (Class<?>) rawType; } if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); return Array.newInstance(getRawType(componentType), 0).getClass(); } if (type instanceof TypeVariable) { // We could use the variable's bounds, but that won't work if there are multiple. Having a raw // type that's more general than necessary is okay. return Object.class; } if (type instanceof WildcardType) { return getRawType(((WildcardType) type).getUpperBounds()[0]); } throw new IllegalArgumentException("Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName()); }
/** * Create a synthetic successful response using {@code headers} with {@code body} as the * deserialized body. */ public static <T> Response<T> success(@Nullable T body, Headers headers) { checkNotNull(headers, "headers == null"); return success(body, new okhttp3.Response.Builder() // .code(200) .message("OK") .protocol(Protocol.HTTP_1_1) .headers(headers) .request(new Request.Builder().url("http://localhost/").build()) .build()); }