1 | <?php namespace Syntax\SteamApi; |
||
19 | class Client |
||
20 | { |
||
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 | 41 | public function __construct() |
|
45 | { |
||
46 | 41 | $apiKey = $this->getApiKey(); |
|
47 | |||
48 | 41 | $this->client = new GuzzleClient(['base_uri' => $this->url]); |
|
49 | |||
50 | 41 | $this->apiKey = $apiKey; |
|
51 | |||
52 | // Set up the Ids |
||
53 | 41 | $this->setUpFormatted(); |
|
54 | 41 | } |
|
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) |
|
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 = $this->client->get($steamUrl . '?' . $parameters); |
|
94 | $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 = $this->client->get($steamUrl . '?' . $parameters); |
|
119 | 6 | $response = $this->sendRequest($request); |
|
120 | |||
121 | // Pass the results back |
||
122 | 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 | 6 | protected function sendRequest($request) |
|
167 | |||
168 | 30 | private function buildUrl($version = false) |
|
180 | |||
181 | 33 | public function __call($name, $arguments) |
|
209 | |||
210 | /** |
||
211 | * @param Collection $objects |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | protected function sortObjects($objects) |
||
221 | |||
222 | /** |
||
223 | * @param string $method |
||
224 | * @param string $version |
||
225 | */ |
||
226 | 10 | protected function setApiDetails($method, $version) |
|
231 | |||
232 | 10 | protected function getServiceResponse($arguments) |
|
241 | |||
242 | /** |
||
243 | * @return string |
||
244 | * @throws Exceptions\InvalidApiKeyException |
||
245 | */ |
||
246 | 41 | protected function getApiKey() |
|
259 | |||
260 | 27 | private function convertSteamIdTo64() |
|
274 | } |
||
275 |