Skip to content

Commit

Permalink
Merge pull request #2 from Time02/master
Browse files Browse the repository at this point in the history
add mix request
  • Loading branch information
zhouaini528 authored Jul 22, 2021
2 parents 5b77afa + 2f65eb9 commit 0825657
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 10 deletions.
24 changes: 18 additions & 6 deletions src/Api/Exchange/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Market extends Request
/**
*GET https://api.coinex.com/v1/market/list
* */
public function getMarketList(array $data=[]){
public function getList(array $data=[]){
$this->type='GET';
$this->path='/market/list';
$this->data=$data;
Expand All @@ -22,7 +22,7 @@ public function getMarketList(array $data=[]){
/**
*GET https://api.coinex.com/v1/market/ticker?market=BCHBTC
* */
public function getMarketTicker(array $data=[]){
public function getTicker(array $data=[]){
$this->type='GET';
$this->path='/market/ticker';
$this->data=$data;
Expand All @@ -32,17 +32,29 @@ public function getMarketTicker(array $data=[]){
/**
*GET https://api.coinex.com/v1/market/depth
* */
public function getMarketDepth(array $data=[]){
public function getDepth(array $data=[]){
$this->type='GET';
$this->path='/market/depth';
$this->data=$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
* */
public function getMarketDeals(array $data=[]){
public function getDeals(array $data=[]){
$this->type='GET';
$this->path='/market/deals';
$this->data=$data;
Expand All @@ -52,7 +64,7 @@ public function getMarketDeals(array $data=[]){
/**
*GET https://api.coinex.com/v1/market/kline?market=BCHBTC&type=1min
* */
public function getMarketKline(array $data=[]){
public function getKline(array $data=[]){
$this->type='GET';
$this->path='/market/kline';
$this->data=$data;
Expand All @@ -72,7 +84,7 @@ public function getMarketInfo(array $data=[]){
/**
*GET https://api.coinex.com/v1/market/detail
* */
public function getMarketDetail(array $data=[]){
public function getDetail(array $data=[]){
$this->type='GET';
$this->path='/market/detail';
$this->data=$data;
Expand Down
12 changes: 12 additions & 0 deletions src/Api/Exchange/Trading.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ 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: 12 additions & 0 deletions src/Api/Perpetual/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ 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: 13 additions & 0 deletions src/Api/Perpetual/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ 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
51 changes: 51 additions & 0 deletions src/CoinexMix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Lin\Coinex;

class CoinexMix extends Request
{
protected $spot; //现货类
protected $futures; //合约类

/**
* CoinexMix constructor.
* @param $spot
* @param $futures
*/
function __construct($spot,$futures){
$this->spot=$spot;
$this->futures=$futures;
}

/**
* 异步同时获取期货和现货盘口
* @param array $spotParam
* @param array $futuresParam
* @param string $spotName
* @param string $futuresName
* @return array
* @throws Exceptions\Exception
*/
function mixGetMarketDepthAsync(array $spotParam,array $futuresParam,string $spotName,string $futuresName){
$requestParams = [];
$requestParams[] = $this->spot->market()->getMarketDepthRequestParams($spotParam,$spotName);
$requestParams[] = $this->futures->market()->getMarketDepthRequestParams($futuresParam,$futuresName);
return $this->execAsync($requestParams);
}

/**
* 异步现货,期货同时下单
* @param array $spotData
* @param array $futuresData
* @param string $spotName
* @param string $futuresName
* @return array
* @throws Exceptions\Exception
*/
function mixPostOrderAsync(array $spotData,array $futuresData,string $spotName,string $futuresName){
$requestParams = [];
$requestParams[] = $this->spot->trading()->postLimitRequestParams($spotData,$spotName);
$requestParams[] = $this->futures->order()->postPutLimitRequestParams($futuresData,$futuresName);
return $this->execAsync($requestParams);
}
}
79 changes: 75 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use GuzzleHttp\Exception\RequestException;
use Lin\Coinex\Exceptions\Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;

class Request
{
Expand Down Expand Up @@ -169,7 +171,7 @@ protected function sort($param)
*
* */
protected function send(){
$client = new \GuzzleHttp\Client();
$client = new Client();

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

Expand All @@ -187,14 +189,83 @@ protected function send(){
}
}

/*echo $this->type.PHP_EOL.$url.PHP_EOL;
print_r($this->options);
die;*/
$response = $client->request($this->type, $url, $this->options);

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

/**
* @param string $functionName
* @return array
*/
protected function getRequestParam(string $functionName){
$this->auth();

$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;
}
}
}

$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
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();
}
}else{
$temp['_message']=$e->getMessage();
}

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

throw new Exception(json_encode($temp));
}
}
/*
*
* */
Expand Down

0 comments on commit 0825657

Please sign in to comment.