Skip to content

Commit

Permalink
Remove RequestManager Class
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0Vu committed Dec 30, 2023
1 parent a80c512 commit 1af508e
Show file tree
Hide file tree
Showing 27 changed files with 376 additions and 563 deletions.
11 changes: 2 additions & 9 deletions src/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
use InvalidArgumentException;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;
use Web3\RequestManagers\WsRequestManager;
use Web3\Utils;
use Web3\Eth;
use Web3\Contracts\Ethabi;
Expand Down Expand Up @@ -120,13 +117,9 @@ public function __construct($provider, $abi, $defaultBlock = 'latest')
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$requestManager = new WsRequestManager($provider);

$this->provider = new WsProvider($requestManager);
$this->provider = new WsProvider($provider);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand Down
11 changes: 2 additions & 9 deletions src/Eth.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;
use Web3\RequestManagers\WsRequestManager;

class Eth
{
Expand Down Expand Up @@ -54,13 +51,9 @@ public function __construct($provider)
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$requestManager = new WsRequestManager($provider);

$this->provider = new WsProvider($requestManager);
$this->provider = new WsProvider($provider);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand Down
11 changes: 2 additions & 9 deletions src/Net.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;
use Web3\RequestManagers\WsRequestManager;

class Net
{
Expand Down Expand Up @@ -54,13 +51,9 @@ public function __construct($provider)
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$requestManager = new WsRequestManager($provider);

$this->provider = new WsProvider($requestManager);
$this->provider = new WsProvider($provider);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand Down
11 changes: 2 additions & 9 deletions src/Personal.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
use Web3\Providers\WsProvider;
use Web3\RequestManagers\WsRequestManager;

class Personal
{
Expand Down Expand Up @@ -54,13 +51,9 @@ public function __construct($provider)
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider);

$this->provider = new HttpProvider($requestManager);
$this->provider = new HttpProvider($provider);
} else if (preg_match('/^wss?:\/\//', $provider) === 1) {
$requestManager = new WsRequestManager($provider);

$this->provider = new WsProvider($requestManager);
$this->provider = new WsProvider($provider);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@

/**
* This file is part of web3.php package.
*
*
* (c) Kuan-Cheng,Lai <[email protected]>
*
*
* @author Peter Lai <[email protected]>
* @license MIT
*/

namespace Web3\RequestManagers;
namespace Web3\Providers;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use RuntimeException as RPCException;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use React;
use React\Async;
use React\EventLoop\Loop;
use React\Http\Browser;
use React\Socket\Connector;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\IRequestManager;
use Web3\Providers\Provider;
use Web3\Providers\IProvider;

class HttpAsyncRequestManager extends RequestManager implements IRequestManager
class HttpAsyncProvider extends Provider implements IProvider
{
/**
* methods
*
* @var array
*/
protected $methods = [];

/**
* client
*
Expand All @@ -48,6 +56,92 @@ public function __construct($host, $timeout = 1)
$this->client = (new Browser($connector, Loop::get()))->withRejectErrorResponse(false);
}

/**
* close
*
* @return void
*/
public function close() {}

/**
* send
*
* @param \Web3\Methods\Method $method
* @param callable $callback
* @return void
*/
public function send($method, $callback)
{
$payload = $method->toPayloadString();

if (!$this->isBatch) {
$proxy = function ($err, $res) use ($method, $callback) {
if ($err !== null) {
return call_user_func($callback, $err, null);
}
if (!is_array($res)) {
$res = $method->transform([$res], $method->outputFormatters);
return call_user_func($callback, null, $res[0]);
}
$res = $method->transform($res, $method->outputFormatters);

return call_user_func($callback, null, $res);
};
return $this->sendPayload($payload, $proxy);
} else {
$this->methods[] = $method;
$this->batch[] = $payload;
}
}

/**
* batch
*
* @param bool $status
* @return void
*/
public function batch($status)
{
$status = is_bool($status);

$this->isBatch = $status;
}

/**
* execute
*
* @param callable $callback
* @return void
*/
public function execute($callback)
{
if (!$this->isBatch) {
throw new \RuntimeException('Please batch json rpc first.');
}
$methods = $this->methods;
$proxy = function ($err, $res) use ($methods, $callback) {
if ($err !== null) {
return call_user_func($callback, $err, null);
}
foreach ($methods as $key => $method) {
if (isset($res[$key])) {
if (!is_array($res[$key])) {
$transformed = $method->transform([$res[$key]], $method->outputFormatters);
$res[$key] = $transformed[0];
} else {
$transformed = $method->transform($res[$key], $method->outputFormatters);
$res[$key] = $transformed;
}
}
}
return call_user_func($callback, null, $res);
};
$r = $this->sendPayload('[' . implode(',', $this->batch) . ']', $proxy);
$this->methods = [];
$this->batch = [];
return $r;
}

/**
* sendPayload
*
Expand Down Expand Up @@ -122,4 +216,4 @@ public function sendPayload($payload, $callback)

return Async\coroutine($request);
}
}
}
98 changes: 91 additions & 7 deletions src/Providers/HttpProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@

namespace Web3\Providers;

use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use RuntimeException as RPCException;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Client;
use Web3\Providers\Provider;
use Web3\Providers\IProvider;
use Web3\RequestManagers\RequestManager;

class HttpProvider extends Provider implements IProvider
{
Expand All @@ -24,15 +29,24 @@ class HttpProvider extends Provider implements IProvider
*/
protected $methods = [];

/**
* client
*
* @var \GuzzleHttp
*/
protected $client;

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

/**
Expand Down Expand Up @@ -66,7 +80,7 @@ public function send($method, $callback)

return call_user_func($callback, null, $res);
};
return $this->requestManager->sendPayload($payload, $proxy);
return $this->sendPayload($payload, $proxy);
} else {
$this->methods[] = $method;
$this->batch[] = $payload;
Expand Down Expand Up @@ -115,9 +129,79 @@ public function execute($callback)
}
return call_user_func($callback, null, $res);
};
$r = $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy);
$r = $this->sendPayload('[' . implode(',', $this->batch) . ']', $proxy);
$this->methods = [];
$this->batch = [];
return $r;
}

/**
* sendPayload
*
* @param string $payload
* @param callable $callback
* @return void
*/
public function sendPayload($payload, $callback)
{
if (!is_string($payload)) {
throw new InvalidArgumentException('Payload must be string.');
}

try {
$res = $this->client->post($this->host, [
'headers' => [
'content-type' => 'application/json'
],
'body' => $payload,
'timeout' => $this->timeout,
'connect_timeout' => $this->timeout
]);
/**
* @var StreamInterface $stream ;
*/
$stream = $res->getBody();
$json = json_decode($stream);
$stream->close();

if (JSON_ERROR_NONE !== json_last_error()) {
call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null);
}
if (is_array($json)) {
// batch results
$results = [];
$errors = [];

foreach ($json as $result) {
if (property_exists($result,'result')) {
$results[] = $result->result;
} else {
if (isset($json->error)) {
$error = $json->error;
$errors[] = new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code);
} else {
$results[] = null;
}
}
}
if (count($errors) > 0) {
call_user_func($callback, $errors, $results);
} else {
call_user_func($callback, null, $results);
}
} elseif (property_exists($json,'result')) {
call_user_func($callback, null, $json->result);
} else {
if (isset($json->error)) {
$error = $json->error;

call_user_func($callback, new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code), null);
} else {
call_user_func($callback, new RPCException('Something wrong happened.'), null);
}
}
} catch (RequestException $err) {
call_user_func($callback, $err, null);
}
}
}
Loading

0 comments on commit 1af508e

Please sign in to comment.