-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
143 lines (126 loc) · 4.02 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Screenshots client. Need for speaks with screenshot server
* Client.php class file
* @author DOL <[email protected]>
* @date 27.10.14
*/
namespace Cagoi\Screenshots;
use Cagoi\Screenshots\Adapter;
use Cagoi\Screenshots\Params;
use Cagoi\Screenshots\Logger;
class Client {
// Make screenshot action
const MAKE_SCREENSHOT_ACTION = '/create-job';
// Get screenshot action
const GET_SCREENSHOT_ACTION = '/get-job-result';
/** @var Logger\LoggerInterface */
private $_logger;
/**
* Server key
* @var string
*/
private $_server;
/**
* Client key
* @var string
*/
private $_secret;
/**
* Client constructor.
* @param string $server - server key
* @param string $secret - client key
*/
public function __construct($server, $secret) {
$this->_server = $server;
$this->_secret = $secret;
}
/**
* Enable logging
* @param Logger\LoggerInterface $logger
*/
public function setLogger(Logger\LoggerInterface $logger) {
$this->_logger = $logger;
}
/**
* Do request to take screenshot
* @param Params\MakeParams $params
* @param Adapter\AdapterInterface $adapter
* @param ImageCreator $creator
*/
public function makeScreenshot(Params\MakeParams $params, Adapter\AdapterInterface $adapter, ImageCreator $creator) {
// Make request
$params = $params->getParams();
if ($response = $this->postRequest($this->getActionUrl(self::MAKE_SCREENSHOT_ACTION), $params)) {
$adapter->makeScreenshot($response, $creator);
}
}
/**
* Do request to get screenshot
* @param Params\GetParams $params
* @param Adapter\AdapterInterface $adapter
*/
public function getScreenshot(Params\GetParams $params, Adapter\AdapterInterface $adapter) {
// Make request
$params = $params->getParams();
if ($response = $this->postRequest($this->getActionUrl(self::GET_SCREENSHOT_ACTION), $params)) {
$adapter->getScreenshot($response);
}
}
/**
* Handler for callback
* @param array $data - data from server
* @param Adapter\AdapterInterface $adapter
*/
public function onCallback(array $data, Adapter\AdapterInterface $adapter) {
if ($this->_logger) {
$this->_logger->log(Logger\LoggerInterface::CALLBACK, "Callback from {$this->_server}", $data);
}
$adapter->onCallback($data);
}
/**
* Generate url by action
* @param string $action
* @return string - action url
*/
protected function getActionUrl($action) {
return rtrim($this->_server) . $action;
}
/**
* POST request
* @param $url - request to url
* @param array - $data to send
* @param closure - $callback
* @return array
*/
protected function postRequest($url, $data = []) {
if ($this->_logger) {
$this->_logger->log(Logger\LoggerInterface::REQUEST, "Request to {$url}", $data);
}
$ch = curl_init($url);
$postString = http_build_query($data, '', '&');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer '. $this->_secret,
"Cache-Control: no-cache",
]);
$response = curl_exec($ch);
if(curl_errno($ch)) {
if ($this->_logger) {
$this->_logger->log(Logger\LoggerInterface::ERROR, curl_error($ch));
}
} else {
$response = json_decode($response, true);
$response = (is_array($response)) ?
$response : [$response];
if ($this->_logger) {
$this->_logger->log(Logger\LoggerInterface::RESPONSE, "Response from {$url}", $response);
}
return $response;
}
curl_close($ch);
return [];
}
}