1
|
|
|
<?php namespace Syntax\SteamApi; |
2
|
|
|
|
3
|
|
|
use stdClass; |
4
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use Exception; |
7
|
|
|
use Illuminate\Support\Facades\Config; |
8
|
|
|
use GuzzleHttp\Exception\ClientErrorResponseException; |
9
|
|
|
use GuzzleHttp\Exception\ServerErrorResponseException; |
10
|
|
|
use Syntax\SteamApi\Exceptions\ApiCallFailedException; |
11
|
|
|
use Syntax\SteamApi\Exceptions\ClassNotFoundException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @method news() |
15
|
|
|
* @method player($steamId) |
16
|
|
|
* @method user($steamId) |
17
|
|
|
* @method userStats($steamId) |
18
|
|
|
* @method app() |
19
|
|
|
* @method group() |
20
|
|
|
*/ |
21
|
|
|
class Client { |
22
|
|
|
|
23
|
|
|
use SteamId; |
24
|
|
|
|
25
|
|
|
public $validFormats = ['json', 'xml', 'vdf']; |
26
|
|
|
|
27
|
|
|
protected $url = 'http://api.steampowered.com/'; |
28
|
|
|
|
29
|
|
|
protected $client; |
30
|
|
|
|
31
|
|
|
protected $interface; |
32
|
|
|
|
33
|
|
|
protected $method; |
34
|
|
|
|
35
|
|
|
protected $version = 'v0002'; |
36
|
|
|
|
37
|
|
|
protected $apiKey; |
38
|
|
|
|
39
|
|
|
protected $apiFormat = 'json'; |
40
|
|
|
|
41
|
|
|
protected $steamId; |
42
|
|
|
|
43
|
|
|
protected $isService = false; |
44
|
|
|
|
45
|
42 |
|
public function __construct() |
46
|
|
|
{ |
47
|
42 |
|
$apiKey = $this->getApiKey(); |
48
|
|
|
|
49
|
42 |
|
$this->client = new GuzzleClient(); |
50
|
42 |
|
$this->apiKey = $apiKey; |
51
|
|
|
|
52
|
|
|
// Set up the Ids |
53
|
42 |
|
$this->setUpFormatted(); |
54
|
42 |
|
} |
55
|
|
|
|
56
|
|
|
public function get() |
57
|
|
|
{ |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function getSteamId() |
62
|
|
|
{ |
63
|
1 |
|
return $this->steamId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $arguments |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
* |
71
|
|
|
* @throws ApiArgumentRequired |
72
|
|
|
* @throws ApiCallFailedException |
73
|
|
|
*/ |
74
|
10 |
|
protected function setUpService($arguments = null) |
75
|
|
|
{ |
76
|
|
|
// Services have a different url syntax |
77
|
10 |
|
if ($arguments == null) { |
|
|
|
|
78
|
|
|
throw new ApiArgumentRequired; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$parameters = [ |
82
|
10 |
|
'key' => $this->apiKey, |
83
|
10 |
|
'format' => $this->apiFormat, |
84
|
10 |
|
'input_json' => $arguments, |
85
|
10 |
|
]; |
86
|
|
|
|
87
|
10 |
|
$steamUrl = $this->buildUrl(true); |
88
|
|
|
|
89
|
|
|
// Build the query string |
90
|
10 |
|
$parameters = http_build_query($parameters); |
91
|
|
|
|
92
|
|
|
// Send the request and get the results |
93
|
10 |
|
$request = new Request('GET', $steamUrl . '?' . $parameters); |
94
|
10 |
|
$response = $this->sendRequest($request); |
|
|
|
|
95
|
|
|
|
96
|
|
|
// Pass the results back |
97
|
|
|
return $response->body; |
98
|
|
|
} |
99
|
|
|
|
100
|
17 |
|
protected function setUpClient(array $arguments = []) |
101
|
|
|
{ |
102
|
17 |
|
$versionFlag = ! is_null($this->version); |
103
|
17 |
|
$steamUrl = $this->buildUrl($versionFlag); |
104
|
|
|
|
105
|
|
|
$parameters = [ |
106
|
17 |
|
'key' => $this->apiKey, |
107
|
17 |
|
'format' => $this->apiFormat |
108
|
17 |
|
]; |
109
|
|
|
|
110
|
17 |
|
if (! empty($arguments)) { |
111
|
16 |
|
$parameters = array_merge($arguments, $parameters); |
112
|
16 |
|
} |
113
|
|
|
|
114
|
|
|
// Build the query string |
115
|
17 |
|
$parameters = http_build_query($parameters); |
116
|
|
|
|
117
|
|
|
// Send the request and get the results |
118
|
17 |
|
$request = new Request('GET', $steamUrl . '?' . $parameters); |
119
|
17 |
|
$response = $this->sendRequest($request); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// Pass the results back |
122
|
6 |
|
return $response->body; |
123
|
|
|
} |
124
|
|
|
|
125
|
4 |
|
protected function setUpXml(array $arguments = []) |
126
|
|
|
{ |
127
|
4 |
|
$steamUrl = $this->buildUrl(); |
128
|
|
|
|
129
|
|
|
// Build the query string |
130
|
4 |
|
$parameters = http_build_query($arguments); |
131
|
|
|
|
132
|
|
|
// Pass the results back |
133
|
4 |
|
return simplexml_load_file($steamUrl . '?' . $parameters); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \Guzzle\Http\Message\RequestInterface $request |
138
|
|
|
* |
139
|
|
|
* @throws ApiCallFailedException |
140
|
|
|
* @return stdClass |
141
|
|
|
*/ |
142
|
27 |
|
protected function sendRequest($request) |
143
|
|
|
{ |
144
|
|
|
// Try to get the result. Handle the possible exceptions that can arise |
145
|
|
|
try { |
146
|
27 |
|
$response = $this->client->send($request); |
147
|
|
|
|
148
|
6 |
|
$result = new stdClass(); |
149
|
6 |
|
$result->code = $response->getStatusCode(); |
150
|
6 |
|
$result->body = json_decode($response->getBody(true)); |
|
|
|
|
151
|
|
|
|
152
|
27 |
|
} catch (ClientErrorResponseException $e) { |
|
|
|
|
153
|
|
|
throw new ApiCallFailedException($e->getMessage(), $e->getResponse()->getStatusCode(), $e); |
154
|
|
|
|
155
|
21 |
|
} catch (ServerErrorResponseException $e) { |
|
|
|
|
156
|
|
|
throw new ApiCallFailedException('Api call failed to complete due to a server error.', $e->getResponse()->getStatusCode(), $e); |
157
|
|
|
|
158
|
21 |
|
} catch (Exception $e) { |
159
|
21 |
|
throw new ApiCallFailedException($e->getMessage(), $e->getCode(), $e); |
160
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// If all worked out, return the result |
164
|
6 |
|
return $result; |
165
|
|
|
} |
166
|
|
|
|
167
|
30 |
|
private function buildUrl($version = false) |
168
|
|
|
{ |
169
|
|
|
// Set up the basic url |
170
|
30 |
|
$url = $this->url . $this->interface . '/' . $this->method . '/'; |
171
|
|
|
|
172
|
|
|
// If we have a version, add it |
173
|
30 |
|
if ($version) { |
174
|
25 |
|
return $url . $this->version . '/'; |
175
|
|
|
} |
176
|
|
|
|
177
|
5 |
|
return $url; |
178
|
|
|
} |
179
|
|
|
|
180
|
33 |
|
public function __call($name, $arguments) |
181
|
|
|
{ |
182
|
|
|
// Handle a steamId being passed |
183
|
33 |
|
if (! empty($arguments) && count($arguments) == 1) { |
184
|
27 |
|
$this->steamId = $arguments[0]; |
185
|
|
|
|
186
|
27 |
|
$this->convertSteamIdTo64(); |
187
|
27 |
|
} |
188
|
|
|
|
189
|
|
|
// Inside the root steam directory |
190
|
33 |
|
$class = ucfirst($name); |
191
|
33 |
|
$steamClass = '\Syntax\SteamApi\Steam\\' . $class; |
192
|
|
|
|
193
|
33 |
|
if (class_exists($steamClass)) { |
194
|
28 |
|
return new $steamClass($this->steamId); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Inside a nested directory |
198
|
6 |
|
$class = implode('\\', preg_split('/(?=[A-Z])/', $class, -1, PREG_SPLIT_NO_EMPTY)); |
199
|
6 |
|
$steamClass = '\Syntax\SteamApi\Steam\\' . $class; |
200
|
|
|
|
201
|
6 |
|
if (class_exists($steamClass)) { |
202
|
6 |
|
return new $steamClass($this->steamId); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Nothing found |
206
|
|
|
throw new ClassNotFoundException($name); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param Collection $objects |
211
|
|
|
* |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
2 |
|
protected function sortObjects($objects) |
215
|
|
|
{ |
216
|
2 |
|
return $objects->sortBy(function ($object) { |
217
|
2 |
|
return $object->name; |
218
|
2 |
|
}); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string $method |
223
|
|
|
* @param string $version |
224
|
|
|
*/ |
225
|
10 |
|
protected function setApiDetails($method, $version) |
226
|
|
|
{ |
227
|
10 |
|
$this->method = $method; |
228
|
10 |
|
$this->version = $version; |
229
|
10 |
|
} |
230
|
|
|
|
231
|
10 |
|
protected function getServiceResponse($arguments) |
232
|
|
|
{ |
233
|
10 |
|
$arguments = json_encode($arguments); |
234
|
|
|
|
235
|
|
|
// Get the client |
236
|
10 |
|
$client = $this->setUpService($arguments)->response; |
237
|
|
|
|
238
|
|
|
return $client; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return string |
243
|
|
|
* @throws Exceptions\InvalidApiKeyException |
244
|
|
|
*/ |
245
|
42 |
|
protected function getApiKey() |
246
|
|
|
{ |
247
|
42 |
|
$apiKey = \Config::get('steam-api.steamApiKey'); |
248
|
|
|
|
249
|
42 |
|
if ($apiKey == 'YOUR-API-KEY') { |
250
|
|
|
throw new Exceptions\InvalidApiKeyException(); |
251
|
|
|
} |
252
|
42 |
|
if (is_null($apiKey) || $apiKey == '' || $apiKey == []) { |
253
|
42 |
|
$apiKey = getenv('apiKey'); |
254
|
42 |
|
} |
255
|
|
|
|
256
|
42 |
|
return $apiKey; |
257
|
|
|
} |
258
|
|
|
|
259
|
27 |
|
private function convertSteamIdTo64() |
260
|
|
|
{ |
261
|
27 |
|
if (is_array($this->steamId)) { |
262
|
1 |
|
array_walk($this->steamId, function (&$id) { |
263
|
1 |
|
if (strpos($id, ':') !== false) { |
264
|
|
|
// Convert the id to all types and grab the 64 bit version |
265
|
1 |
|
$id = $this->convertToAll($id)->id64; |
266
|
1 |
|
} |
267
|
1 |
|
}); |
268
|
27 |
|
} elseif (strpos(':', $this->steamId) !== false) { |
269
|
|
|
// Convert the id to all types and grab the 64 bit version |
270
|
|
|
$this->steamId = $this->convertToAll($this->steamId)->id64; |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
} |