Skip to content

Commit f6fc257

Browse files
authored
Merge pull request #51 from apisearch-io/fix/fixing-app-repository
Moved all index repository logic to AppRepository
2 parents 46ee99b + 864f152 commit f6fc257

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1612
-952
lines changed

App/AppRepository.php

+68-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@
1515

1616
namespace Apisearch\App;
1717

18+
use Apisearch\Config\Config;
19+
use Apisearch\Config\ImmutableConfig;
20+
use Apisearch\Exception\ResourceExistsException;
21+
use Apisearch\Exception\ResourceNotAvailableException;
22+
use Apisearch\Model\Index;
23+
use Apisearch\Model\IndexUUID;
24+
use Apisearch\Model\Token;
25+
use Apisearch\Model\TokenUUID;
1826
use Apisearch\Repository\WithRepositoryReference;
19-
use Apisearch\Token\Token;
20-
use Apisearch\Token\TokenUUID;
2127

2228
/**
2329
* Class AppRepository.
@@ -49,4 +55,64 @@ public function getTokens(): array;
4955
* Purge tokens.
5056
*/
5157
public function deleteTokens();
58+
59+
/**
60+
* Get indices.
61+
*
62+
* @return Index[]
63+
*/
64+
public function getIndices(): array;
65+
66+
/**
67+
* Create an index.
68+
*
69+
* @param IndexUUID $indexUUID
70+
* @param ImmutableConfig $config
71+
*
72+
* @throws ResourceExistsException
73+
*/
74+
public function createIndex(
75+
IndexUUID $indexUUID,
76+
ImmutableConfig $config
77+
);
78+
79+
/**
80+
* Delete an index.
81+
*
82+
* @param IndexUUID $indexUUID
83+
*
84+
* @throws ResourceNotAvailableException
85+
*/
86+
public function deleteIndex(IndexUUID $indexUUID);
87+
88+
/**
89+
* Reset the index.
90+
*
91+
* @param IndexUUID $indexUUID
92+
*
93+
* @throws ResourceNotAvailableException
94+
*/
95+
public function resetIndex(IndexUUID $indexUUID);
96+
97+
/**
98+
* Checks the index.
99+
*
100+
* @param IndexUUID $indexUUID
101+
*
102+
* @return bool
103+
*/
104+
public function checkIndex(IndexUUID $indexUUID): bool;
105+
106+
/**
107+
* Config the index.
108+
*
109+
* @param IndexUUID $indexUUID
110+
* @param Config $config
111+
*
112+
* @throws ResourceNotAvailableException
113+
*/
114+
public function configureIndex(
115+
IndexUUID $indexUUID,
116+
Config $config
117+
);
52118
}

App/HttpAppRepository.php

+162-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@
1515

1616
namespace Apisearch\App;
1717

18+
use Apisearch\Config\Config;
19+
use Apisearch\Config\ImmutableConfig;
20+
use Apisearch\Exception\ResourceExistsException;
21+
use Apisearch\Exception\ResourceNotAvailableException;
1822
use Apisearch\Http\Http;
1923
use Apisearch\Http\HttpRepositoryWithCredentials;
20-
use Apisearch\Token\Token;
21-
use Apisearch\Token\TokenUUID;
24+
use Apisearch\Model\Index;
25+
use Apisearch\Model\IndexUUID;
26+
use Apisearch\Model\Token;
27+
use Apisearch\Model\TokenUUID;
2228

