15
15
16
16
namespace Apisearch \App ;
17
17
18
+ use Apisearch \Config \Config ;
19
+ use Apisearch \Config \ImmutableConfig ;
20
+ use Apisearch \Exception \ResourceExistsException ;
21
+ use Apisearch \Exception \ResourceNotAvailableException ;
18
22
use Apisearch \Http \Http ;
19
23
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 ;
22
28
23
29
/**
24
30
* Class HttpAppRepository.
@@ -37,7 +43,7 @@ public function addToken(Token $token)
37
43
->get (
38
44
'/token ' ,
39
45
'post ' ,
40
- Http::getQueryValues ($ this ),
46
+ Http::getAppQueryValues ($ this ),
41
47
[
42
48
Http::TOKEN_FIELD => $ token ->toArray (),
43
49
]
@@ -58,7 +64,7 @@ public function deleteToken(TokenUUID $tokenUUID)
58
64
->get (
59
65
'/token ' ,
60
66
'delete ' ,
61
- Http::getQueryValues ($ this ),
67
+ Http::getAppQueryValues ($ this ),
62
68
[
63
69
Http::TOKEN_FIELD => $ tokenUUID ->toArray (),
64
70
]
@@ -79,7 +85,7 @@ public function getTokens(): array
79
85
->get (
80
86
'/tokens ' ,
81
87
'get ' ,
82
- Http::getQueryValues ($ this )
88
+ Http::getAppQueryValues ($ this )
83
89
);
84
90
85
91
self ::throwTransportableExceptionIfNeeded ($ response );
@@ -99,9 +105,159 @@ public function deleteTokens()
99
105
->get (
100
106
'/tokens ' ,
101
107
'delete ' ,
102
- Http::getQueryValues ($ this )
108
+ Http::getAppQueryValues ($ this )
103
109
);
104
110
105
111
self ::throwTransportableExceptionIfNeeded ($ response );
106
112
}
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
+ }
107
263
}
0 commit comments