Skip to content

Commit

Permalink
Make constructQuery typesafe (closes #62)
Browse files Browse the repository at this point in the history
  • Loading branch information
asoltysik committed Jul 6, 2018
1 parent f45679b commit 9dd2f12
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 21 deletions.
1 change: 0 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ lazy val root = project
Dependencies.Libraries.collUtil,
Dependencies.Libraries.hammockCore,
Dependencies.Libraries.hammockCirce,
Dependencies.Libraries.circeLiteral,
Dependencies.Libraries.specs2,
Dependencies.Libraries.specs2Mock,
Dependencies.Libraries.specsScalaCheck
Expand Down
2 changes: 0 additions & 2 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ object Dependencies {
// Scala
val collUtil = "6.39.0"
val hammock = "0.8.5"
val circe = "0.9.3"
// Tests
val specs2 = "3.9.4"
}
Expand All @@ -41,7 +40,6 @@ object Dependencies {
val hammockCore = "com.pepegar" %% "hammock-core" % V.hammock
val hammockCirce = "com.pepegar" %% "hammock-circe" % V.hammock
// Tests
val circeLiteral = "io.circe" %% "circe-literal" % V.circe % "test"
val specs2 = "org.specs2" %% "specs2-core" % V.specs2 % "test"
val specs2Mock = "org.specs2" %% "specs2-mock" % V.specs2 % "test"
val specsScalaCheck = "org.specs2" %% "specs2-scalacheck" % V.specs2 % "test"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ case class OwmAsyncClient[F[_]: Sync](appId: String, apiHost: String = "api.open

def receive[W <: OwmResponse: Decoder](request: OwmRequest): F[Either[WeatherError, W]] = {

val uri = request.constructQuery(appId)
val scheme = if (ssl) "https://" else "http://"
val url = scheme + apiHost + uri
val scheme = if (ssl) "https" else "http"

Uri
.fromString(url)
val uriOrError = Uri
.fromString(s"$scheme://$apiHost")
.map(request.constructQuery(_, appId))
.leftMap(InternalError)

uriOrError
.traverse { uri =>
Hammock
.request(Method.GET, uri, Map())
.map(uri => processResponse(uri))
.exec[F]
}
.map(x => x.joinRight)
.map(_.joinRight)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,28 @@
*/
package com.snowplowanalytics.weather.providers.openweather

import cats.data.NonEmptyList
import hammock.Uri

private[weather] object Requests {

sealed trait WeatherRequest {
def constructQuery(appId: String): String
def constructQuery(baseUri: Uri, apiKey: String): Uri
}

sealed trait OwmRequest extends WeatherRequest {
val endpoint: Option[String]
val resource: String
val parameters: Map[String, String]

/**
* Construct URI for specific type of request and all other data
*
* @param appId API key
* @return URI string ready to be sent
*/
def constructQuery(appId: String): String = {
val end = endpoint.map(e => s"$e/$resource").getOrElse(s"$resource")
val params = parameters ++ Map("appid" -> appId)
val queryString = params.map { case (a, b) => s"$a=$b" }.mkString("&")
s"/data/2.5/$end?$queryString"
def constructQuery(baseUri: Uri, apiKey: String): Uri = {
val versionedBaseUri = baseUri / "data" / "2.5"
val uriWithPath = endpoint.map(e => versionedBaseUri / e / resource).getOrElse(versionedBaseUri / resource)
val params = NonEmptyList.of("appid" -> apiKey) ++ parameters.toList

uriWithPath ? params
}

}

final case class OwmHistoryRequest(resource: String, parameters: Map[String, String]) extends OwmRequest {
Expand Down

0 comments on commit 9dd2f12

Please sign in to comment.