web3p / web3.php

A php interface for interacting with the Ethereum blockchain and ecosystem. Native ABI parsing and smart contract interactions.
MIT License
1.16k stars 543 forks source link

Allow to inject config to GuzzleClient #294

Open kennynguyeenx opened 2 years ago

kennynguyeenx commented 2 years ago

I faced problem when trying to connect to Host which requires Basic Auth and couldn't find any way to config this header in this project. I checked and found that we initiate Guzzle Client inside class HttpRequestManager which is not good. Sometimes we need to configurate something such as authentication or proxy. It would be great if we can allow developer inject config there. Of course, there are some config we don't want to developer to change, we can forbid them to do that but still allow them to change the rest

/**
 * construct
 *
 * @param string $host
 * @param int $timeout
 * @return void
 */
public function __construct($host, $timeout = 1)
{
    parent::__construct($host, $timeout);
    $this->client = new Client;
}

For now I have to create new class which inherits class HttpRequestManager and inject config there

/**
 * construct
 *
 * @param string $host
 * @param int $timeout
 * @param string $apiSecret
 */
public function __construct($host, $timeout = 1, $config = [])
{
    parent::__construct($host, $timeout);
    // We can do some filters here for $config before transferring to Client
    $this->client = new Client($config);
}
mcpicoli commented 5 months ago

Please note that some configurations (in my specific case, the CA file) may be set externally via environment variables or via php.ini settings.