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 22, 2019
1 parent 95baa58 commit eb690b1
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 134 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ Local development sets the proxy [More](https://github.com/zhouaini528/bitmex-ph
```php
$bitmex=new Bitmex();

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

//More flexible Settings
$bitmex->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
//You can set special needs
$bitmex->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 @@ -33,13 +33,21 @@ composer require "linwj/bitmex dev-master"
```php
$bitmex=new Bitmex();

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

//More flexible Settings
$bitmex->setProxy([
'http' => 'http://127.0.0.1:12333',
'https' => 'http://127.0.0.1:12333',
//You can set special needs
$bitmex->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
107 changes: 25 additions & 82 deletions src/Bitmex.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ class Bitmex
protected $secret;
protected $host;

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

function __construct(string $key='',string $secret='',string $host='https://www.bitmex.com'){
$this->key=$key;
Expand All @@ -49,211 +48,155 @@ 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 Announcements List Operations Expand Operations
* */
function announcement(){
$announcement=new Announcement($this->init());
$announcement->proxy($this->proxy);
return $announcement;
return new Announcement($this->init());
}

/**
* Persistent API Keys for Developers List Operations Expand Operations
* */
function apiKey(){
$api_key= new ApiKey($this->init());
$api_key->proxy($this->proxy);
return $api_key;
return new ApiKey($this->init());
}

/**
* Trollbox Data List Operations Expand Operations
* */
function chat(){
$chat= new Chat($this->init());
$chat->proxy($this->proxy);
return $chat;
return new Chat($this->init());
}

/**
* Raw Order and Balance Data List Operations Expand Operations
* */
function execution(){
$execution= new Execution($this->init());
$execution->proxy($this->proxy);
return $execution;
return new Execution($this->init());
}

/**
* Swap Funding History List Operations Expand Operations
* */
function funding(){
$funding= new Funding($this->init());
$funding->proxy($this->proxy);
return $funding;
return new Funding($this->init());
}

/**
* Account Notifications List Operations Expand Operations
* */
function globalNotification(){
$global_notification= new GlobalNotification($this->init());
$global_notification->proxy($this->proxy);
return $global_notification;
return new GlobalNotification($this->init());
}

/**
* Tradeable Contracts, Indices, and History List Operations Expand Operations
* */
function instrument(){
$instrument= new Instrument($this->init());
$instrument->proxy($this->proxy);
return $instrument;
return new Instrument($this->init());
}

/**
* Insurance Fund Data List Operations Expand Operations
* */
function insurance(){
$insurance= new Insurance($this->init());
$insurance->proxy($this->proxy);
return $insurance;
return new Insurance($this->init());
}

/**
* Information on Top Users List Operations Expand Operations
* */
function leaderboard(){
$leaderboard= new Leaderboard($this->init());
$leaderboard->proxy($this->proxy);
return $leaderboard;
return new Leaderboard($this->init());
}

/**
*Active Liquidations List Operations Expand Operations
* */
function liquidation(){
$liquidation= new Liquidation($this->init());
$liquidation->proxy($this->proxy);
return $liquidation;
return new Liquidation($this->init());
}

/**
*Placement, Cancellation, Amending, and History List Operations Expand Operations
* */
function order(){
$order= new Order($this->init());
$order->proxy($this->proxy);
return $order;
return new Order($this->init());
}

/**
*Level 2 Book Data List Operations Expand Operations
* */
function orderBook(){
$order_book =new OrderBook($this->init());
$order_book->proxy($this->proxy);
return $order_book;
return new OrderBook($this->init());
}

/**
*Summary of Open and Closed Positions List Operations Expand Operations
* */
function position(){
$position= new Position($this->init());
$position->proxy($this->proxy);
return $position;
return new Position($this->init());
}

/**
*Best Bid/Offer Snapshots & Historical Bins List Operations Expand Operations
* */
function quote(){
$quote= new Quote($this->init());
$quote->proxy($this->proxy);
return $quote;
return new Quote($this->init());
}

/**
*Dynamic Schemata for Developers List Operations Expand Operations
* */
function schema(){
$schema= new Schema($this->init());
$schema->proxy($this->proxy);
return $schema;
return new Schema($this->init());
}

/**
*Historical Settlement Data List Operations Expand Operations
* */
function settlement(){
$settlement= new Settlement($this->init());
$settlement->proxy($this->proxy);
return $settlement;
return new Settlement($this->init());
}

/**
*Exchange Statistics List Operations Expand Operations
* */
function stats(){
$stats= new Stats($this->init());
$stats->proxy($this->proxy);
return $stats;
return new Stats($this->init());
}

/**
*Individual & Bucketed Trades List Operations Expand Operations
* */
function trade(){
$trade= new Trade($this->init());
$trade->proxy($this->proxy);
return $trade;
return new Trade($this->init());
}

/**
*Account Operations List Operations Expand Operations
* */
function user(){
$user= new User($this->init());
$user->proxy($this->proxy);
return $user;
return new User($this->init());
}

/**
*User Events for auditing
* */
function userEvent(){
$user_event =new UserEvent($this->init());
$user_event->proxy($this->proxy);
return $user_event;
return new UserEvent($this->init());
}
}
51 changes: 22 additions & 29 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,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://www.bitmex.com';

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

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

$this->headers();

$this->options();
}

/**
Expand Down Expand Up @@ -90,41 +90,34 @@ 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['form_params']=$this->data;
if(!empty($this->data)) $this->options['form_params']=$this->data;

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

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

0 comments on commit eb690b1

Please sign in to comment.