Skip to content

Commit

Permalink
Parse uri in config
Browse files Browse the repository at this point in the history
  • Loading branch information
colmsnowplow committed Sep 4, 2023
1 parent f5517fb commit 6ec7e6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object Config {
case class File(path: URI) extends Output
case class PubSub(subscription: String) extends Output
case class Kafka(brokers: String, topic: String, producerConf: Map[String, String] = Map.empty) extends Output
case class Http(endpoint: String) extends Output
case class Http(endpoint: org.http4s.Uri) extends Output
}

val configOpt = Opts.option[Path]("config", "Path to the configuration HOCON").orNone
Expand Down Expand Up @@ -103,6 +103,10 @@ object Config {
Either.catchOnly[IllegalArgumentException](URI.create(str)).leftMap(_.getMessage)
}

implicit val httpUriDecoder: Decoder[org.http4s.Uri] = Decoder[String].emap { str =>
org.http4s.Uri.fromString(str).leftMap(_.getMessage)
}

implicit val kafkaDecoder: Decoder[Output.Kafka] =
deriveConfiguredDecoder[Output.Kafka]

Expand All @@ -115,6 +119,9 @@ object Config {
implicit val pubSubDecoder: Decoder[Output.PubSub] =
deriveConfiguredDecoder[Output.PubSub]

implicit val httpDecoder: Decoder[Output.Http] =
deriveConfiguredDecoder[Output.Http]

implicit val outputDecoder: Decoder[Output] =
deriveConfiguredDecoder[Output]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import org.http4s.ember.client.EmberClientBuilder
import org.http4s.Request
import org.http4s.Header.Raw
import org.http4s.Method
import org.http4s.Uri

import org.typelevel.ci._
import com.snowplowanalytics.snowplow.eventgen.tracker.HttpRequestQuerystring
Expand All @@ -36,15 +35,6 @@ object Http {

def sink[F[_]: Async](properties: Config.Output.Http): Pipe[F, Main.GenOutput, Unit] = {

val baseUri = Uri.fromString(properties.endpoint) match {
case Right(value) => value
case Left(_) =>
throw new IllegalArgumentException(
s"Error: cannot produce uri from endpoint provided: ${properties.endpoint}"
)
}


def buildRequesst(
generatedRequest: HttpRequest
): Request[F] = {
Expand All @@ -71,8 +61,8 @@ object Http {
// since /i requests have empty version, we pattern match to add the path.
// address is a var since we modify it when adding querystring
var address = generatedRequest.method.path.version match {
case "" => baseUri / generatedRequest.method.path.vendor
case version: String => baseUri / generatedRequest.method.path.vendor / version
case "" => properties.endpoint / generatedRequest.method.path.vendor
case version: String => properties.endpoint / generatedRequest.method.path.vendor / version
}

val body = generatedRequest.body match {
Expand Down

0 comments on commit 6ec7e6c

Please sign in to comment.