public HttpException(Response<?> response) { super(getMessage(response)); this.code = response.code(); this.message = response.message(); this.response = response; }
@Override ReturnT invoke(Object[] args) { return callAdapter.adapt( new OkHttpCall<>(requestFactory, args, callFactory, responseConverter)); } }
/** * 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")); }
private static String getMessage(Response<?> response) { checkNotNull(response, "response == null"); return "HTTP " + response.code() + " " + response.message(); }
@Override void apply(RequestBuilder builder, @Nullable T value) throws IOException { if (value == null) { throw Utils.parameterError(method, p, "Path parameter \"" + name + "\" value must not be null."); } builder.addPathParam(name, valueConverter.convert(value), encoded); } }
@Override void apply(RequestBuilder builder, @Nullable T value) { if (value == null) return; // Skip null values. RequestBody body; try { body = converter.convert(value); } catch (IOException e) { throw Utils.parameterError(method, p, "Unable to convert " + value + " to RequestBody", e); } builder.addPart(headers, body); } }
@Override void apply(RequestBuilder builder, @Nullable T value) { if (value == null) { throw Utils.parameterError(method, p, "Body parameter value must not be null."); } RequestBody body; try { body = converter.convert(value); } catch (IOException e) { throw Utils.parameterError(method, e, p, "Unable to convert " + value + " to RequestBody"); } builder.setBody(body); } }
@Override public RequestBody convert(Object value) throws IOException { final RequestBody realBody = delegate.convert(value); return new RequestBody() { @Override public MediaType contentType() { return realBody.contentType(); } @Override public void writeTo(BufferedSink sink) throws IOException { realBody.writeTo(sink); } }; } };
@Override public @Nullable Converter<ResponseBody, ?> responseBodyConverter( Type type, Annotation[] annotations, Retrofit retrofit) { if (getRawType(type) != Optional.class) { return null; } Type innerType = getParameterUpperBound(0, (ParameterizedType) type); Converter<ResponseBody, Object> delegate = retrofit.responseBodyConverter(innerType, annotations); return new OptionalConverter<>(delegate); }
@SuppressWarnings("CloneDoesntCallSuperClone") // Performing deep clone. @Override public Call<T> clone() { return new ExecutorCallbackCall<>(callbackExecutor, delegate.clone()); }
/** * Returns a {@link Converter} for {@code type} to {@link RequestBody} from the available * {@linkplain #converterFactories() factories}. * * @throws IllegalArgumentException if no converter available for {@code type}. */ public <T> Converter<T, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations) { return nextRequestBodyConverter(null, type, parameterAnnotations, methodAnnotations); }
static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) { RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method); Type returnType = method.getGenericReturnType(); if (Utils.hasUnresolvableType(returnType)) { throw methodError(method, "Method return type must not include a type variable or wildcard: %s", returnType); } if (returnType == void.class) { throw methodError(method, "Service methods cannot return void."); } return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory); }
/** * Returns the {@link CallAdapter} for {@code returnType} from the available {@linkplain * #callAdapterFactories() factories}. * * @throws IllegalArgumentException if no call adapter available for {@code type}. */ public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) { return nextCallAdapter(null, returnType, annotations); }
Query(String name, Converter<T, String> valueConverter, boolean encoded) { this.name = checkNotNull(name, "name == null"); this.valueConverter = valueConverter; this.encoded = encoded; }
/** * Extract the upper bound of the generic parameter at {@code index} from {@code type}. For * example, index 1 of {@code Map<String, ? extends Runnable>} returns {@code Runnable}. */ protected static Type getParameterUpperBound(int index, ParameterizedType type) { return Utils.getParameterUpperBound(index, type); }
/** * Extract the raw class type from {@code type}. For example, the type representing * {@code List<? extends Runnable>} returns {@code List.class}. */ protected static Class<?> getRawType(Type type) { return Utils.getRawType(type); } }
@Override public String toString() { if (lowerBound != null) return "? super " + typeToString(lowerBound); if (upperBound == Object.class) return "?"; return "? extends " + typeToString(upperBound); } }
Field(String name, Converter<T, String> valueConverter, boolean encoded) { this.name = checkNotNull(name, "name == null"); this.valueConverter = valueConverter; this.encoded = encoded; }
/** * 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; }