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