1 | <?php |
||
36 | class Client |
||
37 | { |
||
38 | 1 | use SteamId; |
|
39 | |||
40 | public $validFormats = ['json', 'xml', 'vdf']; |
||
41 | |||
42 | protected $url = 'http://api.steampowered.com/'; |
||
43 | |||
44 | protected $client; |
||
45 | |||
46 | protected $interface; |
||
47 | |||
48 | protected $method; |
||
49 | |||
50 | protected $version = 'v0002'; |
||
51 | |||
52 | protected $apiKey; |
||
53 | |||
54 | protected $apiFormat = 'json'; |
||
55 | |||
56 | protected $steamId; |
||
57 | |||
58 | protected $isService = false; |
||
59 | |||
60 | 27 | public function __construct() |
|
70 | |||
71 | public function get() |
||
75 | |||
76 | 1 | public function getSteamId() |
|
80 | |||
81 | /** |
||
82 | * @param string $arguments |
||
83 | * |
||
84 | * @return string |
||
85 | * |
||
86 | * @throws ApiArgumentRequired |
||
87 | * @throws ApiCallFailedException |
||
88 | * @throws GuzzleException |
||
89 | */ |
||
90 | protected function setUpService($arguments = null) |
||
91 | { |
||
92 | // Services have a different url syntax |
||
93 | if ($arguments == null) { |
||
|
|||
94 | throw new ApiArgumentRequired; |
||
95 | } |
||
96 | |||
97 | $parameters = [ |
||
98 | 'key' => $this->apiKey, |
||
99 | 'format' => $this->apiFormat, |
||
100 | 'input_json' => $arguments, |
||
101 | ]; |
||
102 | |||
103 | $steamUrl = $this->buildUrl(true); |
||
104 | |||
105 | // Build the query string |
||
106 | $parameters = http_build_query($parameters); |
||
107 | |||
108 | // Send the request and get the results |
||
109 | $request = new Request('GET', $steamUrl . '?' . $parameters); |
||
110 | $response = $this->sendRequest($request); |
||
111 | |||
112 | // Pass the results back |
||
113 | return $response->body; |
||
114 | } |
||
115 | |||
116 | 8 | protected function setUpClient(array $arguments = []) |
|
147 | |||
148 | 4 | protected function setUpXml(array $arguments = []) |
|
165 | |||
166 | 1 | public function getRedirectUrl() |
|
178 | |||
179 | /** |
||
180 | * @param Request $request |
||
181 | * |
||
182 | * @return stdClass |
||
183 | * @throws ApiCallFailedException |
||
184 | * @throws GuzzleException |
||
185 | */ |
||
186 | 8 | protected function sendRequest(Request $request) |
|
187 | { |
||
188 | // Try to get the result. Handle the possible exceptions that can arise |
||
189 | try { |
||
190 | 8 | $response = $this->client->send($request); |
|
191 | |||
192 | 6 | $result = new stdClass(); |
|
193 | 6 | $result->code = $response->getStatusCode(); |
|
194 | 6 | $result->body = json_decode($response->getBody(true)); |
|
195 | 2 | } catch (ClientException $e) { |
|
196 | 2 | throw new ApiCallFailedException($e->getMessage(), $e->getResponse()->getStatusCode(), $e); |
|
197 | } catch (ServerException $e) { |
||
198 | throw new ApiCallFailedException('Api call failed to complete due to a server error.', $e->getResponse()->getStatusCode(), $e); |
||
199 | } catch (Exception $e) { |
||
200 | throw new ApiCallFailedException($e->getMessage(), $e->getCode(), $e); |
||
201 | } |
||
202 | |||
203 | // If all worked out, return the result |
||
204 | 6 | return $result; |
|
205 | } |
||
206 | |||
207 | 12 | private function buildUrl($version = false) |
|
219 | |||
220 | 15 | public function __call($name, $arguments) |
|
248 | |||
249 | /** |
||
250 | * @param Collection $objects |
||
251 | * |
||
252 | * @return Collection |
||
253 | */ |
||
254 | 2 | protected function sortObjects($objects) |
|
255 | { |
||
256 | 2 | return $objects->sortBy(function ($object) { |
|
257 | 2 | return $object->name; |
|
258 | 2 | }); |
|
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $method |
||
263 | * @param string $version |
||
264 | */ |
||
265 | protected function setApiDetails($method, $version) |
||
266 | { |
||
267 | $this->method = $method; |
||
268 | $this->version = $version; |
||
269 | } |
||
270 | |||
271 | protected function getServiceResponse($arguments) |
||
272 | { |
||
273 | $arguments = json_encode($arguments); |
||
274 | |||
275 | // Get the client |
||
276 | return $this->setUpService($arguments)->response; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return string |
||
281 | * @throws Exceptions\InvalidApiKeyException |
||
282 | */ |
||
283 | 27 | protected function getApiKey() |
|
296 | |||
297 | 8 | private function convertSteamIdTo64() |
|
298 | { |
||
299 | 8 | if (is_array($this->steamId)) { |
|
300 | 1 | array_walk($this->steamId, function (&$id) { |
|
301 | // Convert the id to all types and grab the 64 bit version |
||
309 | } |
||
310 |