Skip to content

Instantly share code, notes, and snippets.

@eyalroth
Last active October 17, 2018 14:43
Show Gist options
  • Save eyalroth/c26f383ba6edf0f4e1fc39a42e876e12 to your computer and use it in GitHub Desktop.
Save eyalroth/c26f383ba6edf0f4e1fc39a42e876e12 to your computer and use it in GitHub Desktop.
akka-http #2257 - Http API to server
import akka.actor.ActorSystem
import akka.http.scaladsl.client.RequestBuilding
import akka.http.scaladsl.model.{HttpResponse, StatusCodes}
import akka.http.scaladsl.unmarshalling.Unmarshal
import com.typesafe.scalalogging.StrictLogging
import HttpClient
import scala.concurrent.Future
class HttpServerAPI(lowThroughputClient: HttpClient, highThroughputClient: HttpClient)
extends ServerApi[HttpResponse]
with MyJsonProtocol
with StrictLogging {
/* --- Methods --- */
/* --- Public Methods --- */
override def sayHello(requestId: Int): Future[HttpResponse] = {
import lowThroughputClient.ec
val hello = Hello(requestId)
val request = RequestBuilding.Post(buildUri("hello"), hello)
lowThroughputClient.queueRequest(request)
}
override def sendMetrics(metricMessage: MetricMessage): Future[HttpResponse] = {
import highThroughputClient.ec
val request = RequestBuilding.Post(buildUri("metrics"), metricMessage)
highThroughputClient.queueRequest(request)
}
override def sendReportCard(reportCard: ReportCard): Future[HttpResponse] = {
import highThroughputClient.ec
val request = RequestBuilding.Post(buildUri(s"card"), reportCard)
highThroughputClient.queueRequest(request)
}
/* --- Private Methods --- */
private def buildUri(suffix: String) = s"${lowThroughputClient.baseUri}/$suffix"
}
object HttpServerAPI {
/* --- Constructors --- */
def apply(config: TypesafeConfiguration, system: ActorSystem): HttpServerAPI = {
val lowThroughputClient = HttpClient(config.getConfig("server-low"), system)
val highThroughputClient = HttpClient(config.getConfig("server-high"), system)
new HttpServerAPI(lowThroughputClient, highThroughputClient)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment