binance / binance-connector-php

This is a thin library that working as a connector to the Binance public API.
78 stars 22 forks source link

Fix bug with showHeader => true and weightUsage => true #25

Open vovkapoc opened 1 year ago

vovkapoc commented 1 year ago

I detect bug with enabled both showHeader and weightUsage, returns only weightUsage in array without header. I fixed this in method processRequest in file /src/Binance/APIClient.php Fixed code:

    private function processRequest($method, $path, $params = array())
    {
        try {
            $response = $this->httpRequest->request($method, $this->buildQuery($path, $params));
        } catch (\GuzzleHttp\Exception\ClientException $e) {
            throw new ClientException($e);
        } catch (\GuzzleHttp\Exception\ServerException $e) {
            throw new ServerException($e);
        }

        $body = json_decode($response->getBody(), true);

        // fixed show header bug with weight usage
        $returnData = null;
        if ($this->showWeightUsage || $this->showHeader) {
            $returnData['data'] = $body;
        }

        if ($this->showWeightUsage) {
            $weights = [];
            foreach ($response->getHeaders() as $name => $value) {
                $name = strtolower($name);
                if (strpos($name, 'x-mbx-used-weight') === 0 ||
                    strpos($name, 'x-mbx-order-count') === 0 ||
                    strpos($name, 'x-sapi-used') === 0) {
                    $weights[$name] = $value;
                }
            }

            $returnData['weight_usage'] = $weights;
        }

        if ($this->showHeader) {
            $returnData['header'] = $response->getHeaders();
        }

        return is_null($returnData) ? $body : $returnData;
    }