Skip to content

Commit

Permalink
Add Request Options
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouaini528 committed Aug 23, 2019
1 parent 64ce167 commit c1b07de
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 154 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,21 @@ Local development sets the proxy [More](https://github.com/zhouaini528/huobi-php
```php
$huobi=new HuobiSpot();

//If you are developing locally and need an agent, you can set this
$huobi->setProxy();

//More flexible Settings
$huobi->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
//You can set special needs
$huobi->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
//'verify'=>false,
]);
```

Expand Down
22 changes: 15 additions & 7 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ composer require "linwj/huobi dev-master"
```php
$huobi=new HuobiSpot();

//If you are developing locally and need an agent, you can set this
$huobi->setProxy();

//More flexible Settings
$huobi->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
//You can set special needs
$huobi->setOptions([
//Set the request timeout to 60 seconds by default
'timeout'=>10,

//If you are developing locally and need an agent, you can set this
'proxy'=>true,
//More flexible Settings
/* 'proxy'=>[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
], */
//Close the certificate
//'verify'=>false,
]);
```

Expand Down
36 changes: 7 additions & 29 deletions src/HuobiFuture.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class HuobiFuture
protected $secret;
protected $host;

protected $proxy=false;
protected $timeout=60;
protected $options=[];

function __construct(string $key='',string $secret='',string $host='https://api.hbdm.com'){
$this->key=$key;
Expand All @@ -33,50 +32,29 @@ private function init(){
'key'=>$this->key,
'secret'=>$this->secret,
'host'=>$this->host,
'timeout'=>$this->timeout,
'options'=>$this->options,
];
}

/**
* Local development sets the proxy
* @param bool|array
* $proxy=false Default
* $proxy=true Local proxy http://127.0.0.1:12333
*
* Manual proxy
* $proxy=[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
* ]
* */
function setProxy($proxy=true){
$this->proxy=$proxy;
}

/**
* Set the request timeout to 60 seconds by default
*
* */
function setTimeOut($timeout=60){
$this->timeout=$timeout;
function setOptions(array $options=[]){
$this->options=$options;
}

/**
*
* */
public function contract(){
$contract= new Contract($this->init());
$contract->proxy($this->proxy);
return $contract;
return new Contract($this->init());
}

/**
*
* */
public function market(){
$market= new Market($this->init());
$market->proxy($this->proxy);
return $market;
return new Market($this->init());
}

}
68 changes: 15 additions & 53 deletions src/HuobiSpot.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class HuobiSpot
protected $secret;
protected $host;

protected $proxy=false;
protected $timeout=60;
protected $options=[];

function __construct(string $key='',string $secret='',string $host='https://api.huobi.pro'){
$this->key=$key;
Expand All @@ -39,121 +38,84 @@ private function init(){
'key'=>$this->key,
'secret'=>$this->secret,
'host'=>$this->host,
'timeout'=>$this->timeout,
'options'=>$this->options,
];
}

/**
* Local development sets the proxy
* @param bool|array
* $proxy=false Default
* $proxy=true Local proxy http://127.0.0.1:12333
*
* Manual proxy
* $proxy=[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
* ]
* */
function setProxy($proxy=true){
$this->proxy=$proxy;
}

/**
* Set the request timeout to 60 seconds by default
*
* */
function setTimeOut($timeout=60){
$this->timeout=$timeout;
function setOptions(array $options=[]){
$this->options=$options;
}

/**
*
* */
public function account(){
$account= new Account($this->init());
$account->proxy($this->proxy);
return $account;
return new Account($this->init());
}

/**
*
* */
public function common(){
$common= new Common($this->init());
$common->proxy($this->proxy);
return $common;
return new Common($this->init());
}

/**
*
* */
public function dw(){
$dw= new Dw($this->init());
$dw->proxy($this->proxy);
return $dw;
return new Dw($this->init());
}

/**
*
* */
public function etf(){
$etf= new Etf($this->init());
$etf->proxy($this->proxy);
return $etf;
return new Etf($this->init());
}

/**
*
* */
public function margin(){
$margin= new Margin($this->init());
$margin->proxy($this->proxy);
return $margin;
return new Margin($this->init());
}

/**
*
* */
public function market(){
$market= new Market($this->init());
$market->proxy($this->proxy);
return $market;
return new Market($this->init());
}

/**
*
* */
public function order(){
$order= new Order($this->init());
$order->proxy($this->proxy);
return $order;
return new Order($this->init());
}

/**
*
* */
public function subuser(){
$subuser= new Subuser($this->init());
$subuser->proxy($this->proxy);
return $subuser;
return new Subuser($this->init());
}

/**
*
* */
public function futures(){
$futures = new Futures($this->init());
$futures->proxy($this->proxy);
return $futures;
return new Futures($this->init());
}

/**
*
* */
public function fee(){
$fee = new Fee($this->init());
$fee->proxy($this->proxy);
return $fee;
return new Fee($this->init());
}
}
51 changes: 22 additions & 29 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@ class Request

protected $data=[];

protected $timeout=60;

protected $proxy=false;
protected $options=[];

public function __construct(array $data)
{
$this->key=$data['key'] ?? '';
$this->secret=$data['secret'] ?? '';
$this->host=$data['host'] ?? 'https://api.huobi.pro';

$this->timeout=$data['timeout'] ?? 60;
$this->options=$data['options'] ?? [];
}

/**
Expand All @@ -54,6 +52,8 @@ protected function auth(){
$this->signature();

$this->headers();

$this->options();
}

/**
Expand Down Expand Up @@ -115,43 +115,36 @@ protected function headers(){
}

/**
* 代理端口设置
* @param bool|array
* false 默认
* true 设置本地代理
* array 手动设置代理
* */
function proxy($proxy=false){
$this->proxy=$proxy;
}

/**
* 发送http
* 请求设置
* */
protected function send(){
$client = new \GuzzleHttp\Client();

$data=[
protected function options(){
$this->options=array_merge([
'headers'=>$this->headers,
'timeout'=>$this->timeout
];
//'verify'=>false //关闭证书认证
],$this->options);

$this->options['timeout'] = $this->options['timeout'] ?? 60;

//是否有代理设置
if(is_array($this->proxy)){
$data=array_merge($data,['proxy'=>$this->proxy]);
}else{
if($this->proxy) $data['proxy']=[
if(isset($this->options['proxy']) && $this->options['proxy']===true) {
$this->options['proxy']=[
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
'no' => ['.cn']
];
}
}

/**
* 发送http
* */
protected function send(){
$client = new \GuzzleHttp\Client();

if(!empty($this->data)) {
$data['body']=json_encode($this->data);
$this->options['body']=json_encode($this->data);
}

$response = $client->request($this->type, $this->host.$this->path.'?'.$this->signature, $data);
$response = $client->request($this->type, $this->host.$this->path.'?'.$this->signature, $this->options);

return $response->getBody()->getContents();
}
Expand Down
Loading

0 comments on commit c1b07de

Please sign in to comment.