|
| 1 | +import scala.io.Source |
| 2 | +import scala.concurrent.duration.Duration |
| 3 | +import scala.concurrent.{Await, ExecutionContextExecutor} |
| 4 | + |
| 5 | +import algoliasearch.api.IngestionClient |
| 6 | +import algoliasearch.config.* |
| 7 | + |
| 8 | +import algoliasearch.config.* |
| 9 | +import algoliasearch.ingestion.* |
| 10 | + |
| 11 | +import org.json4s.native.JsonMethods |
| 12 | +import org.json4s.jvalue2extractable |
| 13 | + |
| 14 | +object PushSetup { |
| 15 | + def main(args: Array[String]): Unit = { |
| 16 | + implicit val ec: ExecutionContextExecutor = scala.concurrent.ExecutionContext.global |
| 17 | + implicit val formats: org.json4s.Formats = JsonSupport.format |
| 18 | + |
| 19 | + val result = Source.fromFile("records.json").getLines().mkString |
| 20 | + val records = JsonMethods.parse(result).extract[Seq[algoliasearch.ingestion.PushTaskRecords]] |
| 21 | + |
| 22 | + // use the region matching your applicationID |
| 23 | + val client = IngestionClient( |
| 24 | + appId = "ALGOLIA_APPLICATION_ID", |
| 25 | + apiKey = "ALGOLIA_API_KEY", |
| 26 | + region = "ALGOLIA_APPLICATION_REGION" |
| 27 | + ) |
| 28 | + |
| 29 | + try { |
| 30 | + // setting `watch` to `true` will make the call synchronous |
| 31 | + val resp = Await.result( |
| 32 | + client.pushTask( |
| 33 | + taskID = "YOUR_TASK_ID", |
| 34 | + pushTaskPayload = PushTaskPayload( |
| 35 | + action = Action.withName("addObject"), |
| 36 | + records = records |
| 37 | + ), |
| 38 | + watch = Some(true) |
| 39 | + ), |
| 40 | + Duration(100, "sec") |
| 41 | + ) |
| 42 | + |
| 43 | + println(resp) |
| 44 | + } catch { |
| 45 | + case e: Exception => println(e) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments