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