/*
 * Copyright (c) 2017 pCloud AG
 *
 * 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 com.pcloud.sdk.internal;

import com.pcloud.sdk.api.ApiError;
import com.pcloud.sdk.api.Call;
import com.pcloud.sdk.api.Callback;
import okhttp3.Response;

import java.io.IOException;

final class OkHttpCall<T> implements Call<T> {

    private okhttp3.Call rawCall;
    private ResponseAdapter<T> responseAdapter;

    public OkHttpCall(okhttp3.Call rawCall, ResponseAdapter<T> adapter) {
        this.rawCall = rawCall;
        this.responseAdapter = adapter;
    }

    @Override
    public T execute() throws IOException, ApiError {
        Response response = rawCall.execute();
        return adapt(response);
    }

    @Override
    public void enqueue(final Callback<T> callback) {
        rawCall.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {

            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                try {
                    callback.onResponse(OkHttpCall.this, adapt(response));
                } catch (ApiError | IOException e) {
                    callback.onFailure(OkHttpCall.this, e);
                }
            }
        });
    }

    @Override
    public boolean isExecuted() {
        return rawCall.isExecuted();
    }

    @Override
    public void cancel() {
        rawCall.cancel();
    }

    @Override
    public boolean isCanceled() {
        return rawCall.isCanceled();
    }

    @SuppressWarnings("CloneDoesntCallSuperClone")
    @Override
    public Call<T> clone() {
        // Class is final, there will be no 'super'.
        return new OkHttpCall<>(rawCall.clone(), responseAdapter);
    }

    okhttp3.Call rawCall() {
        return rawCall;
    }

    ResponseAdapter<T> responseAdapter() {
        return responseAdapter;
    }

    private T adapt(Response response) throws IOException, ApiError {
        return responseAdapter.adapt(response);
    }
}

/*
 * Copyright (c) 2017 pCloud AG
 *
 * 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 com.pcloud.sdk.internal;

import com.pcloud.sdk.api.ApiError;
import com.pcloud.sdk.api.Call;
import com.pcloud.sdk.api.Callback;
import okhttp3.Response;

import java.io.IOException;

final class OkHttpCall<T> implements Call<T> {

    private okhttp3.Call rawCall;
    private ResponseAdapter<T> responseAdapter;

    OkHttpCall(okhttp3.Call rawCall, ResponseAdapter<T> adapter) {
        this.rawCall = rawCall;
        this.responseAdapter = adapter;
    }

    @Override
    public T execute() throws IOException, ApiError {
        Response response = rawCall.execute();
        return adapt(response);
    }

    @Override
    public void enqueue(final Callback<T> callback) {
        rawCall.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                callback.onFailure(OkHttpCall.this, e);
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                try {
                    callback.onResponse(OkHttpCall.this, adapt(response));
                } catch (ApiError | IOException e) {
                    callback.onFailure(OkHttpCall.this, e);
                }
            }
        });
    }

    @Override
    public boolean isExecuted() {
        return rawCall.isExecuted();
    }

    @Override
    public void cancel() {
        rawCall.cancel();
    }

    @Override
    public boolean isCanceled() {
        return rawCall.isCanceled();
    }

    @SuppressWarnings("CloneDoesntCallSuperClone")
    @Override
    public OkHttpCall<T> clone() {
        // Class is final, there will be no 'super'.
        return new OkHttpCall<>(rawCall.clone(), responseAdapter);
    }

    okhttp3.Call rawCall() {
        return rawCall;
    }

    ResponseAdapter<T> responseAdapter() {
        return responseAdapter;
    }

    private T adapt(Response response) throws IOException, ApiError {
        return responseAdapter.adapt(response);
    }
}

Add missing method call in OkHttpCall