Skip to content

Commit

Permalink
Add async and Optimize code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Jul 23, 2021
1 parent 97ddc5d commit f5e2e0f
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 196 deletions.
12 changes: 0 additions & 12 deletions src/Api/Exchange/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,6 @@ public function getDepth(array $data=[]){
return $this->exec();
}

/**
* @param array $data
* @param string|null $functionName
* @return array|bool
*/
public function getDepthRequestParams(array $data,string $functionName){
$this->type='GET';
$this->path='/market/depth';
$this->data=$data;
return $this->getRequestParam($functionName);
}

/**
*GET https://api.coinex.com/v1/market/deals?market=BCHBTC
* */
Expand Down
12 changes: 0 additions & 12 deletions src/Api/Exchange/Trading.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,6 @@ public function postLimit(array $data=[]){
return $this->exec();
}

/**
* @param array $data
* @param string $functionName
* @return array
*/
public function postLimitRequestParams(array $data,string $functionName)
{
$this->type='POST';
$this->path='/order/limit';
$this->data=$data;
return $this->getRequestParam($functionName);
}
/**
*POST https://api.coinex.com/v1/order/limit/batch
* */
Expand Down
12 changes: 0 additions & 12 deletions src/Api/Perpetual/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,6 @@ public function getDepth(array $data=[]){
return $this->exec();
}

/**
* @param array $data
* @param string $functionName
* @return array
*/
public function getDepthRequestParams(array $data,string $functionName){
$this->type='GET';
$this->path='/market/depth';
$this->data=$data;
return $this->getRequestParam($functionName);
}

/**
*GET https://api.coinex.com/perpetual/v1/market/deals
* */
Expand Down
13 changes: 0 additions & 13 deletions src/Api/Perpetual/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ public function postPutLimit(array $data=[]){
return $this->exec();
}

/**
* @param array $data
* @param string $functionName
* @return array
*/
public function postPutLimitRequestParams(array $data,string $functionName)
{
$this->type='POST';
$this->path='/order/put_limit';
$this->data=$data;
return $this->getRequestParam($functionName);
}

