@@ -13,10 +13,10 @@ import io.cequence.pineconescala.domain.settings.IndexSettings.{
13
13
import io .cequence .pineconescala .domain .settings ._
14
14
import io .cequence .pineconescala .domain .{Metric , PodType }
15
15
import io .cequence .wsclient .JsonUtil .JsonOps
16
- import io .cequence .wsclient .domain .WsRequestContext
16
+ import io .cequence .wsclient .ResponseImplicits ._
17
+ import io .cequence .wsclient .domain .{RichResponse , WsRequestContext }
17
18
import io .cequence .wsclient .service .ws .{Timeouts , WSRequestHelper }
18
19
import play .api .libs .json .JsValue
19
- import play .api .libs .ws .StandaloneWSRequest
20
20
21
21
import scala .concurrent .{ExecutionContext , Future }
22
22
@@ -59,7 +59,7 @@ private final class ServerlessIndexServiceImpl(
59
59
dimension : Int ,
60
60
settings : CreateServerlessIndexSettings
61
61
): Future [CreateResponse ] =
62
- execPOSTWithStatus (
62
+ execPOSTRich (
63
63
indexesEndpoint,
64
64
bodyParams = {
65
65
jsonBodyParams(
@@ -120,7 +120,7 @@ private final class PineconePodPineconeBasedImpl(
120
120
dimension : Int ,
121
121
settings : CreatePodBasedIndexSettings
122
122
): Future [CreateResponse ] =
123
- execPOSTWithStatus (
123
+ execPOSTRich (
124
124
indexesEndpoint,
125
125
bodyParams = jsonBodyParams(
126
126
Tag .fromCreatePodBasedIndexSettings(name, dimension, settings): _*
@@ -133,21 +133,22 @@ private final class PineconePodPineconeBasedImpl(
133
133
replicas : Option [Int ],
134
134
podType : Option [PodType ]
135
135
): Future [ConfigureIndexResponse ] =
136
- execPATCHWithStatus (
136
+ execPATCRich (
137
137
indexesEndpoint,
138
138
endPointParam = Some (indexName),
139
139
bodyParams = jsonBodyParams(
140
140
Tag .replicas -> replicas,
141
141
Tag .pod_type -> podType.map(_.toString)
142
142
)
143
- ).map { response =>
144
- val (statusCode, message) = statusCodeAndMessage(response)
143
+ ).map { richResponse =>
144
+ val status = richResponse.status
145
145
146
- statusCode match {
146
+ status.code match {
147
147
case 202 => ConfigureIndexResponse .Updated
148
148
case 400 => ConfigureIndexResponse .BadRequestNotEnoughQuota
149
149
case 404 => ConfigureIndexResponse .NotFound
150
- case _ => throw new PineconeScalaClientException (s " Code ${statusCode} : ${message}" )
150
+ case _ =>
151
+ throw new PineconeScalaClientException (s " Code ${status.code} : ${status.message}" )
151
152
}
152
153
}
153
154
@@ -165,7 +166,7 @@ private final class PineconePodPineconeBasedImpl(
165
166
name : String ,
166
167
source : String
167
168
): Future [CreateResponse ] =
168
- execPOSTWithStatus (
169
+ execPOSTRich (
169
170
EndPoint .collections,
170
171
bodyParams = jsonBodyParams(
171
172
Tag .name -> Some (name),
@@ -175,7 +176,8 @@ private final class PineconePodPineconeBasedImpl(
175
176
).map(handleCreateResponse)
176
177
177
178
override def listCollections : Future [Seq [String ]] =
178
- execGET(EndPoint .collections).map(response => response.asSafe[Seq [String ]]
179
+ execGET(EndPoint .collections).map(
180
+ _.asSafeJson[Seq [String ]]
179
181
// response
180
182
// .asSafe[Seq[String]](response \ "collections")
181
183
// .asOpt[Seq[JsValue]]
@@ -206,41 +208,44 @@ abstract class PineconeIndexServiceImpl[S <: IndexSettings](
206
208
override protected type PEP = EndPoint
207
209
override protected type PT = Tag
208
210
209
- override protected val requestContext = WsRequestContext (explTimeouts = explicitTimeouts)
211
+ override protected val requestContext = WsRequestContext (
212
+ authHeaders = Seq ((" Api-Key" , apiKey)),
213
+ explTimeouts = explicitTimeouts
214
+ )
210
215
211
216
def isPodBasedIndex : Boolean = environment.isDefined
212
217
def isServerlessIndex : Boolean = ! isPodBasedIndex
213
218
214
219
override def describeCollection (
215
220
collectionName : String
216
221
): Future [Option [CollectionInfo ]] =
217
- execGETWithStatus (
222
+ execGETRich (
218
223
EndPoint .collections,
219
224
endPointParam = Some (collectionName)
220
225
).map { response =>
221
226
handleNotFoundAndError(response).map(
222
- _.asSafe [CollectionInfo ]
227
+ _.asSafeJson [CollectionInfo ]
223
228
)
224
229
}
225
230
226
231
override def deleteCollection (
227
232
collectionName : String
228
233
): Future [DeleteResponse ] =
229
- execDELETEWithStatus (
234
+ execDELETERich (
230
235
EndPoint .collections,
231
236
endPointParam = Some (collectionName),
232
237
acceptableStatusCodes = Nil // don't parse response at all
233
238
).map(handleDeleteResponse)
234
239
235
240
override def listIndexes : Future [Seq [String ]] =
236
241
execGET(indexesEndpoint).map(response =>
237
- (response \ " indexes" )
242
+ (response.json \ " indexes" )
238
243
.asOpt[Seq [JsValue ]]
239
244
.map(indexes => {
240
245
indexes.flatMap(index => (index \ " name" ).asOpt[String ])
241
246
})
242
247
.getOrElse(
243
- response.asSafe [Seq [String ]]
248
+ response.asSafeJson [Seq [String ]]
244
249
)
245
250
)
246
251
@@ -249,17 +254,19 @@ abstract class PineconeIndexServiceImpl[S <: IndexSettings](
249
254
override def describeIndex (
250
255
indexName : String
251
256
): Future [Option [IndexInfo ]] =
252
- execGETWithStatus (
257
+ execGETRich (
253
258
indexesEndpoint,
254
259
endPointParam = Some (indexName)
255
- ).map { response : RichJsResponse =>
256
- handleNotFoundAndError(response).map(describeIndexResponse)
260
+ ).map { richResponse : RichResponse =>
261
+ handleNotFoundAndError(richResponse).map(response =>
262
+ describeIndexResponse(response.json)
263
+ )
257
264
}
258
265
259
266
override def deleteIndex (
260
267
indexName : String
261
268
): Future [DeleteResponse ] =
262
- execDELETEWithStatus (
269
+ execDELETERich (
263
270
indexesEndpoint,
264
271
endPointParam = Some (indexName),
265
272
acceptableStatusCodes = Nil // don't parse response at all
@@ -271,25 +278,6 @@ abstract class PineconeIndexServiceImpl[S <: IndexSettings](
271
278
// otherwise (serverless arch) we use indexes endpoint
272
279
protected def indexesEndpoint : PEP // Either[EndPoint.databases.type, EndPoint.indexes.type]
273
280
274
- // TODO: remove this and use WsRequestContext instead
275
- override protected def addHeaders (request : StandaloneWSRequest ): StandaloneWSRequest = {
276
- val apiKeyHeader = (" Api-Key" , apiKey)
277
- request.addHttpHeaders(apiKeyHeader)
278
- }
279
-
280
- protected def statusCodeAndMessage (
281
- response : RichJsResponse
282
- ): (Int , String ) =
283
- response match {
284
- case Right (statusCodeAndMessage) => statusCodeAndMessage
285
-
286
- // should never happen
287
- case Left (json) =>
288
- throw new IllegalArgumentException (
289
- s " Status code and message expected but got a json value ' ${json}'. "
290
- )
291
- }
292
-
293
281
/**
294
282
* This operation creates a Pinecone index. You can use it to specify the measure of
295
283
* similarity, the dimension of vectors to be stored in the index, the numbers of replicas to
@@ -318,27 +306,27 @@ abstract class PineconeIndexServiceImpl[S <: IndexSettings](
318
306
): Nothing =
319
307
throw new PineconeScalaClientException (s " Code ${httpCode} : ${message}" )
320
308
321
- protected def handleCreateResponse (response : RichJsResponse ): CreateResponse = {
322
- val (statusCode, message) = statusCodeAndMessage(response)
323
-
324
- statusCode match {
309
+ protected def handleCreateResponse (response : RichResponse ): CreateResponse =
310
+ response.status.code match {
325
311
case 201 => CreateResponse .Created
326
312
// Encountered when request exceeds quota or an invalid index name.
327
313
case 400 => CreateResponse .BadRequest
328
314
case 409 => CreateResponse .AlreadyExists
329
- case _ => throw new PineconeScalaClientException (s " Code ${statusCode} : ${message}" )
315
+ case _ =>
316
+ throw new PineconeScalaClientException (
317
+ s " Code ${response.status.code} : ${response.status.message}"
318
+ )
330
319
}
331
- }
332
320
333
- protected def handleDeleteResponse (response : RichJsResponse ): DeleteResponse = {
334
- val (statusCode, message) = statusCodeAndMessage(response)
335
-
336
- statusCode match {
321
+ protected def handleDeleteResponse (response : RichResponse ): DeleteResponse =
322
+ response.status.code match {
337
323
case 202 => DeleteResponse .Deleted
338
324
case 404 => DeleteResponse .NotFound
339
- case _ => throw new PineconeScalaClientException (s " Code ${statusCode} : ${message}" )
325
+ case _ =>
326
+ throw new PineconeScalaClientException (
327
+ s " Code ${response.status.code} : ${response.status.message}"
328
+ )
340
329
}
341
- }
342
330
}
343
331
344
332
object PineconeIndexServiceFactory extends PineconeServiceFactoryHelper {
0 commit comments