David I noticed that when I define a contract, I u...
# http4k
r
David I noticed that when I define a contract, I use route and bindContract Method.GET to { request -> Response}. So it seems I can only bind the POST method to a synchronous method.. But in my case I get a request, I go to another backend asynchronously and only when I get a response from the other backend then have a callback. Is there anything I can do to go around this? Sorry for bothering you so much! and really thank you for everything!
d
Hey. Can you just clarify for me? The call to the external system - when you say that it's asynchronous - what do you mean exactly? Is the other system calling back to you or are you calling back the original.caller?
r
I receive a request into my service and I wanted to call a second service with my own request, once I get a response from that service, also respond to the original request. I was using the apache async client to call the second service through my service:
Copy code
val asyncClient = ApacheAsyncClient()
    asyncClient(Request(Method.GET, “<http://httpbin.org/second/service“>)) {
    }
Check response from second service, and response to my original called here.
}
    thread {
        Thread.sleep(500)
        asyncClient.close()
    }
But bindContracts to method expects a {request -> response} while I will have to make a new request with a callback inside of that using the async call.. I might still not be used to kotlin but it seems as though one has a non async signature and the other has an async one…
d
You have a synchronous call chain of a -> b -> c. That's the defacto standard microservice pattern (I checked with Sam Newman 😉), so is there any reason that you don't just want to model it in that way? ie. Have your service (b) call service (c) using a synchronous client and then transform the result.and return it to your caller (a).
👍 1
r
To be completely honest, I used to be mainly client side developer for 2 years (SDK/App/Game) but I switched now to backend and in the first 2 weeks I have to implement the backend service, learn kotlin, micro-services, define the API. I’m almost done, but of course they will review it before when I finish, they expect some mistakes. However to your question, it’s because I come from a different environment where I’m used to using almost exclusively async calls whenever possible. I understand now that this is a logical chain of events in micro-services, and I have used the non async one in your library. I’ll go over a micro-services book today, reading a kotlin and backend ones weren’t enough. Again, can’t thank you enough! hope to be able to contribute to this library or some other made by you ASAP, having you answer these questions is really cutting my time and focusing me in the right directions!
d
So generally there is no need to do async in this type of scenario using http4k. It's easier to stick to using sync interactions.
👍 1
http4k is designed to work using a sync model. async is something we're looking at for the future but it's not imminent.
👍 1