@@ -2,35 +2,40 @@ package io.cequence.pineconescala.service
2
2
3
3
import akka .stream .Materializer
4
4
import com .typesafe .config .{Config , ConfigFactory }
5
+ import io .cequence .pineconescala .ConfigImplicits .ConfigExt
5
6
import play .api .libs .ws .StandaloneWSRequest
6
7
import io .cequence .pineconescala .JsonUtil .JsonOps
7
8
import io .cequence .pineconescala .JsonFormats ._
8
9
import io .cequence .pineconescala .PineconeScalaClientException
9
10
import io .cequence .pineconescala .domain .settings ._
10
11
import 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
13
13
import io .cequence .pineconescala .service .ws .{Timeouts , WSRequestHelper }
14
+ import play .api .libs .json .JsObject
14
15
15
16
import scala .concurrent .{ExecutionContext , Future }
16
17
17
18
/**
18
19
* Private impl. class of [[PineconeIndexService ]].
19
20
*
20
21
* @param apiKey
21
- * @param environment
22
+ * @param environment (optional)
22
23
* @since Apr 2023
23
24
*/
24
25
private class PineconeIndexServiceImpl (
25
26
apiKey : String ,
26
- environment : String ,
27
+ environment : Option [ String ] = None ,
27
28
explTimeouts : Option [Timeouts ] = None )(
28
29
implicit val ec : ExecutionContext , val materializer : Materializer
29
30
) extends PineconeIndexService with WSRequestHelper {
30
31
31
32
override protected type PEP = EndPoint
32
33
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
+ )
34
39
35
40
override protected def timeouts : Timeouts =
36
41
explTimeouts.getOrElse(
@@ -97,17 +102,21 @@ private class PineconeIndexServiceImpl(
97
102
}
98
103
99
104
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
+ )
102
111
)
103
112
104
113
override def createIndex (
105
114
name : String ,
106
115
dimension : Int ,
107
- settings : CreateIndexSettings
116
+ settings : CreatePodBasedIndexSettings // TODO
108
117
): Future [CreateResponse ] =
109
118
execPOSTWithStatus(
110
- EndPoint .databases ,
119
+ indexesEndpoint ,
111
120
bodyParams = jsonBodyParams(
112
121
Tag .name -> Some (name),
113
122
Tag .dimension -> Some (dimension),
@@ -134,7 +143,7 @@ private class PineconeIndexServiceImpl(
134
143
indexName : String
135
144
): Future [Option [IndexInfo ]] =
136
145
execGETWithStatus(
137
- EndPoint .databases ,
146
+ indexesEndpoint ,
138
147
endPointParam = Some (indexName)
139
148
).map { response =>
140
149
handleNotFoundAndError(response).map(
@@ -146,7 +155,7 @@ private class PineconeIndexServiceImpl(
146
155
indexName : String
147
156
): Future [DeleteResponse ] =
148
157
execDELETEWithStatus(
149
- EndPoint .databases ,
158
+ indexesEndpoint ,
150
159
endPointParam = Some (indexName),
151
160
acceptableStatusCodes = Nil // don't parse response at all
152
161
).map { response =>
@@ -165,7 +174,7 @@ private class PineconeIndexServiceImpl(
165
174
podType : Option [PodType .Value ]
166
175
): Future [ConfigureIndexResponse ] =
167
176
execPATCHWithStatus(
168
- EndPoint .databases ,
177
+ indexesEndpoint ,
169
178
endPointParam = Some (indexName),
170
179
bodyParams = jsonBodyParams(
171
180
Tag .replicas -> replicas,
@@ -184,6 +193,11 @@ private class PineconeIndexServiceImpl(
184
193
185
194
// aux
186
195
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
+
187
201
override protected def getWSRequestOptional (
188
202
endPoint : Option [PEP ],
189
203
endPointParam : Option [String ],
@@ -219,7 +233,7 @@ object PineconeIndexServiceFactory extends PineconeServiceFactoryHelper {
219
233
220
234
def apply (
221
235
apiKey : String ,
222
- environment : String ,
236
+ environment : Option [ String ] = None ,
223
237
timeouts : Option [Timeouts ] = None )(
224
238
implicit ec : ExecutionContext , materializer : Materializer
225
239
): PineconeIndexService =
@@ -238,7 +252,7 @@ object PineconeIndexServiceFactory extends PineconeServiceFactoryHelper {
238
252
239
253
apply(
240
254
apiKey = config.getString(s " $configPrefix.apiKey " ),
241
- environment = config.getString (s " $configPrefix.environment " ),
255
+ environment = config.optionalString (s " $configPrefix.environment " ),
242
256
timeouts = timeoutsToOption(timeouts)
243
257
)
244
258
}
0 commit comments