Question

I have copied Spray Client's example code into my own project, to have it easily available. I use IntelliJ 13.

Here is the code I have:

package mypackage

import scala.util.Success
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.json.{JsonFormat, DefaultJsonProtocol}
import spray.can.Http
import spray.util._
import spray.client.pipelining._
import scala.util.Success
import scala.util.Failure

case class Elevation(location: Location, elevation: Double)
case class Location(lat: Double, lng: Double)
case class GoogleApiResult[T](status: String, results: List[T])

object ElevationJsonProtocol extends DefaultJsonProtocol {
  implicit val locationFormat = jsonFormat2(Location)
  implicit val elevationFormat = jsonFormat2(Elevation)
  implicit def googleApiResultFormat[T :JsonFormat] = jsonFormat2(GoogleApiResult.apply[T])
}

object SprayExample extends App {
  // we need an ActorSystem to host our application in
  implicit val system = ActorSystem("simple-spray-client")
  import system.dispatcher // execution context for futures below
  val log = Logging(system, getClass)

  log.info("Requesting the elevation of Mt. Everest from Googles Elevation API...")

  val pipeline = sendReceive ~> unmarshal[GoogleApiResult[Elevation]]

  val responseFuture = pipeline {
    Get("http://maps.googleapis.com/maps/api/elevation/json?locations=27.988056,86.925278&sensor=false")
  }
  responseFuture onComplete {
    case Success(GoogleApiResult(_, Elevation(_, elevation) :: _)) =>
      log.info("The elevation of Mt. Everest is: {} m", elevation)
      shutdown()

    case Success(somethingUnexpected) =>
      log.warning("The Google API call was successful but returned something unexpected: '{}'.", somethingUnexpected)
      shutdown()

    case Failure(error) =>
      log.error(error, "Couldn't get elevation")
      shutdown()
  }

  def shutdown(): Unit = {
    IO(Http).ask(Http.CloseAll)(1.second).await
    system.shutdown()
  }
}

As it stands, this works perfectly and it prints the height of Mt.Everest as expected.
The strange thing happens if I move the file down one level in the package structure, that is I create a mypackage.myinnerpackage and move the file inside it.
IDEA changes my first line of code into package mypackage.myinnerpackage and that's it.

Then I try to run the app and the compilation will fail with the following message:

  could not find implicit value for evidence parameter of type spray.httpx.unmarshalling.FromResponseUnmarshaller[courserahelper.sprayexamples.GoogleApiResult[courserahelper.sprayexamples.Elevation]]
  val pipeline = sendReceive ~> unmarshal[GoogleApiResult[Elevation]]
                             ^

I did not change anything in the code, I effectively just changed the package! Additionally, this code is self contained, it does not rely on any other implicit I declared in any other part of my code....

What am I missing?

Thanks!

Was it helpful?

Solution

(Replaced the comment by this answer which supports proper formatting.)

The code you posted is missing these two imports before the usage of unmarshal:

 import ElevationJsonProtocol._
 import SprayJsonSupport._
 val pipeline = sendReceive ~> unmarshal[GoogleApiResult[Elevation]]

which exist in the original code. IntelliJ is sometimes messing with imports so that may be the reason they got lost in the transition.

OTHER TIPS

You need to provide a Json Formatter for your case class.

case class Foo(whatever: Option[String])

object FooProtocol extends DefaultJsonProtocol {
  implicit val fooJsonFormat = jsonFormat1(Foo)
}

Then include the following near the implementation...

import SprayJsonSupport._
import co.xxx.FooProtocol._
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top