ApiCall::getHttpClient()   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 0
1
<?php
2
/**
3
 * Client.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:Twitter!
9
 * @subpackage	common
10
 * @since		5.0
11
 *
12
 * @date		06.03.15
13
 */
14
15
namespace IPub\Twitter;
16
17
use Nette;
18
use Nette\Utils;
19
20
use IPub;
21
use IPub\Twitter;
22
use IPub\Twitter\Api;
23
24
use IPub\OAuth;
25
26
/**
27
 * Abstract API calls definition
28
 *
29
 * @package		iPublikuj:Twitter!
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 = $twitter->api('users/show.json');
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 $data;
230
	}
231
232
	/**
233
	 * Upload photo to the Twitter
234
	 *
235
	 * @param string $file
236
	 * @param string|null $status
237
	 *
238
	 * @return Utils\ArrayHash
239
	 *
240
	 * @throws Exceptions\FileNotFoundException
241
	 * @throws Exceptions\InvalidArgumentException
242
	 * @throws OAuth\Exceptions\ApiException|static
243
	 */
244
	public function uploadMedia($file, $status = NULL)
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
		if ($status !== NULL && !is_string($status)) {
251
			throw new Exceptions\InvalidArgumentException("Status text '$status' have to be a string. Please provide valid status text.");
252
		}
253
254
		if ($status !== NULL) {
255
			$post = [
256
				'media[]' => new \CURLFile($file),
257
				'status' => $status
258
			];
259
260
			$result = $this->post('statuses/update_with_media.json', [], $post);
261
262
			return $result instanceof Utils\ArrayHash ? $result : new Utils\ArrayHash;
263
264
		} else {
265
			// Add file to post params
266
			$post = [
267
				'media' => new \CURLFile($file),
268
			];
269
270
			$response = $this->httpClient->makeRequest(
271
				new Api\Request($this->consumer, $this->config->createUrl('upload', 'media/upload.json'), Api\Request::POST, $post, [], $this->getAccessToken()),
272
				'HMAC-SHA1'
273
			);
274
275
			if ($response->isOk() && $response->isJson() && ($data = Utils\ArrayHash::from($response->toArray()))) {
276
				return $data;
277
278
			} else {
279
				$ex = $response->toException();
280
				throw $ex;
281
			}
282
		}
283
	}
284
}