-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added: Add get/set personalizationStrategy for Personalization API
- Loading branch information
Showing
8 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/scala/algolia/definitions/StrategyDefinition.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package algolia.definitions | ||
|
||
import algolia.http.{GET, HttpPayload, POST} | ||
import algolia.objects.Strategy | ||
import algolia.objects.RequestOptions | ||
import org.json4s.Formats | ||
import org.json4s.native.Serialization.write | ||
|
||
case class GetPersonalizationStrategyDefinition(requestOptions: Option[RequestOptions] = None)( | ||
implicit val formats: Formats) | ||
extends Definition { | ||
|
||
override type T = GetPersonalizationStrategyDefinition | ||
|
||
override def options(requestOptions: RequestOptions): GetPersonalizationStrategyDefinition = | ||
copy(requestOptions = Some(requestOptions)) | ||
|
||
override private[algolia] def build(): HttpPayload = { | ||
HttpPayload( | ||
GET, | ||
Seq("1", "recommendation", "personalization", "strategy"), | ||
isSearch = true, | ||
requestOptions = requestOptions | ||
) | ||
} | ||
|
||
} | ||
|
||
case class SetPersonalizationStrategyDefinition( | ||
s: Strategy, | ||
requestOptions: Option[RequestOptions] = None)(implicit val formats: Formats) | ||
extends Definition { | ||
|
||
override type T = SetPersonalizationStrategyDefinition | ||
|
||
override def options(requestOptions: RequestOptions): SetPersonalizationStrategyDefinition = | ||
copy(requestOptions = Some(requestOptions)) | ||
|
||
override private[algolia] def build(): HttpPayload = { | ||
HttpPayload( | ||
POST, | ||
Seq("1", "recommendation", "personalization", "strategy"), | ||
body = Some(write(s)), | ||
isSearch = false, | ||
requestOptions = requestOptions | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package algolia.dsl | ||
|
||
import algolia.{AlgoliaClient, Executable} | ||
import algolia.definitions.{ | ||
GetPersonalizationStrategyDefinition, | ||
SetPersonalizationStrategyDefinition | ||
} | ||
import algolia.inputs._ | ||
import algolia.objects.Strategy | ||
import algolia.responses.SetStrategyResult | ||
import org.json4s.Formats | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
trait SetDsl { | ||
implicit val formats: Formats | ||
|
||
case object set { | ||
|
||
def personalizationStrategy(s: Strategy): SetPersonalizationStrategyDefinition = | ||
SetPersonalizationStrategyDefinition(s) | ||
|
||
} | ||
|
||
implicit object SetPersonalizationStrategyExecutable | ||
extends Executable[SetPersonalizationStrategyDefinition, SetStrategyResult] { | ||
override def apply(client: AlgoliaClient, query: SetPersonalizationStrategyDefinition)( | ||
implicit executor: ExecutionContext): Future[SetStrategyResult] = { | ||
client.request[SetStrategyResult](query.build()) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package algolia.objects | ||
|
||
case class Strategy(eventsScoring: Option[Map[String, ScoreType]] = None, | ||
facetsScoring: Option[Map[String, Score]] = None) | ||
|
||
case class ScoreType(score: Int, `type`: String) | ||
|
||
case class Score(score: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package algolia.responses | ||
|
||
case class SetStrategyResult(updatedAt: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package algolia.dsl | ||
|
||
import algolia.AlgoliaDsl._ | ||
import algolia.AlgoliaTest | ||
import algolia.http.{GET, HttpPayload, POST} | ||
import algolia.objects.{Score, ScoreType, Strategy} | ||
|
||
import scala.language.postfixOps | ||
|
||
class StrategyTest extends AlgoliaTest { | ||
|
||
describe("personalization strategy payloads") { | ||
|
||
it("should produce empty set strategy payload") { | ||
(set personalizationStrategy Strategy()).build() should be( | ||
HttpPayload( | ||
POST, | ||
Seq("1", "recommendation", "personalization", "strategy"), | ||
body = Some("{}"), | ||
isSearch = false, | ||
requestOptions = None | ||
) | ||
) | ||
} | ||
|
||
it("should produce non-empty set strategy payload") { | ||
val strategy = Strategy( | ||
eventsScoring = Some( | ||
Map( | ||
"event 1" -> ScoreType(10, "click"), | ||
"event 2" -> ScoreType(20, "conversion") | ||
)), | ||
facetsScoring = Some( | ||
Map( | ||
"event 3" -> Score(30) | ||
)) | ||
) | ||
|
||
(set personalizationStrategy strategy).build() should be( | ||
HttpPayload( | ||
POST, | ||
Seq("1", "recommendation", "personalization", "strategy"), | ||
body = Some( | ||
"""{"eventsScoring":{"event 1":{"score":10,"type":"click"},"event 2":{"score":20,"type":"conversion"}},"facetsScoring":{"event 3":{"score":30}}}"""), | ||
isSearch = false, | ||
requestOptions = None | ||
) | ||
) | ||
} | ||
|
||
it("should produce get strategy payload") { | ||
(get personalizationStrategy).build() should be( | ||
HttpPayload( | ||
GET, | ||
Seq("1", "recommendation", "personalization", "strategy"), | ||
body = None, | ||
isSearch = true, | ||
requestOptions = None | ||
) | ||
) | ||
} | ||
|
||
} | ||
} |