|
1
|
|
|
<?php namespace UON; |
|
2
|
|
|
|
|
3
|
|
|
use UON\Interfaces\ConfigInterface; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Paul Rock <[email protected]> |
|
7
|
|
|
* @link http://drteam.rocks |
|
8
|
|
|
* @license MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
class Client |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Initial state of some variables |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $_client; |
|
16
|
|
|
protected $_config; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Default server parameters |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $host = 'api.u-on.ru'; |
|
22
|
|
|
protected $port = '443'; |
|
23
|
|
|
protected $path = '/'; |
|
24
|
|
|
protected $useSSL = true; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* User initial values |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $token; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Default format of output |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $format = 'json'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Client constructor. |
|
38
|
|
|
* @param ConfigInterface $config User defined configuration |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ConfigInterface $config) |
|
41
|
|
|
{ |
|
42
|
|
|
// Extract toke from config |
|
43
|
|
|
$this->token = $config->get('token'); |
|
44
|
|
|
|
|
45
|
|
|
// Save config into local variable |
|
46
|
|
|
$this->_config = $config; |
|
47
|
|
|
|
|
48
|
|
|
// Store the client object |
|
49
|
|
|
$this->_client = new \GuzzleHttp\Client($config->getParameters(true)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Make the request and analyze the result |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $type Request method |
|
56
|
|
|
* @param string $endpoint Api request endpoint |
|
57
|
|
|
* @param array $params Parameters |
|
58
|
|
|
* @return array|false Array with data or error, or False when something went fully wrong |
|
59
|
|
|
* @throws |
|
60
|
|
|
*/ |
|
61
|
|
|
public function doRequest($type, $endpoint, array $params = []) |
|
62
|
|
|
{ |
|
63
|
|
|
// Create the base URL |
|
64
|
|
|
$base = $this->useSSL ? 'https' : 'http'; |
|
65
|
|
|
|
|
66
|
|
|
// Generate the URL for request |
|
67
|
|
|
$url = $base . '://' . $this->host . ':' . $this->port . $this->path . $this->token . $endpoint . '.' . $this->format; |
|
68
|
|
|
|
|
69
|
|
|
// |
|
70
|
|
|
$result = \in_array($type, ['get', 'post', 'put', 'delete']) |
|
71
|
|
|
? $this->_client->request($type, $url, array('form_params' => $params)) |
|
72
|
|
|
: null; |
|
73
|
|
|
|
|
74
|
|
|
return [ |
|
75
|
|
|
'code' => $result->getStatusCode(), |
|
|
|
|
|
|
76
|
|
|
'reason' => $result->getReasonPhrase(), |
|
77
|
|
|
'message' => json_decode($result->getBody()) |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.