ApiCall::post()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 4
1
<?php
2
/**
3
 * ApiCall.php
4
 *
5
 * @copyright	More in license.md
6
 * @license		http://www.ipublikuj.eu
7
 * @author		Adam Kadlec http://www.ipublikuj.eu
8
 * @package		iPublikuj:500px!
9
 * @subpackage	common
10
 * @since		5.0
11
 *
12
 * @date		07.03.15
13
 */
14
15
namespace IPub\FiveHundredPixel;
16
17
use Nette;
18
use Nette\Utils;
19
20
use IPub;
21
use IPub\FiveHundredPixel;
22
use IPub\FiveHundredPixel\Api;
23
24
use IPub\OAuth;
25
26
/**
27
 * Abstract API calls definition
28
 *
29
 * @package		iPublikuj:500px!
30
 * @subpackage	common
31
 *
32
 * @author Adam Kadlec <[email protected]>
33
 */
34
abstract class ApiCall extends Nette\Object
35
{
36
	/**
37
	 * @var OAuth\Consumer
38
	 */
39
	protected $consumer;
40
41
	/**
42
	 * @var OAuth\HttpClient
43
	 */
44
	protected $httpClient;
45
46
	/**
47
	 * @var Configuration
48
	 */
49
	protected $config;
50
51
	/**
52
	 * @param OAuth\Consumer $consumer
53
	 * @param OAuth\HttpClient $httpClient
54
	 * @param Configuration $config
55
	 */
56
	public function __construct(
57
		OAuth\Consumer $consumer,
58
		OAuth\HttpClient $httpClient,
59
		Configuration $config
60
	){
61
		$this->consumer = $consumer;
62
		$this->httpClient = $httpClient;
63
		$this->config = $config;
64
	}
65
66
	/**
67
	 * @internal
68
	 *
69
	 * @return OAuth\HttpClient
70
	 */
71
	public function getHttpClient()
72
	{
73
		return $this->httpClient;
74
	}
75
76
	/**
77
	 * @return Configuration
78
	 */
79
	public function getConfig()
80
	{
81
		return $this->config;
82
	}
83
84
	/**
85
	 * @return OAuth\Consumer
86
	 */
87
	public function getConsumer()
88
	{
89
		return $this->consumer;
90
	}
91
92
	/**
93
	 * Determines the access token that should be used for API calls.
94
	 * The first time this is called, $this->accessToken is set equal
95
	 * to either a valid user access token, or it's set to the application
96
	 * access token if a valid user access token wasn't available.  Subsequent
97
	 * calls return whatever the first call returned.
98
	 *
99
	 * @return OAuth\Token The access token
100
	 */
101
	abstract public function getAccessToken();
102
103
	/**
104
	 * @param string $path
105
	 * @param array $params
106
	 * @param array $headers
107
	 *
108
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
109
	 *
110
	 * @throws OAuth\Exceptions\ApiException
111
	 */
112
	public function get($path, array $params = [], array $headers = [])
113
	{
114
		return $this->api($path, Api\Request::GET, $params, [], $headers);
115
	}
116
117
	/**
118
	 * @param string $path
119
	 * @param array $params
120
	 * @param array $headers
121
	 *
122
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
123
	 *
124
	 * @throws OAuth\Exceptions\ApiException
125
	 */
126
	public function head($path, array $params = [], array $headers = [])
127
	{
128
		return $this->api($path, Api\Request::HEAD, $params, [], $headers);
129
	}
130
131
	/**
132
	 * @param string $path
133
	 * @param array $params
134
	 * @param array $post
135
	 * @param array $headers
136
	 *
137
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
138
	 *
139
	 * @throws OAuth\Exceptions\ApiException
140
	 */
141
	public function post($path, array $params = [], array $post = [], array $headers = [])
142
	{
143
		return $this->api($path, Api\Request::POST, $params, $post, $headers);
144
	}
145
146
	/**
147
	 * @param string $path
148
	 * @param array $params
149
	 * @param array $post
150
	 * @param array $headers
151
	 *
152
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
153
	 *
154
	 * @throws OAuth\Exceptions\ApiException
155
	 */
156
	public function patch($path, array $params = [], array $post = [], array $headers = [])
157
	{
158
		return $this->api($path, Api\Request::PATCH, $params, $post, $headers);
159
	}
160
161
	/**
162
	 * @param string $path
163
	 * @param array $params
164
	 * @param array $post
165
	 * @param array $headers
166
	 *
167
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
168
	 *
169
	 * @throws OAuth\Exceptions\ApiException
170
	 */
171
	public function put($path, array $params = [], array $post = [], array $headers = [])
172
	{
173
		return $this->api($path, Api\Request::PUT, $params, $post, $headers);
174
	}
175
176
	/**
177
	 * @param string $path
178
	 * @param array $params
179
	 * @param array $headers
180
	 *
181
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
182
	 *
183
	 * @throws OAuth\Exceptions\ApiException
184
	 */
185
	public function delete($path, array $params = [], array $headers = [])
186
	{
187
		return $this->api($path, Api\Request::DELETE, $params, [], $headers);
188
	}
189
190
	/**
191
	 * Simply pass anything starting with a slash and it will call the Api, for example
192
	 * <code>
193
	 * $details = $fiveHundredPixel->api('flick.people.info');
194
	 * </code>
195
	 *
196
	 * @param string $path
197
	 * @param string $method The argument is optional
198
	 * @param array $params Query parameters
199
	 * @param array $post Post request parameters or body to send
200
	 * @param array $headers Http request headers
201
	 *
202
	 * @return Utils\ArrayHash|string|Paginator|Utils\ArrayHash[]
203
	 *
204
	 * @throws OAuth\Exceptions\ApiException
205
	 */
206
	public function api($path, $method = Api\Request::GET, array $params = [], array $post = [], array $headers = [])
207
	{
208
		if (is_array($method)) {
209
			$headers = $post;
210
			$post = $params;
211
			$params = $method;
212
			$method = Api\Request::GET;
213
		}
214
215
		$response = $this->httpClient->makeRequest(
216
			new Api\Request($this->consumer, $this->config->createUrl('api', $path, $params), $method, $post, $headers, $this->getAccessToken()),
217
			'HMAC-SHA1'
218
		);
219
220
		if (!$response->isJson() || (!$data = Utils\ArrayHash::from($response->toArray()))) {
221
			$ex = $response->toException();
222
			throw $ex;
223
		}
224
225
		if ($response->isPaginated()) {
226
			return new Paginator($this, $response);
227
		}
228
229
		return Utils\ArrayHash::from($response->toArray());
230
	}
231
232
	/**
233
	 * Upload photo to the 500px
234
	 *
235
	 * @param string $file
236
	 * @param array $params
237
	 *
238
	 * @return Utils\ArrayHash
239
	 *
240
	 * @throws Exceptions\FileNotFoundException
241
	 * @throws Exceptions\FileUploadFailedException
242
	 * @throws OAuth\Exceptions\ApiException|static
243
	 */
244
	public function uploadPhoto($file, array $params = [])
245
	{
246
		if (!is_file($file)) {
247
			throw new Exceptions\FileNotFoundException("File '$file' does not exists. Please provide valid path to file.");
248
		}
249
250
		$result = $this->post('photos', $params);
251
252
		// Check if upload token was successfully created
253
		if ($result instanceof Utils\ArrayHash && $result->offsetExists('upload_key') && $result->offsetExists('photo')) {
254
			$photo = $result->photo;
255
256
			$post = [
257
				'consumer_key' => $this->consumer->key,
258
				'access_key' => $this->getAccessToken()->getToken(),
259
				'upload_key' => $result->upload_key,
260
				'photo_id' => $photo->id,
261
				'file' => new \CURLFile($file)
262
			];
263
264
			$response = $this->httpClient->makeRequest(
265
				new Api\Request($this->consumer, $this->config->createUrl('upload', 'upload', []), Api\Request::POST, $post, [], $this->getAccessToken()),
266
				'HMAC-SHA1'
267
			);
268
269
			if ($response->isOk() && $response->isJson() && $response->getHttpCode() == 200) {
270
				return $photo;
271
			}
272
		}
273
274
		throw new Exceptions\FileUploadFailedException("File '$file' could not be uploaded.'");
275
	}
276
}