0

I am new in scala. Currently, to create rest API I'm using the spray. Now I want to consume API from another server. I'm calling this API on every key-stroke from UI. I'm aborting the request using AbortController if the user keeps typing and the previous request is in pending status. To hit other server request, I'm using spray-client. Which goes something like this:

def completeService(completeRequest: CompleteRequest): Future[HttpResponse] = {

  val pipeline: HttpRequest => Future[HttpResponse] = sendReceive ~> unmarshal[HttpResponse]
  val response: Future[HttpResponse] = pipeline(Post(someremoteUrl.concat("complete"), completeRequest)
   ~> addHeader("demo", "test"))
  response
}

I'm able to access using this above code. And I'm getting the expected response. But the thing is time-consuming. It creates new TCP connection and communicates with the host, hits the API gives the response then terminates the connection. Here while terminating it sits idle for sometimes and didn't accept a new connection.

Is there any alternative way to do this?

dharmesh singh
  • 101
  • 1
  • 6

1 Answers1

0

You can create rest request using akka http client. you can see detailed example here

igx
  • 4,101
  • 11
  • 43
  • 88
  • Can we use Akka-Http-client with spray? Because both have the same models(HttpMethods, HttpResponse,...) – dharmesh singh Mar 19 '20 at 04:37
  • @dharmeshsingh definitely yes, just add the akka-http-spry-json lib https://doc.akka.io/docs/akka-http/current/common/json-support.html – igx Mar 20 '20 at 08:16