Skip to content

Commit

Permalink
feat(query,indexsettings): Fix index settings & query types
Browse files Browse the repository at this point in the history
Closes #369
  • Loading branch information
ElPicador committed Mar 28, 2017
1 parent 7762ac7 commit 43c8b53
Show file tree
Hide file tree
Showing 52 changed files with 529 additions and 698 deletions.
1 change: 0 additions & 1 deletion .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
maxColumn = 100
bestEffortInDeeplyNestedCode = true
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jdk:
- oraclejdk8

before_script:
- "sbt ++$TRAVIS_SCALA_VERSION scalafmtTest test:scalafmtTest"
- "sbt ++$TRAVIS_SCALA_VERSION scalafmtTest"

script: "sbt ++$TRAVIS_SCALA_VERSION -J-Dsun.net.maxDatagramSockets=2048 clean coverage test"
after_success: "sbt ++$TRAVIS_SCALA_VERSION coverageReport coveralls"
7 changes: 2 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ lazy val myProject = project
.in(file("."))
.enablePlugins(AutomateHeaderPlugin, BuildInfoPlugin)
.settings(
buildInfoKeys := Seq[BuildInfoKey](name,
version,
scalaVersion,
sbtVersion),
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
buildInfoPackage := "algolia"
)

Expand Down Expand Up @@ -133,6 +130,6 @@ publishTo := {
}
}

addCommandAlias("formatAll", ";scalafmt;test:scalafmt")
addCommandAlias("formatAll", ";scalafmt")

addCommandAlias("checkAll", ";compile;test:compile;formatAll")
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.5.0")
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.8.2")

//Formatter
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.4.8")
addSbtPlugin("com.geirsson" % "sbt-scalafmt" % "0.6.3")
75 changes: 34 additions & 41 deletions src/main/scala/algolia/AlgoliaClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ import scala.util.{Failure, Success}
class AlgoliaClient(applicationId: String,
apiKey: String,
customHeader: Map[String, String] = Map.empty,
configuration: AlgoliaClientConfiguration =
AlgoliaClientConfiguration.default,
configuration: AlgoliaClientConfiguration = AlgoliaClientConfiguration.default,
private[algolia] val utils: AlgoliaUtils = AlgoliaUtils) {

if (applicationId == null || applicationId.isEmpty) {
throw new AlgoliaClientException(
s"'applicationId' is probably too short: '$applicationId'")
throw new AlgoliaClientException(s"'applicationId' is probably too short: '$applicationId'")
}

if (apiKey == null || apiKey.isEmpty) {
throw new AlgoliaClientException(
s"'apiKey' is probably too short: '$apiKey'")
throw new AlgoliaClientException(s"'apiKey' is probably too short: '$apiKey'")
}

final private val ALGOLIANET_COM_HOST = "algolianet.com"
Expand All @@ -69,51 +66,48 @@ class AlgoliaClient(applicationId: String,
val indexingHosts: Seq[String] =
s"https://$applicationId.$ALGOLIANET_HOST" +:
utils.shuffle(
Seq(
s"https://$applicationId-1.$ALGOLIANET_COM_HOST",
s"https://$applicationId-2.$ALGOLIANET_COM_HOST",
s"https://$applicationId-3.$ALGOLIANET_COM_HOST"
))
Seq(
s"https://$applicationId-1.$ALGOLIANET_COM_HOST",
s"https://$applicationId-2.$ALGOLIANET_COM_HOST",
s"https://$applicationId-3.$ALGOLIANET_COM_HOST"
))

val queryHosts: Seq[String] =
s"https://$applicationId-dsn.$ALGOLIANET_HOST" +:
utils.shuffle(
Seq(
s"https://$applicationId-1.$ALGOLIANET_COM_HOST",
s"https://$applicationId-2.$ALGOLIANET_COM_HOST",
s"https://$applicationId-3.$ALGOLIANET_COM_HOST"
))
Seq(
s"https://$applicationId-1.$ALGOLIANET_COM_HOST",
s"https://$applicationId-2.$ALGOLIANET_COM_HOST",
s"https://$applicationId-3.$ALGOLIANET_COM_HOST"
))

val userAgent =
s"Algolia for Scala (${BuildInfo.version}); JVM (${System.getProperty(
"java.version")}); Scala (${BuildInfo.scalaVersion})"
s"Algolia for Scala (${BuildInfo.version}); JVM (${System.getProperty("java.version")}); Scala (${BuildInfo.scalaVersion})"

val headers: Map[String, String] = customHeader ++ Map(
"Accept-Encoding" -> "gzip",
"X-Algolia-Application-Id" -> applicationId,
"X-Algolia-API-Key" -> apiKey,
"User-Agent" -> userAgent,
"Content-Type" -> "application/json; charset=UTF-8",
"Accept" -> "application/json"
)
"Accept-Encoding" -> "gzip",
"X-Algolia-Application-Id" -> applicationId,
"X-Algolia-API-Key" -> apiKey,
"User-Agent" -> userAgent,
"Content-Type" -> "application/json; charset=UTF-8",
"Accept" -> "application/json"
)

