-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUptimeRobotAPI.php
188 lines (156 loc) · 4.87 KB
/
UptimeRobotAPI.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace twentysteps\Commons\UptimeRobotBundle;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Encoder\JsonEncode;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use Http\Message\MessageFactory\GuzzleMessageFactory;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use function GuzzleHttp\Psr7\stream_for;
use twentysteps\Commons\EnsureBundle\Ensure;
use twentysteps\Commons\UptimeRobotBundle\Resource\WrappedAccountDetailsResource;
use twentysteps\Commons\UptimeRobotBundle\Resource\WrappedAlertContactResource;
use twentysteps\Commons\UptimeRobotBundle\Resource\WrappedMonitorResource;
use twentysteps\Commons\UptimeRobotBundle\Resource\WrappedMWindowResource;
use twentysteps\Commons\UptimeRobotBundle\Resource\WrappedPSPResource;
use twentysteps\Commons\UptimeRobotBundle\Normalizer\NormalizerFactory;
/**
* Class UptimeRobotAPI
*
* The central entrypoint to all resources provided by this API.
*
* E.g. use monitors()->all() to list all monitors configured at your UptimeRobot.com account.
*
* @package twentysteps\Commons\UptimeRobotBundle
*/
class UptimeRobotAPI {
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var Stopwatch
*/
protected $stopwatch;
/**
* @var string
*/
protected $apiKey;
// initialize
public function __construct(LoggerInterface $logger, string $apiKey, Stopwatch $stopwatch = null) {
$this->logger = $logger;
$this->apiKey = $apiKey;
$this->stopwatch = $stopwatch;
}
// public API
/**
* @return WrappedAccountDetailsResource
*/
public function accountDetails() {
// create the resource
return new WrappedAccountDetailsResource($this,$this->getHTTPClient(),$this->getMessageFactory(),$this->getSerializer());
}
/**
* @return WrappedMonitorResource
*/
public function monitor() {
return new WrappedMonitorResource($this,$this->getHTTPClient(),$this->getMessageFactory(),$this->getSerializer());
}
/**
* @return WrappedAlertContactResource
*/
public function alertContact() {
// create the resource
return new WrappedAlertContactResource($this,$this->getHTTPClient(),$this->getMessageFactory(),$this->getSerializer());
}
/**
* @return WrappedMWindowResource
*/
public function maintenanceWindow() {
// create the resource
return new WrappedMWindowResource($this,$this->getHTTPClient(),$this->getMessageFactory(),$this->getSerializer());
}
/**
* @return WrappedPSPResource
*/
public function publicStatusPage() {
// create the resource
return new WrappedPSPResource($this,$this->getHTTPClient(),$this->getMessageFactory(),$this->getSerializer());
}
/**
* @return mixed
*/
public function getApiKey() {
return $this->apiKey;
}
/**
* @param mixed $apiKey
* @return UptimeRobotAPI
*/
public function setApiKey($apiKey) {
$this->apiKey = $apiKey;
return $this;
}
// helpers
/**
* @param array $config
* @return GuzzleAdapter
*/
protected function getHTTPClient($config = []) {
Ensure::isTrue(is_array($config),'config parameter must be an array');
// configure base uri
$config['base_uri'] = 'https://api.uptimerobot.com';
// add some default headers
if (!array_key_exists('headers',$config)) {
$config['headers']=[];
}
$config['headers']['Content-Type'] = 'application/x-www-form-urlencoded';
// add api_key and Cache-control header to all requests if POST and x-www-form-urlencoded
$handlerStack = new HandlerStack();
$handlerStack->setHandler(new CurlHandler());
$handlerStack->unshift(Middleware::mapRequest(function (RequestInterface $request) {
if ($request->getMethod() == 'POST' && $request->getHeader('Content-Type')[0] == 'application/x-www-form-urlencoded') {
$now = new \DateTime();
$url = $request->getUri().'?cb='.$now->getTimestamp();
return new Request(
$request->getMethod(),
$url,
$request->getHeaders() + ['Content-Type' => 'application/x-www-form-urlencoded', 'Cache-Control' => 'no-cache'],
stream_for($request->getBody() . '&' . http_build_query(['api_key' => $this->getApiKey()])),
$request->getProtocolVersion()
);
}
return $request;
}));
$config['handler'] = $handlerStack;
// create HTTPlug client, message factory and serializer
return GuzzleAdapter::createWithConfig($config);
}
/**
* @return GuzzleMessageFactory
*/
protected function getMessageFactory() {
return new GuzzleMessageFactory();
}
/**
* @return Serializer
*/
protected function getSerializer() {
return new Serializer(
NormalizerFactory::create(),
[
new JsonEncoder(
new JsonEncode(),
new JsonDecode()
)
]
);
}
}