1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Multicoin\Api; |
4
|
|
|
|
5
|
|
|
use Multicoin\Api\Traits\User; |
6
|
|
|
use Multicoin\Api\Traits\Address; |
7
|
|
|
use Multicoin\Api\Traits\Invoice; |
8
|
|
|
use Multicoin\Api\Traits\Currency; |
9
|
|
|
use Multicoin\Api\Service\ApiClient; |
10
|
|
|
use Multicoin\Api\Traits\Transaction; |
11
|
|
|
use Http\Message\Authentication\Bearer; |
12
|
|
|
use Http\Client\Common\Plugin\ErrorPlugin; |
13
|
|
|
use Http\Client\Common\Plugin\RetryPlugin; |
14
|
|
|
use Http\Client\Common\Plugin\DecoderPlugin; |
15
|
|
|
use Http\Client\Common\Plugin\HeaderSetPlugin; |
16
|
|
|
use Http\Client\Common\Plugin\QueryDefaultsPlugin; |
17
|
|
|
use Http\Client\Common\Plugin\AuthenticationPlugin; |
18
|
|
|
|
19
|
|
|
class Multicoin |
20
|
|
|
{ |
21
|
|
|
use Address, Invoice, Transaction; |
22
|
|
|
use User, Currency; |
23
|
|
|
|
24
|
|
|
public $coin; |
25
|
|
|
protected $client; |
|
|
|
|
26
|
|
|
|
27
|
|
|
protected $config; |
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct(array $config = [], $client = null) |
30
|
|
|
{ |
31
|
|
|
$this->config = $config; |
32
|
|
|
$this->coin = $config['coin']; |
33
|
|
|
$this->client = $client ?: $this->setClient(); |
34
|
|
|
// $this->setAuth($this->config['key']); |
35
|
|
|
// parent::__construct($this->setUrl($this->config['url'])); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function buildQueryParam(array $default, array $param = []) |
39
|
|
|
{ |
40
|
|
|
//$data = array_filter(array_merge($default, $param), 'strlen'); |
41
|
|
|
$params = array_merge($default, $param); |
42
|
|
|
|
43
|
|
|
return http_build_query($params); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function buildUrl($url) |
47
|
|
|
{ |
48
|
|
|
return '/'.trim($this->coin, '/').$url; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setAuth($apiKey = null) |
52
|
|
|
{ |
53
|
|
|
if (null === $apiKey) { |
54
|
|
|
$apiKey = config('multicoin.api_token'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$authentication = new Bearer($apiKey); |
58
|
|
|
$authenticationPlugin = new AuthenticationPlugin($authentication); |
59
|
|
|
|
60
|
|
|
return $authenticationPlugin; |
61
|
|
|
|
62
|
|
|
return [ |
|
|
|
|
63
|
|
|
$authenticationPlugin, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setClient() |
68
|
|
|
{ |
69
|
|
|
$plugins = $this->setPlugins($this->config['api_token']); |
70
|
|
|
$baseUrl = $this->setUrl($this->config['url']); |
71
|
|
|
|
72
|
|
|
return new ApiClient($baseUrl, $plugins); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function setPlugins($apiKey = null) |
76
|
|
|
{ |
77
|
|
|
$auth = $this->setAuth($apiKey); |
78
|
|
|
$decoderPlugin = new DecoderPlugin(); |
79
|
|
|
$headerSetPlugin = new HeaderSetPlugin([ |
80
|
|
|
'Accept' => 'application/json', |
81
|
|
|
]); |
82
|
|
|
$queryDefaultsPlugin = new QueryDefaultsPlugin([ |
83
|
|
|
'currency' => 'btc', |
84
|
|
|
]); |
85
|
|
|
|
86
|
|
|
return [ |
87
|
|
|
$auth, |
88
|
|
|
$decoderPlugin, |
89
|
|
|
$headerSetPlugin, |
90
|
|
|
$queryDefaultsPlugin, |
91
|
|
|
new RetryPlugin(), |
92
|
|
|
new ErrorPlugin(), |
93
|
|
|
]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setUrl($url = null) |
97
|
|
|
{ |
98
|
|
|
if (null === $url) { |
99
|
|
|
return config('multicoin.url'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $url; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|