package com.amplifyframework.api.aws;

import android.annotation.SuppressLint;
import com.amplifyframework.api.ApiException;
import com.amplifyframework.api.aws.auth.ApiRequestDecoratorFactory;
import com.amplifyframework.api.graphql.GraphQLRequest;
import com.amplifyframework.api.graphql.GraphQLResponse;
import com.amplifyframework.core.Amplify;
import com.amplifyframework.core.Consumer;
import com.amplifyframework.core.category.CategoryType;
import com.amplifyframework.logging.Logger;
import com.google.firebase.perf.network.FirebasePerfOkHttpClient;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import uq.b0;

/* loaded from: classes4.dex */
public final class AppSyncGraphQLOperation<R> extends AWSGraphQLOperation<R> {
    private static final String CONTENT_TYPE = "application/json";
    private static final int END_OF_CLIENT_ERROR_CODE = 499;
    private static final Logger LOG = Amplify.Logging.logger(CategoryType.API, "amplify:aws-api");
    private static final int START_OF_CLIENT_ERROR_CODE = 400;
    private final ApiRequestDecoratorFactory apiRequestDecoratorFactory;
    private final uq.z client;
    private final String endpoint;
    private final ExecutorService executorService;
    private final Consumer<ApiException> onFailure;
    private final Consumer<GraphQLResponse<R>> onResponse;
    private uq.e ongoingCall;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class Builder<R> {
        private String apiName;
        private ApiRequestDecoratorFactory apiRequestDecoratorFactory;
        private uq.z client;
        private String endpoint;
        private ExecutorService executorService;
        private Consumer<ApiException> onFailure;
        private Consumer<GraphQLResponse<R>> onResponse;
        private GraphQLRequest<R> request;
        private GraphQLResponse.Factory responseFactory;

        Builder() {
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> apiName(String str) {
            this.apiName = str;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> apiRequestDecoratorFactory(ApiRequestDecoratorFactory apiRequestDecoratorFactory) {
            Objects.requireNonNull(apiRequestDecoratorFactory);
            this.apiRequestDecoratorFactory = apiRequestDecoratorFactory;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        @SuppressLint({"SyntheticAccessor"})
        public AppSyncGraphQLOperation<R> build() {
            return new AppSyncGraphQLOperation<>(this);
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> client(uq.z zVar) {
            Objects.requireNonNull(zVar);
            this.client = zVar;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> endpoint(String str) {
            Objects.requireNonNull(str);
            this.endpoint = str;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> executorService(ExecutorService executorService) {
            this.executorService = executorService;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> onFailure(Consumer<ApiException> consumer) {
            Objects.requireNonNull(consumer);
            this.onFailure = consumer;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> onResponse(Consumer<GraphQLResponse<R>> consumer) {
            Objects.requireNonNull(consumer);
            this.onResponse = consumer;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> request(GraphQLRequest<R> graphQLRequest) {
            Objects.requireNonNull(graphQLRequest);
            this.request = graphQLRequest;
            return this;
        }

        /* JADX INFO: Access modifiers changed from: package-private */
        public Builder<R> responseFactory(GraphQLResponse.Factory factory) {
            Objects.requireNonNull(factory);
            this.responseFactory = factory;
            return this;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @SuppressLint({"SyntheticAccessor"})
    /* loaded from: classes4.dex */
    public class OkHttpCallback implements uq.f {
        OkHttpCallback() {
        }

        @Override // uq.f
        public void onFailure(uq.e eVar, IOException iOException) {
            if (eVar.e()) {
                return;
            }
            AppSyncGraphQLOperation.this.onFailure.accept(new ApiException("OkHttp client request failed.", iOException, "See attached exception for more details."));
        }

        @Override // uq.f
        public void onResponse(uq.e eVar, uq.d0 d0Var) {
            String j10;
            uq.e0 b10 = d0Var.b();
            if (b10 != null) {
                try {
                    j10 = b10.j();
                } catch (IOException e10) {
                    AppSyncGraphQLOperation.LOG.warn("Error retrieving JSON from response.", e10);
                    AppSyncGraphQLOperation.this.onFailure.accept(new ApiException("Could not retrieve the response body from the returned JSON", e10, "Sorry, we don’t have a recovery suggestion for this error."));
                    return;
                }
            } else {
                j10 = null;
            }
            if (d0Var.f() >= AppSyncGraphQLOperation.START_OF_CLIENT_ERROR_CODE && d0Var.f() <= AppSyncGraphQLOperation.END_OF_CLIENT_ERROR_CODE) {
                AppSyncGraphQLOperation.this.onFailure.accept(new ApiException.NonRetryableException("OkHttp client request failed.", "Irrecoverable error"));
                return;
            }
            try {
                AppSyncGraphQLOperation.this.onResponse.accept(AppSyncGraphQLOperation.this.wrapResponse(j10));
            } catch (ApiException e11) {
                AppSyncGraphQLOperation.this.onFailure.accept(e11);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public static <R> Builder<R> builder() {
        return new Builder<>();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void dispatchRequest() {
        try {
            LOG.debug("Request: " + ((GraphQLRequest) getRequest()).getContent());
            uq.e a10 = this.client.a(this.apiRequestDecoratorFactory.fromGraphQLRequest((GraphQLRequest) getRequest()).decorate(new b0.a().y(this.endpoint).a("accept", CONTENT_TYPE).a("content-type", CONTENT_TYPE).q(uq.c0.c(((GraphQLRequest) getRequest()).getContent(), uq.x.g(CONTENT_TYPE))).b()));
            this.ongoingCall = a10;
            FirebasePerfOkHttpClient.enqueue(a10, new OkHttpCallback());
        } catch (Exception e10) {
            uq.e eVar = this.ongoingCall;
            if (eVar != null) {
                eVar.cancel();
            }
            this.onFailure.accept(new ApiException("OkHttp client failed to make a successful request.", e10, "Sorry, we don’t have a recovery suggestion for this error."));
        }
    }

    @Override // com.amplifyframework.core.async.Cancelable
    public synchronized void cancel() {
        uq.e eVar = this.ongoingCall;
        if (eVar != null) {
            eVar.cancel();
        }
    }

    @Override // com.amplifyframework.core.async.AmplifyOperation
    public void start() {
        uq.e eVar = this.ongoingCall;
        if (eVar == null || !(eVar.Q() || this.ongoingCall.e())) {
            this.executorService.submit(new Runnable() { // from class: com.amplifyframework.api.aws.i
                @Override // java.lang.Runnable
                public final void run() {
                    AppSyncGraphQLOperation.this.dispatchRequest();
                }
            });
        }
    }

    private AppSyncGraphQLOperation(Builder<R> builder) {
        super(((Builder) builder).request, ((Builder) builder).responseFactory, ((Builder) builder).apiName);
        String str = ((Builder) builder).endpoint;
        Objects.requireNonNull(str);
        this.endpoint = str;
        uq.z zVar = ((Builder) builder).client;
        Objects.requireNonNull(zVar);
        this.client = zVar;
        ApiRequestDecoratorFactory apiRequestDecoratorFactory = ((Builder) builder).apiRequestDecoratorFactory;
        Objects.requireNonNull(apiRequestDecoratorFactory);
        this.apiRequestDecoratorFactory = apiRequestDecoratorFactory;
        ExecutorService executorService = ((Builder) builder).executorService;
        Objects.requireNonNull(executorService);
        this.executorService = executorService;
        Consumer<GraphQLResponse<R>> consumer = ((Builder) builder).onResponse;
        Objects.requireNonNull(consumer);
        this.onResponse = consumer;
        Consumer<ApiException> consumer2 = ((Builder) builder).onFailure;
        Objects.requireNonNull(consumer2);
        this.onFailure = consumer2;
    }
}
