package io.trustbuilder.mfa.android.demoapp.user_api;

import java.lang.reflect.Type;

import io.tb.userapi.invoker.ApiClient;
import io.tb.userapi.invoker.ApiException;
import okhttp3.Response;

public class UserApiClient extends ApiClient {
    public UserApiClient() {

    }

    /**
     * Handle the given response, return the deserialized object when the response is successful.
     *
     * @param <T> Type
     * @param response Response
     * @param returnType Return type
     * @throws ApiException If the response has a unsuccessful status code or
     *   fail to deserialize the response body
     * @return Type
     */
    @Override
    public <T> T handleResponse(Response response, Type returnType) throws ApiException {
        if (response.isSuccessful()) {
            if (returnType == null || response.code() == 204) {
                // returning null if the returnType is not defined,
                // or the status code is 204 (No Content)
                if (response.body() != null) {
                    response.body().close();
                }
                return null;
            } else {
                return deserialize(response, returnType);
            }
        } else {
            String respBody = null;
            if (response.body() != null) {
                return deserialize(response, returnType);
            }
            throw new ApiException(response.message(), response.code(), response.headers().toMultimap(), respBody);
        }
    }

}
