1 | <?php namespace Syntax\SteamApi; |
||
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() |
|
55 | |||
56 | public function get() |
||
60 | |||
61 | 1 | public function getSteamId() |
|
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) |
|
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 = []) |
|
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) |
|
179 | |||
180 | 33 | public function __call($name, $arguments) |
|
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) |
|
230 | |||
231 | 10 | protected function getServiceResponse($arguments) |
|
240 | |||
241 | /** |
||
242 | * @return string |
||
243 | * @throws Exceptions\InvalidApiKeyException |
||
244 | */ |
||
245 | 42 | protected function getApiKey() |
|
258 | |||
259 | 27 | private function convertSteamIdTo64() |
|
273 | } |