2329
/**
2430
* Class HttpAppRepository.
@@ -37,7 +43,7 @@ public function addToken(Token $token)
3743
->get(
3844
'/token',
3945
'post',
40-
Http::getQueryValues($this),
46+
Http::getAppQueryValues($this),
4147
[
4248
Http::TOKEN_FIELD => $token->toArray(),
4349
]
@@ -58,7 +64,7 @@ public function deleteToken(TokenUUID $tokenUUID)
5864
->get(
5965
'/token',
6066
'delete',
61-
Http::getQueryValues($this),
67+
Http::getAppQueryValues($this),
6268
[
6369
Http::TOKEN_FIELD => $tokenUUID->toArray(),
6470
]
@@ -79,7 +85,7 @@ public function getTokens(): array
7985
->get(
8086
'/tokens',
8187
'get',
82-
Http::getQueryValues($this)
88+
Http::getAppQueryValues($this)
8389
);
8490

8591
self::throwTransportableExceptionIfNeeded($response);
@@ -99,9 +105,159 @@ public function deleteTokens()
99105
->get(
100106
'/tokens',
101107
'delete',
102-
Http::getQueryValues($this)
108+
Http::getAppQueryValues($this)
103109
);
104110

105111
self::throwTransportableExceptionIfNeeded($response);
106112
}
113+
114+
/**
115+
* Get indices.
116+
*
117+
* @return Index[]
118+
*/
119+
public function getIndices(): array
120+
{
121+
if (!empty($appId)) {
122+
$queryParams['app-id'] = $appId;
123+
}
124+
125+
$response = $this
126+
->httpClient
127+
->get(
128+
'/indices',
129+
'get',
130+
Http::getAppQueryValues($this)
131+
);
132+
133+
self::throwTransportableExceptionIfNeeded($response);
134+
135+
$result = [];
136+
foreach ($response['body'] as $index) {
137+
$result[] = Index::createFromArray($index);
138+
}
139+
140+
return $result;
141+
}
142+
143+
/**
144+
* Create an index.
145+
*
146+
* @param IndexUUID $indexUUID
147+
* @param ImmutableConfig $config
148+
*
149+
* @throws ResourceExistsException
150+
*/
151+
public function createIndex(
152+
IndexUUID $indexUUID,
153+
ImmutableConfig $config
154+
) {
155+
$response = $this
156+
->httpClient
157+
->get(
158+
'/index',
159+
'post',
160+
Http::getAppQueryValues($this),
161+
[
162+
Http::INDEX_FIELD => $indexUUID->toArray(),
163+
Http::CONFIG_FIELD => $config->toArray(),
164+
]
165+
);
166+
167+
self::throwTransportableExceptionIfNeeded($response);
168+
}
169+
170+
/**
171+
* Delete an index.
172+
*
173+
* @param IndexUUID $indexUUID
174+
*
175+
* @throws ResourceNotAvailableException
176+
*/
177+
public function deleteIndex(IndexUUID $indexUUID)
178+
{
179+
$response = $this
180+
->httpClient
181+
->get(
182+
'/index',
183+
'delete',
184+
Http::getAppQueryValues($this, $indexUUID)
185+
);
186+
187+
self::throwTransportableExceptionIfNeeded($response);
188+
}
189+
190+
/**
191+
* Reset the index.
192+
*
193+
* @param IndexUUID $indexUUID
194+
*
195+
* @throws ResourceNotAvailableException
196+
*/
197+
public function resetIndex(IndexUUID $indexUUID)
198+
{
199+
$response = $this
200+
->httpClient
201+
->get(
202+
'/index/reset',
203+
'post',
204+
Http::getAppQueryValues($this, $indexUUID)
205+
);
206+
207+
self::throwTransportableExceptionIfNeeded($response);
208+
}
209+
210+
/**
211+
* Checks the index.
212+
*
213+
* @param IndexUUID $indexUUID
214+
*
215+
* @return bool
216+
*/
217+
public function checkIndex(IndexUUID $indexUUID): bool
218+
{
219+
$response = $this
220+
->httpClient
221+
->get(
222+
'/index',
223+
'head',
224+
Http::getAppQueryValues($this, $indexUUID)
225+
);
226+
227+
if (is_null($response)) {
228+
return false;
229+
}
230+
231+
return 200 === $response['code'];
232+
}
233+
234+
/**
235+
* Config the index.
236+
*
237+
* @param IndexUUID $indexUUID
238+
* @param Config $config
239+
*
240+
* @throws ResourceNotAvailableException
241+
*/
242+
public function configureIndex(
243+
IndexUUID $indexUUID,
244+
Config $config
245+
) {
246+
$response = $this
247+
->httpClient
248+
->get(
249+
'/index/config',
250+
'post',
251+
Http::getAppQueryValues($this, $indexUUID),
252+
[
253+
Http::CONFIG_FIELD => $config->toArray(),
254+
]
255+
);
256+
257+
if (is_null($response)) {
258+
return;
259+
}
260+
261+
self::throwTransportableExceptionIfNeeded($response);
262+
}
107263
}

0 commit comments

Comments
 (0)