This repository has been archived by the owner on Nov 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
302 lines (232 loc) · 10.2 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
require_once 'IndexTank/Index/Client.php';
require_once 'IndexTank/Index/Search.php';
require_once 'IndexTank/Index/Search/Result.php';
require_once 'Zend/Http/Client.php';
require_once 'Zend/Json.php';
class IndexTank_Client
{
const INDEX = "/v1/indexes/%s";
const INDEXING = "/v1/indexes/%s/docs";
const VARIABLES = "/v1/indexes/%s/docs/variables";
const CATEGORIES = "/v1/indexes/%s/docs/categories";
const FUNCTIONS = "/v1/indexes/%s/functions";
const SCORING = "/v1/indexes/%s/functions/%d";
const SEARCH = "/v1/indexes/%s/search";
const PROMOTE = "/v1/indexes/%s/promote";
const AUTOCOMPLETE = "/v1/indexes/%s/autocomplete";
const API_HOSTNAME = "api.indextank.com";
public function __construct($options = null){
if($options == null){
require_once 'IndexTank/Exception/NoConfigOptions.php';
throw new IndexTank_Exception_NoConfigOptions();
}
if(is_string($options)){
// Assume that the $options is the privateUrl config option.
$this->setPrivateUrl($options);
} elseif ( $options instanceof Zend_Config) {
$options = $options->toArray();
} elseif ( is_array($options)){
if(!array_key_exists("privateUrl", $options)){
require_once 'IndexTank/Exception/PrivateUrlNotSet.php';
throw new IndexTank_Exception_PrivateUrlNotSet();
} else {
$this->setPrivateUrl($options['privateUrl']);
}
} else {
require_once 'IndexTank/Exception/PrivateUrlNotSet.php';
throw new IndexTank_Exception_PrivateUrlNotSet();
}
}
public function getUri($endpoint, $name = false){
return $this->_getUriAccessPoint($endpoint, $name);
}
private function _getUriAccessPoint( $endpoint, $name = false){
if(( strtolower( $endpoint ) != "indexes" ) && ($name == "" || $name === false)){
require_once 'IndexTank/Exception/IndexTank.php';
throw new IndexTank_Exception_IndexTank("You have not provided an IndexName for the URI endpoint.");
}
switch(strtolower( $endpoint)){
case "indexes":
if($name == false){
$uri = sprintf( self::INDEX, urlencode(str_replace('/', '', "")) );
} else {
$uri = sprintf( self::INDEX, urlencode(str_replace('/', '', $name)) );
}
break;
case "indexing":
$uri = sprintf( self::INDEXING, urlencode(str_replace('/', '', $name)) );
break;
case "variables":
$uri = sprintf( self::VARIABLES, urlencode(str_replace('/', '', $name)) );
break;
case "categories":
$uri = sprintf( self::CATEGORIES, urlencode(str_replace('/', '', $name)) );
break;
case "search":
$uri = sprintf( self::SEARCH, urlencode(str_replace('/', '', $name)) );
break;
default:
require_once 'IndexTank/Exception/IndexTank.php';
throw new IndexTank_Exception_IndexTank(sprintf( "The API access point: \"%s\" you are trying to use is not valid.", $endpoint));
break;
}
return $uri;
}
public function setPrivateUrl($url){
$apiUrl = parse_url($url);
if(!array_key_exists("pass", $apiUrl)){
require_once 'IndexTank/Exception/PrivateUrlPasswordNotSet.php';
throw new IndexTank_Exception_PrivateUrlPasswordNotSet();
}
$this->setPassword($apiUrl['pass']);
list($key,) = explode('.', $apiUrl['host']);
$this->setApiKey($key);
$this->setSslClient( $apiUrl['scheme'] == 'https' );
}
private function setApiKey( $key ){
$this->_apiKey = $key;
}
private function _getApiKey( ){
return $this->_apiKey;
}
private function setSslClient( $sslRequest ){
if(!is_bool($sslRequest)){
require_once 'IndexTank/Exception/IndexTank.php';
throw new IndexTank_Exception_IndexTank("Argument passed to setSslClient is not of boolean type.");
}
$this->_useSsl = $sslRequest;
}
public function getPassword()
{
return $this->_password;
}
public function setPassword($password)
{
$this->_password = (string) $password;
return $this;
}
public function createIndex($name, $public = false, $waitUntilStarted = true){
$name = str_replace('/','',$name);
if(strlen($name) < 1){
require_once 'IndexTank/Exception/IndexTank.php';
throw new IndexTank_Exception_IndexTank("The Index Name must not be empty and must not contain forward slashes '/'.");
}
$this->_put( $this->_getUriAccessPoint( 'indexes', $name ), array( "public_search" => (boolean) $public ) );
if($waitUntilStarted){
$i = 0;
while (!$this->getIndex($name)->isStarted() && $i < 5) {
$i++;
sleep(2);
}
}
return $this->getIndex($name);
}
public function deleteIndex($name)
{
$this->_delete( $this->_getUriAccessPoint( 'indexes', $name ));
}
public function getIndex($name){
$index = new IndexTank_Index_Client($this);
return $index->getIndex($name);
}
public function useIndex($name){
$index = new IndexTank_Index_Client($this);
return $index->useIndex($name);
}
public function getAllIndexes(){
return $this->_get( $this->_getUriAccessPoint( 'indexes' ));
}
public function deleteAllIndexes(){
$indexes = $this->getAllIndexes();
foreach($indexes as $indexName=>$index){
$this->deleteIndex($indexName);
}
}
public function delete($uri, $options = array()){
return $this->_delete( $uri, $options);
}
private function _delete($uri, $options = array()){
return $this->_call( $uri, $options, 'DELETE');
}
public function put($uri, $params = array()){
return $this->_put( $uri, $params);
}
private function _put($uri, $meta = array()){
return $this->_call( $uri, $meta, 'PUT');
}
public function get($uri, $params = array()){
return $this->_get( $uri, $params);
}
private function _get($uri, $params = array()){
return $this->_call( $uri, $params, 'GET');
}
private function _call ( $uri , $params = array(), $method){
$key = $this->_getApiKey();
if(!isset($key)){
require_once 'IndexTank/Exception/IndexTank.php';
throw new IndexTank_Exception_IndexTank("API is not set for this IndexTank Client");
}
$url = ($this->_useSsl ? 'https' : 'http') . '://' . $this->_getApiKey() . "." . self::API_HOSTNAME;
$client = $this->getHttpClient();
$client->setAuth("", $this->getPassword());
$client->setUri($url . $uri);
if ($method == 'GET') {
$client->setParameterGet($params);
} else {
$client->setRawData(Zend_Json::encode($params), 'application/json');
}
try {
$response = $client->request($method);
} catch (Zend_Http_Exception $e) {
require_once 'IndexTank/Exception/IndexTank.php';
throw new IndexTank_Exception_IndexTank("The API Request failed with: " . $e->getMessage());
}
if (!$response->isSuccessful()) {
$this->_checkCallResponse($response, $method, $uri);
require_once 'IndexTank/Exception/HttpClient.php';
throw new IndexTank_Exception_HttpClient('Http Request failed: (' . $response->getStatus() . ') ' .
$response->getMessage() . ': ' . $response->getBody());
} else {
$this->_checkCallResponse($response, $method, $uri);
}
$data = Zend_Json::decode($response->getBody());
return $data;
}
private function _checkCallResponse($response, $method, $uri){
if($response->getStatus() == "404"){
require_once 'IndexTank/Exception/HttpClient/Response/NoIndex.php';
throw new IndexTank_Exception_HttpClient_Response_NoIndex("The Index does not exist");
}
if($response->getStatus() == "204"){
if($method == "PUT") {
require_once 'IndexTank/Exception/HttpClient/Response/IndexAlreadyExists.php';
throw new IndexTank_Exception_HttpClient_Response_IndexAlreadyExists("The Index you are trying to create already exists");
} else {
require_once 'IndexTank/Exception/HttpClient/Response/IndexDoesNotExist.php';
throw new IndexTank_Exception_HttpClient_Response_IndexDoesNotExist("The Index you are trying to delete does not exist");
}
}
if($response->getStatus() == "409"){
if(dirname($uri) == dirname(self::INDEX)){
require_once 'IndexTank/Exception/HttpClient/Response/TooManyIndexes.php';
throw new IndexTank_Exception_HttpClient_Response_TooManyIndexes("The Index you are trying to create already exists");
}
}
}
protected static $httpClient = null;
public static function setHttpClient(Zend_Http_Client $httpClient)
{
self::$httpClient = $httpClient;
}
// Singleton HTTP Client.
public static function getHttpClient()
{
if (!isset(self::$httpClient)) {
self::$httpClient = new Zend_Http_Client;
} else {
self::$httpClient->resetParameters();
}
return self::$httpClient;
}
}