### Algolia Scala client usage example Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/README.md Imports the Algolia SearchClient, creates a client with your app ID and API key, saves an object, waits for indexing, and performs a search with typo tolerance. Replace placeholders like YOUR_APP_ID and YOUR_INDEX_NAME. ```scala import algoliasearch.api.SearchClient val client = SearchClient(appId = "YOUR_APP_ID", apiKey = "YOUR_API_KEY") // Add a new record to your Algolia index val response = client.saveObject( indexName = "", body = JObject(List(JField("objectID", JString("id")), JField("test", JString("val")))) ) // Use the response val value = Await.result(response, Duration(100, "sec")) // Poll the task status to know when it has been indexed client.waitTask("", response.getTaskID()) // Fetch search results, with typo tolerance val response = client.search( searchMethodParams = SearchMethodParams( requests = Seq( SearchForHits( indexName = "", query = Some(""), hitsPerPage = Some(50) ) ) ) ) // Use the response val value = Await.result(response, Duration(100, "sec")) ``` -------------------------------- ### Build and Test Commands Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Commands to build, test, and format the Scala client. Run from the repo root or the client directory as indicated. ```bash # From repo root (api-clients-automation) yarn cli build clients scala # Build Scala client yarn cli cts generate scala # Generate CTS tests yarn cli cts run scala # Run CTS tests yarn cli playground scala search # Interactive playground yarn cli format scala clients/algoliasearch-client-scala # From client directory cd clients/algoliasearch-client-scala sbt compile # Compile sbt test # Run tests sbt scalafmtAll # Format code ``` -------------------------------- ### ClientConfig case class Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Configuration case class with appId, apiKey, hosts, and timeouts. Defaults for hosts and timeouts are provided. ```scala // config/ case class ClientConfig( appId: String, apiKey: String, hosts: Seq[Host] = Seq.empty, readTimeout: Duration = 5.seconds, writeTimeout: Duration = 30.seconds ) ``` -------------------------------- ### Implicit Conversions with Extension Classes Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Define implicit classes to add extension methods to existing types. Import the extension package to use them, as shown with the firstHit method. ```scala // Extensions use implicits implicit class SearchResponseOps(response: SearchResponse) { def firstHit: Option[Hit] = response.hits.flatMap(_.headOption) } // Import to use import algoliasearch.extension._ response.firstHit ``` -------------------------------- ### Check Git Remote Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Use this command to verify the current repository's remote origin. The correct remote should be 'api-clients-automation', not the public 'algoliasearch-client-scala' repository. ```bash git remote -v ``` -------------------------------- ### Future-based async with map/flatMap and for-comprehension Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Shows how to chain Future-returning API methods using map/flatMap or for-comprehension. Use for processing search results asynchronously. ```scala // All API methods return Future def search(params: SearchParams): Future[SearchResponse] // Chain with map/flatMap client.search(params) .map(_.hits) .flatMap(processHits) // Or use for-comprehension for { response <- client.search(params) processed <- processHits(response.hits) } yield processed ``` -------------------------------- ### Option Handling with Pattern Matching and Combinators Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Use pattern matching or combinators like getOrElse, map, and flatMap to handle Option values safely. This avoids null checks and enables chaining. ```scala // Use pattern matching or combinators response.hits match { case Some(hits) => process(hits) case None => defaultValue } // Or use getOrElse val hits = response.hits.getOrElse(Seq.empty) // For chaining response.hits.map(_.head).flatMap(_.objectId) ``` -------------------------------- ### Generation Config Patterns Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md JavaScript configuration snippet showing file patterns in 'config/generation.config.mjs'. Patterns without '!' are generated and should not be edited; patterns with '!' are hand-written and safe to edit. ```javascript // In generation.config.mjs - patterns WITHOUT '!' are GENERATED (do not edit) 'clients/algoliasearch-client-scala/src/main/scala/algoliasearch/**', // Generated '!clients/algoliasearch-client-scala/src/main/scala/algoliasearch/ApiClient.scala', // Hand-written ✓ '!clients/algoliasearch-client-scala/src/main/scala/algoliasearch/exception/**', // Hand-written ✓ '!clients/algoliasearch-client-scala/src/main/scala/algoliasearch/internal/**', // Hand-written ✓ '!clients/algoliasearch-client-scala/src/main/scala/algoliasearch/config/**', // Hand-written ✓ '!clients/algoliasearch-client-scala/src/main/scala/algoliasearch/extension/**', // Hand-written ✓ ``` -------------------------------- ### Set JVM DNS cache TTL Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/README.md Sets the JVM DNS cache TTL to 60 seconds to avoid uneven load distribution and dead machines due to the JVM's infinite DNS cache. Place this before creating the Algolia client. ```scala java.security.Security.setProperty("networkaddress.cache.ttl", "60"); ``` -------------------------------- ### Maven Sonatype snapshots repository Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/README.md Adds the Sonatype snapshots repository to a Maven pom.xml to use snapshot versions of the Algolia Scala client. ```xml oss-sonatype oss-sonatype https://oss.sonatype.org/content/repositories/snapshots/ true ``` -------------------------------- ### sbt Sonatype snapshots repository Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/README.md Adds the Sonatype snapshots repository to an sbt build.sbt file to use snapshot versions of the Algolia Scala client. ```scala resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots" ``` -------------------------------- ### ApiClient and SearchApi trait Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Base client class with execute method and API trait mixed into clients. Use for defining API methods that return Future. ```scala // ApiClient.scala - base client class ApiClient(config: ClientConfig) { // HTTP execution with retry logic def execute[T](request: Request[T]): Future[T] } // API traits mixed into clients trait SearchApi { this: ApiClient => def search(params: SearchParams): Future[SearchResponse] } ``` -------------------------------- ### Future Execution Context Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Always have an implicit ExecutionContext in scope when working with Futures. You can import the global one or provide your own explicitly. ```scala // Always have implicit ExecutionContext in scope import scala.concurrent.ExecutionContext.Implicits.global // Or provide explicitly implicit val ec: ExecutionContext = myExecutionContext ``` -------------------------------- ### sbt dependency for algoliasearch-scala Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/README.md Adds the Algolia Scala client dependency to an sbt build.sbt file. The version range [2,) pulls the latest 2.x release. ```scala libraryDependencies += "com.algolia" %% "algoliasearch-scala" % "[2,)" ``` -------------------------------- ### Case Class Copies Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Use the copy method for immutable updates to case classes, preserving immutability and avoiding mutation. ```scala // Immutable updates with copy val updated = params.copy(hitsPerPage = Some(20)) ``` -------------------------------- ### Maven dependency for algoliasearch-scala_2.13 Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/README.md Adds the Algolia Scala client dependency to a Maven pom.xml. The version range [2,) pulls the latest 2.x release. ```xml com.algolia algoliasearch-scala_2.13 [2,) ``` -------------------------------- ### Type Inference in Public APIs vs Local Vals Source: https://github.com/algolia/algoliasearch-client-scala/blob/main/AGENTS.md Be explicit with types in public API signatures, but you can rely on type inference for local values. This improves readability and maintainability. ```scala // Scala infers types, but be explicit in public APIs def search(params: SearchParams): Future[SearchResponse] // Explicit // Local vals can use inference val response = client.search(params) // Type inferred ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.