Skip to content

feat: custom request #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/scala/algolia/AlgoliaDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ trait AlgoliaDsl
with RestoreDsl
with DictionaryDsl
with ReplaceDsl
with CustomDsl

object AlgoliaDsl extends AlgoliaDsl {

Expand Down
58 changes: 58 additions & 0 deletions src/main/scala/algolia/definitions/CustomRequestDefinition.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Algolia
* http://www.algolia.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package algolia.definitions

import algolia.http.HttpPayload
import algolia.objects.{CustomRequest, RequestEndpoint, RequestOptions}
import org.json4s.Formats

case class CustomRequestDefinition(
customRequest: CustomRequest
)(implicit val formats: Formats)
extends Definition {

type T = CustomRequestDefinition

override def options(
requestOptions: RequestOptions
): CustomRequestDefinition =
this

override private[algolia] def build(): HttpPayload = {
import customRequest._
HttpPayload(
verb = verb,
path = path,
queryParameters = queryParameters,
body = body,
isSearch = RequestEndpoint.Search == endpoint,
isAnalytics = RequestEndpoint.Analytics == endpoint,
isInsights = RequestEndpoint.Insights == endpoint,
isPersonalization = RequestEndpoint.Personalization == endpoint,
requestOptions = None
)
}
}
51 changes: 51 additions & 0 deletions src/main/scala/algolia/dsl/CustomDsl.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Algolia
* http://www.algolia.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package algolia.dsl

import algolia.definitions.CustomRequestDefinition
import algolia.objects.CustomRequest
import algolia.{AlgoliaClient, Executable}
import org.json4s.{Formats, JObject}

import scala.concurrent.{ExecutionContext, Future}

trait CustomDsl {

implicit val formats: Formats

case object custom {

def request(request: CustomRequest): CustomRequestDefinition =
CustomRequestDefinition(request)
}

implicit object CustomRequestDefinitionExecutable
extends Executable[CustomRequestDefinition, JObject] {
override def apply(client: AlgoliaClient, query: CustomRequestDefinition)(
implicit executor: ExecutionContext
): Future[JObject] = { client.request[JObject](query.build()) }
}
}
10 changes: 5 additions & 5 deletions src/main/scala/algolia/http/HttpPayload.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@ import org.asynchttpclient.{Request, RequestBuilder}

import java.net.InetAddress

private[algolia] sealed trait HttpVerb
sealed trait HttpVerb

private[algolia] case object GET extends HttpVerb {
case object GET extends HttpVerb {
override def toString: String = "GET"
}

private[algolia] case object POST extends HttpVerb {
case object POST extends HttpVerb {
override def toString: String = "POST"
}

private[algolia] case object PUT extends HttpVerb {
case object PUT extends HttpVerb {
override def toString: String = "PUT"
}

private[algolia] case object DELETE extends HttpVerb {
case object DELETE extends HttpVerb {
override def toString: String = "DELETE"
}

Expand Down
47 changes: 47 additions & 0 deletions src/main/scala/algolia/objects/CustomRequest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Algolia
* http://www.algolia.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package algolia.objects

import algolia.http.HttpVerb

case class CustomRequest(
verb: HttpVerb,
path: Seq[String],
endpoint: RequestEndpoint,
queryParameters: Option[Map[String, String]] = None,
body: Option[String] = None,
requestOptions: Option[RequestOptions] = None
)

sealed trait RequestEndpoint

object RequestEndpoint {
object Search extends RequestEndpoint
object Indexing extends RequestEndpoint
object Analytics extends RequestEndpoint
object Insights extends RequestEndpoint
object Personalization extends RequestEndpoint
}
132 changes: 132 additions & 0 deletions src/test/scala/algolia/dsl/CustomRequestTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Algolia
* http://www.algolia.com/
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package algolia.dsl

import algolia.AlgoliaDsl.custom
import algolia.AlgoliaTest
import algolia.http.{HttpPayload, POST}
import algolia.objects.{CustomRequest, RequestEndpoint}

class CustomRequestTest extends AlgoliaTest {
describe("customRequest") {
it("custom request ") {
custom request CustomRequest(
verb = POST,
path = Seq("1", "indexes", "indexName", "query"),
endpoint = RequestEndpoint.Search,
body = Some("""{"query": "van"}""")
)
}
it("should call search endpoint") {
val json = """{"query":"van","hitsPerPage":1}"""
(custom request CustomRequest(
verb = POST,
path = Seq("1", "indexes", "indexName", "query"),
endpoint = RequestEndpoint.Search,
body = Some(json)
)).build() should be(
HttpPayload(
POST,
Seq("1", "indexes", "indexName", "query"),
body = Some(json),
isSearch = true,
requestOptions = None
)
)
}

it("should call indexing endpoint") {
(custom request CustomRequest(
verb = POST,
path = Seq("1", "indexes", "indexName", "clear"),
endpoint = RequestEndpoint.Indexing
)).build() should be(
HttpPayload(
POST,
Seq("1", "indexes", "indexName", "clear"),
isSearch = false,
requestOptions = None
)
)
}

it("should call analytics endpoint") {
(custom request CustomRequest(
verb = POST,
path = Seq("2", "abtests", "42", "stop"),
endpoint = RequestEndpoint.Analytics
)).build() should be(
HttpPayload(
POST,
Seq("2", "abtests", "42", "stop"),
isSearch = false,
isAnalytics = true,
requestOptions = None
)
)
}

it("should call insights endpoint") {
val json =
"""{"events":[{"eventType":"conversion","eventName":"event-name","index":"index-name","userToken":"user-token","queryID":"query-id","objectIDs":["objectID"]}]}"""
(custom request CustomRequest(
verb = POST,
path = Seq("1", "events"),
endpoint = RequestEndpoint.Insights,
body = Some(json)
)).build() should be(
HttpPayload(
verb = POST,
path = Seq("1", "events"),
body = Some(json),
isSearch = false,
isInsights = true,
requestOptions = None
)
)
}

it("should call personalization endpoint") {
val json =
"""{"eventsScoring":[{"eventName":"buy","eventType":"conversion","score":10}],"facetsScoring":[{"facetName":"brand","score":10}],"personalizationImpact":75}"""
(custom request CustomRequest(
verb = POST,
path = Seq("1", "strategies", "personalization"),
endpoint = RequestEndpoint.Personalization,
body = Some(json)
)).build() should be(
HttpPayload(
verb = POST,
path = Seq("1", "strategies", "personalization"),
body = Some(json),
isSearch = false,
isPersonalization = true,
requestOptions = None
)
)
}
}
}