I have created an API using Ratpack and Groovy. I want a POST API such that the data should be processed and stored in 2 cassandra databases say table-A and table-B. For Now I have this in my Ratpack.groovy, and thus I have to call both the APIs whenever a data has to be pushed:
prefix("A") {
post { registry.get(PostEndpointA).handle(context) }
}
prefix("B") {
post { registry.get(PostEndpointB).handle(context) }
}
I wanted a single POST API Call like this, so that by single API call the request can be delegated to both the endpoints together:
prefix("post") {
post { registry.get(PostEndpointA).handle(context) }
post { registry.get(PostEndpointB).handle(context) }
}
OR, I want this:
prefix("post") {
post { registry.get(PostEndpoint).handle(context) }
}
And in the PostEndpoint, I can perform both the operations as this:
saveJsonAsA(context)
.promiseSingle()
.then { ItemA updatedItem ->
context.response.headers
.add(HttpHeaderNames.LOCATION, "/item/api/A")
context.response.status(HttpResponseStatus.CREATED.code())
.send()
}
saveJsonAsB(context)
.promiseSingle()
.then { ItemB updatedItem ->
context.response.headers
.add(HttpHeaderNames.LOCATION, "/item/api/B")
context.response.status(HttpResponseStatus.CREATED.code())
.send()
}
In both the cases, the item is added to only table-A and not B or whatsoever is written in the code earlier.
Note That ItemA and ItemB relates to essentially same DB, only the primary keys are different, so as to facilitate the GET from 2 ways. Any idea how to do this in Ratpack?