Skip to content

Commit

Permalink
common: add Adapted type (close #560)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy authored and benjben committed Feb 25, 2022
1 parent 12d7e47 commit ca5a37b
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ import cats.implicits._

import cats.effect.Clock

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingData}
import com.snowplowanalytics.iglu.core.circe.CirceIgluCodecs._

import io.circe._
import io.circe.syntax._

import org.apache.http.NameValuePair

import org.joda.time.{DateTime, DateTimeZone}
import org.joda.time.format.{DateTimeFormat, DateTimeFormatter}

import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingData}
import com.snowplowanalytics.iglu.core.circe.CirceIgluCodecs._

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup

import com.snowplowanalytics.snowplow.badrows.FailureDetails

import com.snowplowanalytics.snowplow.enrich.common.RawEventParameters
import com.snowplowanalytics.snowplow.enrich.common.adapters.RawEvent
import com.snowplowanalytics.snowplow.enrich.common.loaders.CollectorPayload
Expand Down Expand Up @@ -394,6 +397,8 @@ trait Adapter {

object Adapter {

type Adapted = ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]

/** The Iglu schema URI for a Snowplow unstructured event */
val UnstructEvent: SchemaKey = SchemaKey(
"com.snowplowanalytics.snowplow",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ package adapters
package registry

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.data.NonEmptyList
import cats.effect.Clock
import cats.syntax.validated._

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}
import com.snowplowanalytics.snowplow.badrows.FailureDetails

import io.circe.Json

import org.joda.time.DateTimeZone
import org.joda.time.format.DateTimeFormat

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}

import com.snowplowanalytics.snowplow.badrows.FailureDetails

import loaders.CollectorPayload
import utils._
import Adapter.Adapted

