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

Commit 49709f4

Browse files
committed
Added get tokens endpoint
1 parent 6285807 commit 49709f4

File tree

5 files changed

+90
-11
lines changed

5 files changed

+90
-11
lines changed

App/AppRepository.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,11 @@ public function addToken(Token $token);
3838
* @param TokenUUID $tokenUUID
3939
*/
4040
public function deleteToken(TokenUUID $tokenUUID);
41+
42+
/**
43+
* Get tokens.
44+
*
45+
* @return Token[]
46+
*/
47+
public function getTokens(): array;
4148
}

App/HttpAppRepository.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ public function addToken(Token $token)
4040
'post',
4141
Http::getQueryValues($this),
4242
[
43-
'token' => json_encode($token->toArray()),
44-
]);
43+
Http::TOKEN_FIELD => json_encode($token->toArray()),
44+
]
45+
);
4546

4647
$this->throwTransportableExceptionIfNeeded($response);
4748
}
@@ -60,9 +61,32 @@ public function deleteToken(TokenUUID $tokenUUID)
6061
'delete',
6162
Http::getQueryValues($this),
6263
[
63-
'token' => json_encode($tokenUUID->toArray()),
64-
]);
64+
Http::TOKEN_FIELD => json_encode($tokenUUID->toArray()),
65+
]
66+
);
6567

6668
$this->throwTransportableExceptionIfNeeded($response);
6769
}
70+
71+
/**
72+
* Get tokens.
73+
*
74+
* @return Token[]
75+
*/
76+
public function getTokens(): array
77+
{
78+
$response = $this
79+
->httpClient
80+
->get(
81+
'/token',
82+
'get',
83+
Http::getQueryValues($this)
84+
);
85+
86+
$this->throwTransportableExceptionIfNeeded($response);
87+
88+
return array_map(function (array $token) {
89+
return Token::createFromArray($token);
90+
}, $response['body']);
91+
}
6892
}

Exception/ConnectionException.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Exception;
18+
19+
/**
20+
* Class ConnectionException.
21+
*/
22+
class ConnectionException extends TransportableException
23+
{
24+
/**
25+
* Get http error code.
26+
*
27+
* @return int
28+
*/
29+
public static function getTransportableHTTPError(): int
30+
{
31+
return 500;
32+
}
33+
}

Http/Endpoints.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public static function all(): array
5050
'path' => '/v1/index/reset',
5151
'verb' => 'post',
5252
],
53+
'v1-config' => [
54+
'name' => 'Config',
55+
'description' => 'Configure your index',
56+
'path' => '/v1/index',
57+
'verb' => 'PUT',
58+
],
5359

5460
/*
5561
* Write endpoints
@@ -100,22 +106,28 @@ public static function all(): array
100106
'path' => '/v1/logs/stream',
101107
'verb' => 'get',
102108
],
103-
'v1-config' => [
104-
'name' => 'Config',
105-
'description' => 'Configure your index',
106-
'path' => '/v1/index',
107-
'verb' => 'PUT',
109+
'v1-tokens' => [
110+
'name' => 'Get tokens',
111+
'description' => 'Get app tokens',
112+
'path' => '/v1/token',
113+
'verb' => 'GET',
108114
],
109115

110116
/*
111117
* Interaction endpoints
112118
*/
113119
'v1-interaction' => [
114-
'name' => 'Interactions',
115-
'description' => 'Push interactions',
120+
'name' => 'Add interaction',
121+
'description' => 'Push a new interaction',
116122
'path' => '/v1/interaction',
117123
'verb' => 'get',
118124
],
125+
'v1-interactions-delete' => [
126+
'name' => 'Delete Interactions',
127+
'description' => 'Delete all stored interactions',
128+
'path' => '/v1/interaction',
129+
'verb' => 'delete',
130+
],
119131
];
120132
}
121133

Http/HttpResponsesToException.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
namespace Apisearch\Http;
1818

19+
use Apisearch\Exception\ConnectionException;
1920
use Apisearch\Exception\InvalidFormatException;
2021
use Apisearch\Exception\InvalidTokenException;
2122
use Apisearch\Exception\ResourceExistsException;
@@ -44,6 +45,8 @@ protected function throwTransportableExceptionIfNeeded(array $response)
4445
throw new InvalidFormatException($response['body']['message']);
4546
case ResourceExistsException::getTransportableHTTPError():
4647
throw new ResourceExistsException($response['body']['message']);
48+
case ConnectionException::getTransportableHTTPError():
49+
throw new ConnectionException('Apisearch returned an internal error code [500]');
4750
}
4851
}
4952
}

0 commit comments

Comments
 (0)