package com.microsoft.azure;
import com.google.common.reflect.TypeToken;
import com.microsoft.rest.RestException;
import com.microsoft.rest.ServiceResponse;
import com.microsoft.rest.ServiceResponseBuilder;
import com.microsoft.rest.ServiceResponseWithHeaders;
import com.microsoft.rest.protocol.ResponseBuilder;
import com.microsoft.rest.protocol.SerializerAdapter;
import okhttp3.ResponseBody;
import retrofit2.Response;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Type;
public final class AzureResponseBuilder<T, E extends RestException> implements ResponseBuilder<T, E> {
private ServiceResponseBuilder<T, E> baseBuilder;
private AzureResponseBuilder(SerializerAdapter<?> serializer) {
baseBuilder = new ServiceResponseBuilder.Factory().newInstance(serializer);
}
@Override
public ResponseBuilder<T, E> register(int statusCode, Type type) {
baseBuilder.register(statusCode, type);
return this;
}
@Override
public ResponseBuilder<T, E> registerError(Class<? extends RestException> type) {
baseBuilder.registerError(type);
return this;
}
@Override
public ServiceResponse<T> build(Response<ResponseBody> response) throws IOException {
return baseBuilder.build(response);
}
@SuppressWarnings("unchecked")
@Override
public ServiceResponse<T> buildEmpty(Response<Void> response) throws E, IOException {
int statusCode = response.code();
if (baseBuilder.isSuccessful(statusCode)) {
if (new TypeToken<T>(getClass()) { }.getRawType().isAssignableFrom(Boolean.class)) {
return new ServiceResponse<T>(response).withBody((T) (Object) (statusCode / 100 == 2));
} else {
return new ServiceResponse<>(response);
}
} else {
try {
throw baseBuilder.exceptionType().getConstructor(String.class, Response.class)
.newInstance("Status code " + statusCode, response);
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new IOException("Invalid status code " + statusCode + ", but an instance of " + baseBuilder.exceptionType().getCanonicalName()
+ " cannot be created.", e);
}
}
}
@Override
public <THeader> ServiceResponseWithHeaders<T, THeader> (Response<ResponseBody> response, Class<THeader> headerType) throws IOException {
return baseBuilder.buildWithHeaders(response, headerType);
}
@Override
public <THeader> ServiceResponseWithHeaders<T, THeader> (Response<Void> response, Class<THeader> headerType) throws IOException {
ServiceResponse<T> bodyResponse = buildEmpty(response);
ServiceResponseWithHeaders<T, THeader> baseResponse = baseBuilder.buildEmptyWithHeaders(response, headerType);
ServiceResponseWithHeaders<T, THeader> serviceResponse = new ServiceResponseWithHeaders<>(baseResponse.headers(), bodyResponse.headResponse());
serviceResponse.withBody(bodyResponse.body());
return serviceResponse;
}
public AzureResponseBuilder<T, E> withThrowOnGet404(boolean throwOnGet404) {
baseBuilder.withThrowOnGet404(throwOnGet404);
return this;
}
public static final class Factory implements ResponseBuilder.Factory {
@Override
public <T, E extends RestException> AzureResponseBuilder<T, E> newInstance(final SerializerAdapter<?> serializerAdapter) {
return new AzureResponseBuilder<T, E>(serializerAdapter);
}
}
}