Skip to content
This repository was archived by the owner on Jun 25, 2025. It is now read-only.

Commit 285eb06

Browse files
committed
Improved endpoints class
1 parent 05a7a48 commit 285eb06

File tree

3 files changed

+183
-9
lines changed

3 files changed

+183
-9
lines changed

Http/Endpoints.php

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static function all(): array
5454
'name' => 'Config',
5555
'description' => 'Configure your index',
5656
'path' => '/v1/index',
57-
'verb' => 'PUT',
57+
'verb' => 'put',
5858
],
5959

6060
/*
@@ -106,11 +106,33 @@ public static function all(): array
106106
'path' => '/v1/logs/stream',
107107
'verb' => 'get',
108108
],
109-
'v1-tokens' => [
110-
'name' => 'Get tokens',
111-
'description' => 'Get app tokens',
109+
110+
/*
111+
* Tokens
112+
*/
113+
'v1-token-add' => [
114+
'name' => 'Add token',
115+
'description' => 'Add token',
112116
'path' => '/v1/token',
113-
'verb' => 'GET',
117+
'verb' => 'post',
118+
],
119+
'v1-token-delete' => [
120+
'name' => 'Delete token',
121+
'description' => 'Delete token',
122+
'path' => '/v1/token',
123+
'verb' => 'delete',
124+
],
125+
'v1-tokens-get' => [
126+
'name' => 'Get all tokens',
127+
'description' => 'Get all tokens',
128+
'path' => '/v1/tokens',
129+
'verb' => 'get',
130+
],
131+
'v1-tokens-delete-all' => [
132+
'name' => 'Delete all tokens',
133+
'description' => 'Delete all tokens',
134+
'path' => '/v1/tokens',
135+
'verb' => 'delete',
114136
],
115137

116138
/*
@@ -125,7 +147,7 @@ public static function all(): array
125147
'v1-interactions-delete' => [
126148
'name' => 'Delete Interactions',
127149
'description' => 'Delete all stored interactions',
128-
'path' => '/v1/interaction',
150+
'path' => '/v1/interactions',
129151
'verb' => 'delete',
130152
],
131153
];
@@ -189,6 +211,19 @@ public static function eventsOnly(): array
189211
];
190212
}
191213

214+
/**
215+
* Read endpoints.
216+
*/
217+
public static function tokensOnly(): array
218+
{
219+
return [
220+
'v1-token-add',
221+
'v1-token-delete',
222+
'v1-tokens-get',
223+
'v1-tokens-delete-all',
224+
];
225+
}
226+
192227
/**
193228
* Interaction endpoints.
194229
*/
@@ -210,10 +245,34 @@ public static function compose(array $endpoints)
210245
{
211246
$all = self::all();
212247

213-
return array_map(function (string $endpoint) use ($all) {
248+
return array_values(array_filter(array_map(function (string $endpoint) use ($all) {
214249
return isset($all[$endpoint])
215-
? $all[$endpoint]['verb'].'~~'.$all[$endpoint]['path']
250+
? strtolower($all[$endpoint]['verb'].'~~'.$all[$endpoint]['path'])
251+
: '';
252+
}, $endpoints)));
253+
}
254+
255+
/**
256+
* From composed.
257+
*
258+
* @param string[] $endpoints
259+
*
260+
* @return string[]
261+
*/
262+
public static function fromComposed(array $endpoints)
263+
{
264+
$all = self::all();
265+
$allInversed = [];
266+
267+
array_walk($all, function (array $element, string $name) use (&$allInversed) {
268+
$composed = strtolower($element['verb'].'~~'.$element['path']);
269+
$allInversed[$composed] = $name;
270+
});
271+
272+
return array_values(array_filter(array_map(function (string $endpoint) use ($allInversed) {
273+
return isset($allInversed[$endpoint])
274+
? $allInversed[$endpoint]
216275
: '';
217-
}, $endpoints);
276+
}, $endpoints)));
218277
}
219278
}

Tests/Http/EndpointsTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Apisearch PHP Client.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Feel free to edit as you please, and have fun.
10+
*
11+
* @author Marc Morera <[email protected]>
12+
* @author PuntMig Technologies
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace Apisearch\Tests\Http;
18+
19+
use Apisearch\Http\Endpoints;
20+
use PHPUnit_Framework_TestCase;
21+
22+
/**
23+
* Class EndpointsTest.
24+
*/
25+
class EndpointsTest extends PHPUnit_Framework_TestCase
26+
{
27+
/**
28+
* All endoints.
29+
*/
30+
private $allEndpoints = [
31+
'v1-index-create',
32+
'v1-index-delete',
33+
'v1-index-reset',
34+
'v1-config',
35+
36+
'v1-items-index',
37+
'v1-items-delete',
38+
39+
'v1-query',
40+
'v1-events',
41+
'v1-events-stream',
42+
'v1-logs',
43+
'v1-logs-stream',
44+
45+
'v1-token-add',
46+
'v1-token-delete',
47+
'v1-tokens-get',
48+
'v1-tokens-delete-all',
49+
50+
'v1-interaction',
51+
'v1-interactions-delete',
52+
];
53+
54+
/**
55+
* All composed endpoints.
56+
*/
57+
private $allComposedEndpoints = [
58+
'post~~/v1/index',
59+
'delete~~/v1/index',
60+
'post~~/v1/index/reset',
61+
'put~~/v1/index',
62+
63+
'post~~/v1/items',
64+
'delete~~/v1/items',
65+
66+
'get~~/v1',
67+
'get~~/v1/events',
68+
'get~~/v1/events/stream',
69+
'get~~/v1/logs',
70+
'get~~/v1/logs/stream',
71+
72+
'post~~/v1/token',
73+
'delete~~/v1/token',
74+
'get~~/v1/tokens',
75+
'delete~~/v1/tokens',
76+
77+
'get~~/v1/interaction',
78+
'delete~~/v1/interactions',
79+
];
80+
81+
/**
82+
* Test compose.
83+
*/
84+
public function testCompose()
85+
{
86+
$this->assertEquals(
87+
$this->allComposedEndpoints,
88+
Endpoints::compose(array_merge(
89+
['v1-non-existing'],
90+
$this->allEndpoints
91+
))
92+
);
93+
}
94+
95+
/**
96+
* Test compose.
97+
*/
98+
public function testFromComposed()
99+
{
100+
$this->assertEquals(
101+
$this->allEndpoints,
102+
Endpoints::fromComposed(array_merge(
103+
['get~~/v1/non/existing'],
104+
$this->allComposedEndpoints
105+
))
106+
);
107+
}
108+
}

Token/Token.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Token implements HttpTransportable
3232
*/
3333
const DEFAULT_TTL = 60;
3434

35+
/**
36+
* @var int
37+
*
38+
* No cache
39+
*/
40+
const NO_CACHE = 0;
41+
3542
/**
3643
* @var int
3744
*

0 commit comments

Comments
 (0)