-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastly.api.inc
193 lines (162 loc) · 5.02 KB
/
fastly.api.inc
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
189
190
191
192
193
<?php
/**
* @file
* Contains Faslt class that handles API calls to the Fastly service.
*/
/**
* Fastly API for Drupal.
*/
class Fastly {
/**
* Construct function for the Fastly class.
*/
public function __construct($api_key, $service_id) {
$this->api_key = $api_key;
$this->service_id = $service_id;
$this->host = 'https://api.fastly.com/';
// $this->host = 'http://stg.fastly.com/';
}
/**
* Registers a new customer.
*
* @param array $data
* Data to post to Fastly for the signup request.
*
* @return array
* Data returned from Fastly.s
*/
public function signup($data) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
$result = $this->query('plugin/drupal/signup', $data, 'POST', $headers);
return json_decode($result->data);
}
/**
* Used to validate API key and service ID.
*
* @return bool
* FALSE if any corrupt data is passed.
*/
public function validate() {
return $this->query('current_customer')->status_message == 'OK';
}
/**
* Gets a list of services for the current customer.
*/
public function getServices() {
$result = $this->query('service');
return json_decode($result->data);
}
/**
* Creates a default service for our website once we signed up.
*
* @param array $data
* An array of data to create the service with.
*
* @return object
* Data returned from the Fastly request.
*/
public function createService($data) {
$service = json_decode($this->query('service', $data, 'POST')->data);
if (isset($service->id)) {
$data['service'] = $service->id;
$this->query('service/' . $service->id . '/version/1/domain', array('name' => $data['domain']), 'POST');
unset($data['domain']);
unset($data['address']);
$this->query('service/' . $service->id . '/version/1/backend', $data, 'POST');
$this->query('service/' . $service->id . '/version/1/syslog', $data, 'POST');
$this->query('service/' . $service->id . '/version/1/activate', array(), 'PUT');
}
return $service;
}
/**
* Gets the settings for a version.
*/
public function getSettings() {
$result = $this->query('service/' . $this->service_id . '/version/' . $this->getActiveVersion() . '/settings');
return json_decode($result->data);
}
/**
* Updates the settings for a version.
*
* @param array $data
* An array of settings to update.
*/
public function updateSettings($data) {
if ($this->service_id) {
$active_version = $this->getActiveVersion();
$new_version = json_decode($this->query('service/' . $this->service_id . '/version/' . $active_version . '/clone', array(), 'PUT')->data);
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
$this->query('service/' . $this->service_id . '/version/' . $new_version->number . '/settings', $data, 'PUT', $headers);
$this->query('service/' . $this->service_id . '/version/' . $new_version->number . '/activate', array(), 'PUT');
}
}
/**
* Purge whole service.
*/
public function purgeAll() {
$this->query('service/' . $this->service_id . '/purge_all', array(), 'POST');
}
/**
* Purge cache by path.
*/
public function purgePath($path) {
global $base_url;
$path = str_replace($base_url, '', $path);
$this->purgeQuery($path);
$this->purgeQuery(drupal_get_path_alias($path));
}
/**
* Performs an actual purge request for the given path.
*/
protected function purgeQuery($path) {
drupal_http_request(url($path, array('absolute' => TRUE)), array(
'headers' => array(
'Host' => $_SERVER['HTTP_HOST'],
),
'method' => 'PURGE',
));
}
/**
* Purge cache by key.
*/
public function purgeKey($key) {
$this->query('service/' . $this->service_id . '/purge/' . $key, array(), 'POST');
}
/**
* Gets active version number for the current service.
*/
protected function getActiveVersion() {
$service = json_decode($this->query('service/' . $this->service_id)->data);
foreach ($service->versions as $version) {
if ($version->active) {
return $version->number;
}
}
}
/**
* Performs http queries to Fastly API server.
*
* @param string $uri
* The uri to use for the request, appended to the host.
* @param array $data
* (optional) Data to send with the request.
* @param string $method
* (optional) The method to use for the request, defaults to GET.
* @param array $headers
* (optional) An array of headers to send with the request.
*
* @return object
* From drupal_http_request().
*/
protected function query($uri, $data = array(), $method = 'GET', $headers = array()) {
$url = $this->host . $uri;
$options['headers'] = $headers;
$options['method'] = $method;
$options['data'] = http_build_query($data);
if ($this->api_key) {
$options['headers']['Fastly-Key'] = $this->api_key;
}
$result = drupal_http_request($url, $options);
return $result;
}
}