1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Olssonm\Roaring; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Main class for the Roaring API-wrapper |
7
|
|
|
*/ |
8
|
|
|
class Roaring |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The fetched token |
12
|
|
|
* |
13
|
|
|
* @var \stdClass |
14
|
|
|
*/ |
15
|
|
|
private $token; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* API consumer key |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $key; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* API consumer secret |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $secret; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The last response |
33
|
|
|
* |
34
|
|
|
* @var mixed |
35
|
|
|
*/ |
36
|
|
|
private $response; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Base URL for the API |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private const BASE_URL = 'https://api.roaring.io'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Constructor |
47
|
|
|
* |
48
|
|
|
* @param string $key |
49
|
|
|
* @param string $secret |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $key = '', string $secret = '', \stdClass $token = null) |
52
|
|
|
{ |
53
|
|
|
$this->key = $key; |
54
|
|
|
$this->secret = $secret; |
55
|
|
|
|
56
|
|
|
($token) ? $this->token = $token : $this->token = $this->generateToken(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Make a GET-request |
61
|
|
|
* |
62
|
|
|
* @param string $path |
63
|
|
|
* @param array $headers |
64
|
|
|
* @param array $parameters |
65
|
|
|
* @return Roaring |
66
|
|
|
*/ |
67
|
|
|
public function get(string $path, array $headers = [], array $parameters = []): Roaring |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
if ($this->token) { |
70
|
|
|
$headers += [ |
71
|
|
|
'Authorization' => sprintf('%s %s', $this->token->token_type, $this->token->access_token) |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$url = sprintf('%s%s', (string) self::BASE_URL, $path); |
76
|
|
|
|
77
|
|
|
$this->response = Request::get($url) |
78
|
|
|
->addHeaders($headers) |
79
|
|
|
->sendsType(\Httpful\Mime::FORM) |
80
|
|
|
->send(); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Make a POST-request |
87
|
|
|
* |
88
|
|
|
* @param string $path |
89
|
|
|
* @param array $headers |
90
|
|
|
* @param array $body |
91
|
|
|
* @return Roaring |
92
|
|
|
*/ |
93
|
|
|
public function post(string $path, array $headers = [], array $body = []): Roaring |
94
|
|
|
{ |
95
|
|
|
if ($this->token) { |
96
|
|
|
$headers += [ |
97
|
|
|
'Authorization' => sprintf('%s %s', $this->token->token_type, $this->token->access_token) |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$url = sprintf('%s%s', (string) self::BASE_URL, $path); |
102
|
|
|
|
103
|
|
|
$this->response = Request::post($url) |
104
|
|
|
->addHeaders($headers) |
105
|
|
|
->sendsType(\Httpful\Mime::FORM) |
106
|
|
|
->body($body) |
107
|
|
|
->send(); |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrive the response from the last request |
114
|
|
|
* |
115
|
|
|
* @param string $type |
116
|
|
|
* @return mixed |
117
|
|
|
*/ |
118
|
|
|
public function getResponse(string $type = null) |
119
|
|
|
{ |
120
|
|
|
if ($this->response) { |
121
|
|
|
if ($type) { |
122
|
|
|
return $this->response->{$type}; |
123
|
|
|
} |
124
|
|
|
return $this->response; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
throw new \Exception("No response available", 1); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Return the generated token |
132
|
|
|
* |
133
|
|
|
* @return \stdClass |
134
|
|
|
*/ |
135
|
|
|
public function getToken(): \stdClass |
136
|
|
|
{ |
137
|
|
|
return $this->token; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Predifined setup to retrieve a token |
142
|
|
|
* |
143
|
|
|
* @return \stdClass |
144
|
|
|
*/ |
145
|
|
|
private function generateToken(): \stdClass |
146
|
|
|
{ |
147
|
|
|
$headers = [ |
148
|
|
|
'Cache-Control' => 'no-cache', |
149
|
|
|
'authorization' => sprintf('Basic %s', (string) base64_encode($this->key . ':' . $this->secret)) |
150
|
|
|
]; |
151
|
|
|
|
152
|
|
|
$parameters = [ |
153
|
|
|
'grant_type' => 'client_credentials' |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
return $this->post('/token', $headers, $parameters)->getResponse('body'); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.