@@ -2,35 +2,40 @@ package io.cequence.pineconescala.service
22
33import akka .stream .Materializer
44import com .typesafe .config .{Config , ConfigFactory }
5+ import io .cequence .pineconescala .ConfigImplicits .ConfigExt
56import play .api .libs .ws .StandaloneWSRequest
67import io .cequence .pineconescala .JsonUtil .JsonOps
78import io .cequence .pineconescala .JsonFormats ._
89import io .cequence .pineconescala .PineconeScalaClientException
910import io .cequence .pineconescala .domain .settings ._
1011import io .cequence .pineconescala .domain .response ._
11- import io .cequence .pineconescala .ConfigImplicits ._
12- import io .cequence .pineconescala .domain .{PVector , PodType }
12+ import io .cequence .pineconescala .domain .PodType
1313import io .cequence .pineconescala .service .ws .{Timeouts , WSRequestHelper }
14+ import play .api .libs .json .JsObject
1415
1516import scala .concurrent .{ExecutionContext , Future }
1617
1718/**
1819 * Private impl. class of [[PineconeIndexService ]].
1920 *
2021 * @param apiKey
21- * @param environment
22+ * @param environment (optional)
2223 * @since Apr 2023
2324 */
2425private class PineconeIndexServiceImpl (
2526 apiKey : String ,
26- environment : String ,
27+ environment : Option [ String ] = None ,
2728 explTimeouts : Option [Timeouts ] = None )(
2829 implicit val ec : ExecutionContext , val materializer : Materializer
2930) extends PineconeIndexService with WSRequestHelper {
3031
3132 override protected type PEP = EndPoint
3233 override protected type PT = Tag
33- override protected val coreUrl = s " https://controller. ${environment}.pinecone.io/ "
34+ override protected val coreUrl = environment.map(env =>
35+ s " https://controller. $env.pinecone.io/ "
36+ ).getOrElse(
37+ " https://api.pinecone.io/"
38+ )
3439
3540 override protected def timeouts : Timeouts =
3641 explTimeouts.getOrElse(
@@ -97,17 +102,21 @@ private class PineconeIndexServiceImpl(
97102 }
98103
99104 override def listIndexes : Future [Seq [String ]] =
100- execGET(EndPoint .databases).map(
101- _.asSafe[Seq [String ]]
105+ execGET(indexesEndpoint).map(response =>
106+ (response \ " indexes" ).toOption.map(
107+ _.asSafe[Seq [JsObject ]].map(_.toString()) // TODO
108+ ).getOrElse(
109+ response.asSafe[Seq [String ]]
110+ )
102111 )
103112
104113 override def createIndex (
105114 name : String ,
106115 dimension : Int ,
107- settings : CreateIndexSettings
116+ settings : CreatePodBasedIndexSettings // TODO
108117 ): Future [CreateResponse ] =
109118 execPOSTWithStatus(
110- EndPoint .databases ,
119+ indexesEndpoint ,
111120 bodyParams = jsonBodyParams(
112121 Tag .name -> Some (name),
113122 Tag .dimension -> Some (dimension),
@@ -134,7 +143,7 @@ private class PineconeIndexServiceImpl(
134143 indexName : String
135144 ): Future [Option [IndexInfo ]] =
136145 execGETWithStatus(
137- EndPoint .databases ,
146+ indexesEndpoint ,
138147 endPointParam = Some (indexName)
139148 ).map { response =>
140149 handleNotFoundAndError(response).map(
@@ -146,7 +155,7 @@ private class PineconeIndexServiceImpl(
146155 indexName : String
147156 ): Future [DeleteResponse ] =
148157 execDELETEWithStatus(
149- EndPoint .databases ,
158+ indexesEndpoint ,
150159 endPointParam = Some (indexName),
151160 acceptableStatusCodes = Nil // don't parse response at all
152161 ).map { response =>
@@ -165,7 +174,7 @@ private class PineconeIndexServiceImpl(
165174 podType : Option [PodType .Value ]
166175 ): Future [ConfigureIndexResponse ] =
167176 execPATCHWithStatus(
168- EndPoint .databases ,
177+ indexesEndpoint ,
169178 endPointParam = Some (indexName),
170179 bodyParams = jsonBodyParams(
171180 Tag .replicas -> replicas,
@@ -184,6 +193,11 @@ private class PineconeIndexServiceImpl(
184193
185194 // aux
186195
196+ // if environment is specified (pod-based arch) we use databases endpoint,
197+ // otherwise (serverless arch) we use indexes endpoint
198+ private def indexesEndpoint =
199+ environment.map(_ => EndPoint .databases).getOrElse(EndPoint .indexes)
200+
187201 override protected def getWSRequestOptional (
188202 endPoint : Option [PEP ],
189203 endPointParam : Option [String ],
@@ -219,7 +233,7 @@ object PineconeIndexServiceFactory extends PineconeServiceFactoryHelper {
219233
220234 def apply (
221235 apiKey : String ,
222- environment : String ,
236+ environment : Option [ String ] = None ,
223237 timeouts : Option [Timeouts ] = None )(
224238 implicit ec : ExecutionContext , materializer : Materializer
225239 ): PineconeIndexService =
@@ -238,7 +252,7 @@ object PineconeIndexServiceFactory extends PineconeServiceFactoryHelper {
238252
239253 apply(
240254 apiKey = config.getString(s " $configPrefix.apiKey " ),
241- environment = config.getString (s " $configPrefix.environment " ),
255+ environment = config.optionalString (s " $configPrefix.environment " ),
242256 timeouts = timeoutsToOption(timeouts)
243257 )
244258 }
0 commit comments