package com.binance.connector.client.utils.websocketapi;

import com.binance.connector.client.enums.RequestType;
import com.binance.connector.client.exceptions.BinanceConnectorException;
import com.binance.connector.client.utils.JSONParser;
import com.binance.connector.client.utils.ParameterChecker;
import com.binance.connector.client.utils.UrlBuilder;
import com.binance.connector.client.utils.WebSocketConnection;
import com.binance.connector.client.utils.signaturegenerator.SignatureGenerator;
import org.json.JSONObject;

/* loaded from: binance-connector-java-3.0.0rc3.jar:com/binance/connector/client/utils/websocketapi/WebSocketApiRequestHandler.class */
public class WebSocketApiRequestHandler {
    private final SignatureGenerator signatureGenerator;
    private final String apiKey;
    private WebSocketConnection connection;

    public WebSocketApiRequestHandler(WebSocketConnection connection, String apiKey, SignatureGenerator signatureGenerator) {
        if (connection == null) {
            throw new BinanceConnectorException("[WebSocketApiRequestHandler] WebSocketConnection cannot be null");
        }
        this.connection = connection;
        this.apiKey = apiKey;
        this.signatureGenerator = signatureGenerator;
    }

    public void publicRequest(String method) {
        request(RequestType.PUBLIC, method, null);
    }

    public void publicRequest(String method, JSONObject parameters) {
        request(RequestType.PUBLIC, method, parameters);
    }

    public void apiRequest(String method, JSONObject parameters) {
        request(RequestType.WITH_API_KEY, method, parameters);
    }

    public void signedRequest(String method, JSONObject parameters) {
        request(RequestType.SIGNED, method, parameters);
    }

    public void request(RequestType requestType, String method, JSONObject parameters) {
        Object requestId = ParameterChecker.processId(JSONParser.pullValue(parameters, "requestId"), "requestId");
        ParameterChecker.checkParameterType(method, String.class, "method");
        switch (requestType) {
            case PUBLIC:
                this.connection.send(JSONParser.buildJSONString(requestId, method, parameters));
                return;
            case WITH_API_KEY:
                ParameterChecker.checkParameterType(this.apiKey, String.class, "apiKey");
                this.connection.send(JSONParser.buildJSONString(requestId, method, JSONParser.addKeyValue(parameters, "apiKey", this.apiKey)));
                return;
            case SIGNED:
                ParameterChecker.checkParameterType(this.apiKey, String.class, "apiKey");
                JSONObject parameters2 = JSONParser.addKeyValue(parameters, "apiKey", this.apiKey);
                parameters2.put("timestamp", UrlBuilder.buildTimestamp());
                ParameterChecker.checkParameterType(this.signatureGenerator, SignatureGenerator.class, "signatureGenerator");
                parameters2.put("signature", this.signatureGenerator.getSignature(UrlBuilder.joinQueryParameters(JSONParser.sortJSONObject(parameters2))));
                this.connection.send(JSONParser.buildJSONString(requestId, method, parameters2));
                return;
            default:
                throw new BinanceConnectorException("[WebSocketApiRequestHandler] Invalid request type: " + requestType);
        }
    }
}
