ratpack.retrofit.internal.RatpackCallAdapterFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ratpack-retrofit2 Show documentation
Show all versions of ratpack-retrofit2 Show documentation
Integration with the Retrofit2 library - http://square.github.io/retrofit
The newest version!
/*
* Copyright 2015 the original author or authors.
*
* 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 ratpack.retrofit.internal;
import com.google.common.reflect.TypeToken;
import ratpack.exec.Promise;
import ratpack.func.Factory;
import ratpack.core.http.client.HttpClient;
import ratpack.core.http.client.ReceivedResponse;
import ratpack.retrofit.RatpackRetrofitCallException;
import ratpack.func.Exceptions;
import retrofit2.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class RatpackCallAdapterFactory extends CallAdapter.Factory {
private final Factory extends HttpClient> factory;
private RatpackCallAdapterFactory(Factory extends HttpClient> factory) {
this.factory = factory;
}
public static RatpackCallAdapterFactory with(Factory extends HttpClient> factory) {
return new RatpackCallAdapterFactory(factory);
}
@Override
public CallAdapter, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
TypeToken> rawType = TypeToken.of(returnType);
if (rawType.getRawType() != Promise.class) {
return null;
}
if (!(returnType instanceof ParameterizedType)) {
throw new IllegalStateException("Promise return type must be parameterized"
+ " as Promise or Promise extends Foo>");
}
return getCallAdapter((ParameterizedType) returnType);
}
// returnType is the parameterization of Promise
protected CallAdapter, Promise>> getCallAdapter(ParameterizedType returnType) {
Type parameterType = Utils.getSingleParameterUpperBound(returnType);
TypeToken> parameterTypeToken = TypeToken.of(parameterType);
//Promising a Response type, need the actual value
if (parameterTypeToken.getRawType() == Response.class) {
if (!(parameterType instanceof ParameterizedType)) {
throw new IllegalStateException("Response return type must be parameterized"
+ " as Response or Response extends Foo>");
}
Type responseType = Utils.getSingleParameterUpperBound((ParameterizedType) parameterType);
return new ResponseCallAdapter(responseType);
} else if (parameterTypeToken.getRawType() == ReceivedResponse.class) {
return new ReceivedResponseCallAdapter(parameterType, factory);
}
//Else we're just promising a value
return new SimpleCallAdapter(parameterType);
}
static final class ReceivedResponseCallAdapter implements CallAdapter
© 2015 - 2025 Weber Informatics LLC | Privacy Policy