private val HMAC_SHA256 = "HmacSHA256"
private[algolia] val hostsStatuses =
HostsStatuses(configuration, utils, queryHosts, indexingHosts)

def execute[QUERY, RESULT](query: QUERY)(
implicit executable: Executable[QUERY, RESULT],
executor: ExecutionContext): Future[RESULT] = executable(this, query)
def execute[QUERY, RESULT](query: QUERY)(implicit executable: Executable[QUERY, RESULT],
executor: ExecutionContext): Future[RESULT] =
executable(this, query)

def generateSecuredApiKey(privateApiKey: String,
query: Query,
userToken: Option[String] = None): String = {
val queryStr = query.copy(userToken = userToken).toParam
val key = hmac(privateApiKey, queryStr)

new String(
Base64.getEncoder.encode(
s"$key$queryStr".getBytes(Charset.forName("UTF8"))))
new String(Base64.getEncoder.encode(s"$key$queryStr".getBytes(Charset.forName("UTF8"))))
}

private def hmac(key: String, msg: String): String = {
Expand All @@ -131,16 +125,15 @@ class AlgoliaClient(applicationId: String,
hostsStatuses.indexingHostsThatAreUp()
}

val result = hosts.foldLeft(Future.failed[T](new TimeoutException())) {
(future, host) =>
future.recoverWith {
case e: APIClientException => Future.failed(e) //No retry if 4XX
case _ =>
httpClient.request[T](host, headers, payload).andThen {
case Failure(_) => hostsStatuses.markHostAsDown(host)
case Success(_) => hostsStatuses.markHostAsUp(host)
}
}
val result = hosts.foldLeft(Future.failed[T](new TimeoutException())) { (future, host) =>
future.recoverWith {
case e: APIClientException => Future.failed(e) //No retry if 4XX
case _ =>
httpClient.request[T](host, headers, payload).andThen {
case Failure(_) => hostsStatuses.markHostAsDown(host)
case Success(_) => hostsStatuses.markHostAsUp(host)
}
}
}

result.recoverWith {
Expand Down
64 changes: 32 additions & 32 deletions src/main/scala/algolia/AlgoliaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ package algolia

import algolia.definitions._
import algolia.objects._
import org.json4s.CustomSerializer
import org.json4s.{CustomSerializer, Formats}
import org.json4s.JsonAST._
import org.json4s.JsonDSL._

import scala.util.matching.Regex

trait AlgoliaDsl
extends Object //Just to have all trait DSL ordered `with`
with ApiKeyDefinitionDsl
Expand All @@ -54,7 +56,7 @@ trait AlgoliaDsl

object AlgoliaDsl extends AlgoliaDsl {

implicit val formats =
implicit val formats: Formats =
org.json4s.DefaultFormats +
new SearchableAttributesSerializer +
new AttributesToIndexSerializer +
Expand All @@ -70,11 +72,13 @@ object AlgoliaDsl extends AlgoliaDsl {
new RemoveStopWordsSerializer +
new IgnorePluralsSerializer

val searchableAttributesUnordered = """^unordered\(([\w-]+)\)$""".r
val searchableAttributesAttributes = """^([\w-]+,[\w-]+[,[\w-]+]*)$""".r
val numericAttributesToIndexEqualOnly = """^equalOnly\(([\w-]+)\)$""".r
val asc = """^asc\(([\w-]+)\)$""".r
val desc = """^desc\(([\w-]+)\)$""".r
val searchableAttributesUnordered: Regex = """^unordered\(([\w-]+)\)$""".r
val searchableAttributesAttributes: Regex =
"""^([\w-]+,[\w-]+[,[\w-]+]*)$""".r
val numericAttributesToIndexEqualOnly: Regex =
"""^equalOnly\(([\w-]+)\)$""".r
val asc: Regex = """^asc\(([\w-]+)\)$""".r
val desc: Regex = """^desc\(([\w-]+)\)$""".r

sealed trait ForwardToReplicas

Expand All @@ -85,7 +89,7 @@ object AlgoliaDsl extends AlgoliaDsl {
sealed trait In

class AttributesToIndexSerializer
extends CustomSerializer[AttributesToIndex](format =>
extends CustomSerializer[AttributesToIndex](_ =>
({
case JString(searchableAttributesUnordered(attr)) =>
AttributesToIndex.unordered(attr)
Expand All @@ -101,7 +105,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class SearchableAttributesSerializer
extends CustomSerializer[SearchableAttributes](format =>
extends CustomSerializer[SearchableAttributes](_ =>
({
case JString(searchableAttributesUnordered(attr)) =>
SearchableAttributes.unordered(attr)
Expand All @@ -117,7 +121,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class NumericAttributesToIndexSerializer
extends CustomSerializer[NumericAttributesToIndex](format =>
extends CustomSerializer[NumericAttributesToIndex](_ =>
({
case JString(numericAttributesToIndexEqualOnly(attr)) =>
NumericAttributesToIndex.equalOnly(attr)
Expand All @@ -127,7 +131,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class RankingSerializer
extends CustomSerializer[Ranking](format =>
extends CustomSerializer[Ranking](_ =>
({
case JString(asc(attr)) => Ranking.asc(attr)
case JString(desc(attr)) => Ranking.desc(attr)
Expand All @@ -151,7 +155,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class CustomRankingSerializer
extends CustomSerializer[CustomRanking](format =>
extends CustomSerializer[CustomRanking](_ =>
({
case JString(asc(attr)) => CustomRanking.asc(attr)
case JString(desc(attr)) => CustomRanking.desc(attr)
Expand All @@ -161,7 +165,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class QueryTypeSerializer
extends CustomSerializer[QueryType](format =>
extends CustomSerializer[QueryType](_ =>
({
case JString("prefixAll") => QueryType.prefixAll
case JString("prefixLast") => QueryType.prefixLast
Expand All @@ -173,7 +177,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class TypoToleranceSerializer
extends CustomSerializer[TypoTolerance](format =>
extends CustomSerializer[TypoTolerance](_ =>
({
case JString("true") => TypoTolerance.`true`
case JString("false") => TypoTolerance.`false`
Expand All @@ -187,7 +191,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class AclSerializer
extends CustomSerializer[Acl](format =>
extends CustomSerializer[Acl](_ =>
({
case JString("search") => Acl.search
case JString("browse") => Acl.browse
Expand All @@ -211,42 +215,38 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class QuerySynonymsSerializer
extends CustomSerializer[QuerySynonyms](format =>
extends CustomSerializer[QuerySynonyms](_ =>
({
case JObject(_) => ???
}, {
case QuerySynonyms(query, synonymsTypes, page, hitsPerPage) =>
var fields = Seq(JField("query", JString(query)))

synonymsTypes.foreach(t =>
fields :+= JField("type", JString(t.mkString(","))))
synonymsTypes.foreach(t => fields :+= JField("type", JString(t.mkString(","))))
page.foreach(p => fields :+= JField("page", JInt(p)))
hitsPerPage.foreach(p => fields :+= JField("hitsPerPage", JInt(p)))

JObject(fields: _*)
}))

class AbstractSynonymSerializer
extends CustomSerializer[AbstractSynonym](format =>
extends CustomSerializer[AbstractSynonym](_ =>
({
case x: JObject =>
val objectID = (x \ "objectID").extract[String]
val synonymType = (x \ "type").extract[String]

synonymType match {
case SynonymType.synonym.name =>
Synonym.Synonym(objectID,
(x \ "synonyms").extract[Seq[String]])
Synonym.Synonym(objectID, (x \ "synonyms").extract[Seq[String]])
case SynonymType.altCorrection1.name =>
Synonym.AltCorrection1(
objectID,
(x \ "word").extract[String],
(x \ "corrections").extract[Seq[String]])
Synonym.AltCorrection1(objectID,
(x \ "word").extract[String],
(x \ "corrections").extract[Seq[String]])
case SynonymType.altCorrection2.name =>
Synonym.AltCorrection2(
objectID,
(x \ "word").extract[String],
(x \ "corrections").extract[Seq[String]])
Synonym.AltCorrection2(objectID,
(x \ "word").extract[String],
(x \ "corrections").extract[Seq[String]])
case SynonymType.oneWaySynonym.name =>
Synonym.OneWaySynonym(objectID,
(x \ "input").extract[String],
Expand All @@ -270,7 +270,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class DistinctSerializer
extends CustomSerializer[Distinct](format =>
extends CustomSerializer[Distinct](_ =>
({
case JBool(true) => Distinct.`true`
case JBool(false) => Distinct.`false`
Expand All @@ -282,7 +282,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class RemoveStopWordsSerializer
extends CustomSerializer[RemoveStopWords](format =>
extends CustomSerializer[RemoveStopWords](_ =>
({
case JBool(true) => RemoveStopWords.`true`
case JBool(false) => RemoveStopWords.`false`
Expand All @@ -294,7 +294,7 @@ object AlgoliaDsl extends AlgoliaDsl {
}))

class IgnorePluralsSerializer
extends CustomSerializer[IgnorePlurals](format =>
extends CustomSerializer[IgnorePlurals](_ =>
({
case JBool(true) => IgnorePlurals.`true`
case JBool(false) => IgnorePlurals.`false`
Expand Down

0 comments on commit 43c8b53

Please sign in to comment.