/**
* Transforms a collector payload which conforms to
Expand Down Expand Up @@ -72,9 +74,10 @@ object CallrailAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] = {
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](
payload: CollectorPayload,
client: Client[F, Json]
): F[Adapted] = {
val _ = client
val params = toMap(payload.querystring)
if (params.isEmpty) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,26 @@ import scala.util.Try
import org.joda.time.DateTime

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.effect.Clock
import cats.data.NonEmptyList
import cats.syntax.apply._
import cats.syntax.either._
import cats.syntax.option._
import cats.syntax.validated._

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}
import cats.effect.Clock

import io.circe._

import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup

import com.snowplowanalytics.snowplow.badrows.FailureDetails

import loaders.CollectorPayload
import utils.{ConversionUtils, HttpClient}
import Adapter.Adapted

/** Transforms a Cloudfront access log into raw events */
object CloudfrontAccessLogAdapter extends Adapter {
Expand Down Expand Up @@ -79,9 +82,7 @@ object CloudfrontAccessLogAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a validation boxing either a NEL of raw events or a NEL of failure strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
payload.body match {
case Some(p) =>
val _ = client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@ import scala.annotation.tailrec

import cats.{Applicative, Functor, Monad}
import cats.data.{NonEmptyList, ValidatedNel}
import cats.effect.Clock
import cats.implicits._

import cats.effect.Clock

import io.circe._
import io.circe.syntax._

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer, SelfDescribingData}

import com.snowplowanalytics.snowplow.badrows.FailureDetails

import io.circe._
import io.circe.syntax._

import loaders.CollectorPayload
import utils.HttpClient
import utils.ConversionUtils._
import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the Google Analytics
Expand Down Expand Up @@ -468,9 +470,10 @@ object GoogleAnalyticsAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] = {
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](
payload: CollectorPayload,
client: Client[F, Json]
): F[Adapted] = {
val events: Option[NonEmptyList[ValidatedNel[FailureDetails.AdapterFailure, RawEvent]]] = for {
body <- payload.body
_ = client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,26 @@ package adapters
package registry

import cats.Monad
import cats.data.{Kleisli, NonEmptyList, ValidatedNel}
import cats.effect.Clock
import cats.data.{Kleisli, ValidatedNel}
import cats.instances.option._
import cats.syntax.either._
import cats.syntax.option._
import cats.syntax.validated._

import cats.effect.Clock

import org.joda.time.DateTime

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}
import com.snowplowanalytics.snowplow.badrows._

import io.circe._
import org.joda.time.DateTime

import loaders.CollectorPayload
import utils.{HttpClient, JsonUtils}
import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the HubSpot webhook
Expand Down Expand Up @@ -76,9 +81,7 @@ object HubSpotAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
(payload.body, payload.contentType) match {
case (None, _) =>
val failure = FailureDetails.AdapterFailure.InputData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import com.snowplowanalytics.snowplow.enrich.common.adapters.RawEvent
import com.snowplowanalytics.snowplow.enrich.common.loaders.CollectorPayload
import com.snowplowanalytics.snowplow.enrich.common.utils.{ConversionUtils, HttpClient, JsonUtils}

import Adapter.Adapted

/**
* Transforms a collector payload which either:
* 1. Provides a set of kv pairs on a GET querystring with a &schema={iglu schema uri} parameter.
Expand Down Expand Up @@ -64,9 +66,10 @@ object IgluAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] = {
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](
payload: CollectorPayload,
client: Client[F, Json]
): F[Adapted] = {
val _ = client
val params = toMap(payload.querystring)
(params.get("schema").flatten, payload.body, payload.contentType) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package adapters
package registry

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.data.NonEmptyList
import cats.effect.Clock
import cats.syntax.either._
import cats.syntax.option._
Expand All @@ -31,6 +31,8 @@ import org.joda.time.format.DateTimeFormat
import loaders.CollectorPayload
import utils.{ConversionUtils, HttpClient, JsonUtils => JU}

import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the Mailchimp Tracking
* webhook into raw events.
Expand Down Expand Up @@ -73,9 +75,7 @@ object MailchimpAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
(payload.body, payload.contentType) match {
case (None, _) =>
val failure = FailureDetails.AdapterFailure.InputData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.collection.JavaConverters._
import scala.util.{Try, Success => TS, Failure => TF}

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.data.NonEmptyList
import cats.effect.Clock
import cats.syntax.either._
import cats.syntax.option._
Expand All @@ -37,6 +37,8 @@ import org.apache.http.client.utils.URLEncodedUtils
import loaders.CollectorPayload
import utils.{HttpClient, JsonUtils => JU}

import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the StatusGator Tracking
* webhook into raw events.
Expand Down Expand Up @@ -71,9 +73,7 @@ object MailgunAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
(payload.body, payload.contentType) match {
case (None, _) =>
val failure = FailureDetails.AdapterFailure.InputData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package adapters
package registry

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.data.ValidatedNel
import cats.effect.Clock
import cats.syntax.either._
import cats.syntax.option._
Expand All @@ -29,6 +29,7 @@ import io.circe._
import loaders.CollectorPayload
import utils.{HttpClient, JsonUtils}
import com.snowplowanalytics.snowplow.enrich.common.utils.ConversionUtils
import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the Mandrill Tracking webhook
Expand Down Expand Up @@ -69,9 +70,7 @@ object MandrillAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
(payload.body, payload.contentType) match {
case (None, _) =>
val failure = FailureDetails.AdapterFailure.InputData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ package adapters
package registry

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.data.ValidatedNel
import cats.effect.Clock
import cats.syntax.either._
import cats.syntax.option._
import cats.syntax.validated._

import com.snowplowanalytics.iglu.client.Client
import com.snowplowanalytics.iglu.client.resolver.registries.RegistryLookup
import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer}
Expand All @@ -30,6 +31,7 @@ import org.joda.time.format.DateTimeFormat

import loaders.CollectorPayload
import utils.{HttpClient, JsonUtils => JU}
import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the Marketo webhook into raw
Expand Down Expand Up @@ -70,9 +72,7 @@ object MarketoAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
(payload.body, payload.contentType) match {
case (None, _) =>
val failure = FailureDetails.AdapterFailure.InputData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.collection.JavaConverters._
import scala.util.{Try, Success => TS, Failure => TF}

import cats.Monad
import cats.data.{NonEmptyList, ValidatedNel}
import cats.data.NonEmptyList
import cats.instances.either._
import cats.syntax.either._
import cats.syntax.option._
Expand All @@ -42,6 +42,7 @@ import org.joda.time.DateTime

import loaders.CollectorPayload
import utils.{HttpClient, JsonUtils => JU}
import Adapter.Adapted

/**
* Transforms a collector payload which conforms to a known version of the Olark Tracking webhook
Expand Down Expand Up @@ -71,9 +72,7 @@ object OlarkAdapter extends Adapter {
* @param client The Iglu client used for schema lookup and validation
* @return a Validation boxing either a NEL of RawEvents on Success, or a NEL of Failure Strings
*/
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[
ValidatedNel[FailureDetails.AdapterFailureOrTrackerProtocolViolation, NonEmptyList[RawEvent]]
] =
override def toRawEvents[F[_]: Monad: RegistryLookup: Clock: HttpClient](payload: CollectorPayload, client: Client[F, Json]): F[Adapted] =
(payload.body, payload.contentType) match {
case (None, _) =>
val failure = FailureDetails.AdapterFailure.InputData(
Expand Down
Loading

0 comments on commit ca5a37b

Please sign in to comment.