/**
*POST https://api.coinex.com/perpetual/v1/order/put_market
* */
Expand Down
31 changes: 19 additions & 12 deletions src/CoinexExchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,49 @@ function setOptions(array $options=[]){
/**
*
* */
function account(){
return new Account($this->init());
function account(array $options=[]){
return new Account(array_merge($this->init(),$options));
}

/**
*
* */
function common(){
return new Common($this->init());
function common(array $options=[]){
return new Common(array_merge($this->init(),$options));
}

/**
*
* */
function cntract(){
return new Contract($this->init());
function cntract(array $options=[]){
return new Contract(array_merge($this->init(),$options));
}

/**
*
* */
function margin(){
return new Margin($this->init());
function margin(array $options=[]){
return new Margin(array_merge($this->init(),$options));
}

/**
*
* */
function market(){
return new Market($this->init());
function market(array $options=[]){
return new Market(array_merge($this->init(),$options));
}

/**
*
* */
function trading(){
return new Trading($this->init());
function trading(array $options=[]){
return new Trading(array_merge($this->init(),$options));
}

/**
* @return array
* */
function sendAsync(array $options=[]){
return (new Request($this->init()))->exec(['async'=>$options]);
}
}
51 changes: 0 additions & 51 deletions src/CoinexMix.php

This file was deleted.

27 changes: 17 additions & 10 deletions src/CoinexPerpetual.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,35 +52,42 @@ function setOptions(array $options=[]){
/**
*
* */
function asset(){
return new Asset($this->init());
function asset(array $options=[]){
return new Asset(array_merge($this->init(),$options));
}

/**
*
* */
function market(){
return new Market($this->init());
function market(array $options=[]){
return new Market(array_merge($this->init(),$options));
}

/**
*
* */
function order(){
return new Order($this->init());
function order(array $options=[]){
return new Order(array_merge($this->init(),$options));
}

/**
*
* */
function position(){
return new Position($this->init());
function position(array $options=[]){
return new Position(array_merge($this->init(),$options));
}

/**
*
* */
function system(){
return new System($this->init());
function system(array $options=[]){
return new System(array_merge($this->init(),$options));
}

/**
* @return array
* */
function sendAsync(array $options=[]){
return (new Request($this->init()))->exec(['async'=>$options]);
}
}
102 changes: 28 additions & 74 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Request

protected $version='';

protected $async='';

public function __construct(array $data)
{
$this->key=$data['key'] ?? '';
Expand All @@ -49,6 +51,8 @@ public function __construct(array $data)

$this->platform=$data['platform'] ?? [];
$this->version=$data['version'] ?? [];

$this->async=$data['async'] ?? '';
}

/**
Expand Down Expand Up @@ -168,7 +172,7 @@ protected function sort($param)
}

/**
*
*@return mixed string|GuzzleHttp\Promise
* */
protected function send(){
$client = new Client();
Expand All @@ -189,90 +193,40 @@ protected function send(){
}
}

$response = $client->request($this->type, $url, $this->options);

return $response->getBody()->getContents();
}

/**
* @param string $functionName
* @return array
*/
protected function getRequestParam(string $functionName){
$this->auth();
/*echo $this->type.PHP_EOL.$url.PHP_EOL;
print_r($this->options);*/

$url=$this->host.$this->path;

if($this->type!='POST') $url.= empty($this->data) ? '' : '?'.http_build_query($this->data);
else {
switch ($this->platform){
case 'exchange':{
$this->options['body']=json_encode($this->data);
break;
}
case 'perpetual':{
$this->options['form_params']=$this->data;
break;
}
}
if(!empty($this->async)) {
return [$this->async=>$client->requestAsync($this->type, $url, $this->options)];
} else {
$response = $client->request($this->type, $url, $this->options);
return $response->getBody()->getContents();
}

$requestParam = [
'type' => $this->type,
'url' => $url,
'option' => $this->options,
'functionName' => $functionName,
];

return $requestParam;
}

/**
* @param array $requestParams
* @return array
* @throws Exception
* @throws \Throwable
*/
protected function execAsync(array $requestParams=[]){

$client = new Client();

$promises = [];
$responses = [];
foreach ($requestParams as $v) {
$promises[$v['functionName']] = $client->requestAsync($v['type'], $v['url'], $v['option']);
}
// Wait for the requests to complete; throws a ConnectException
// if any of the requests fail
*
* */
public function exec(array $param=[]){
try {
return Promise\Utils::unwrap($promises);
} catch (RequestException $e) {
if(method_exists($e->getResponse(),'getBody')){
$contents=$e->getResponse()->getBody()->getContents();

$temp=json_decode($contents,true);
if(!empty($temp)) {
$temp['_method']=$this->type;
$temp['_url']=$this->host.$this->path;
}else{
$temp['_message']=$e->getMessage();
if(isset($param['async']) && !empty($param['async'])){
$async=[];
foreach ($param['async'] as $k=>$v) {
if(is_object(current($v))) $async[key($v)]=current($v);
else throw new Exception('Is not an async type');
}
}else{
$temp['_message']=$e->getMessage();

$temp=[];
$unwarp=Promise\Utils::unwrap($async);
foreach ($unwarp as $k=>$v) $temp[$k]=json_decode($v->getBody()->getContents(),true);

return $temp;
}

$temp['_httpcode']=$e->getCode();
$this->auth();

throw new Exception(json_encode($temp));
}
}
/*
*
* */
protected function exec(){
$this->auth();
if(!empty($this->async)) return $this->send();

try {
return json_decode($this->send(),true);
}catch (RequestException $e){
if(method_exists($e->getResponse(),'getBody')){
Expand Down
Loading

0 comments on commit f5e2e0f

Please sign in to comment.