Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Converting asynchronous backend service to synchronous request

Not applicable

Hi All,

We have an asynchronous service which needs to be serviced as synchronous API to a front-end application. The service responds with a 201 immediately with a requestId and eventually with actual response on a callbackUrl with the same requestId in response body. The callbackUrl is provided as part of request and be another Apigee endpoint of course. We have a need to make this service synchronous using Apigee. API will make the initial call and

- return an error code if anything other than 201 is received

- if 201 is received with requestId, request will be blocked until a response is received in callbackUrl for that responseId OR a timeout is hit. Delayed callback response will need to be ignored.

I have seen several references in the forum to make service callout asynchronously and later wait for responses. However, I am having hard time finding an API example to achieve what we need.

Thanks in advance, Ashish

0 5 1,884
5 REPLIES 5

HI @Ashish Pokharel, maybe we can use a Service Callout(SC) Policy. A SC policy with Response tag is an synchronous call. If you omit he Response tag, the SC will fire a call & proceed forward without any response.

In your case use the Response tag & in SC also set the timeout in milliseconds.

<ServiceCalloutasync="false"continueOnError="false"enabled="true"name="SC-1">
	<DisplayName>SC</DisplayName>
	<Response>calloutResponse</Response>
	<Timeout>60000</Timeout>
	<HTTPTargetConnection>
		<URL>http://example.com</URL>
	</HTTPTargetConnection>
</ServiceCallout>

After SC policy, use calloutResponse.status.code flow variable to check the status code & raise an error if required.

Not applicable

@Siddharth Barahalikar thanks but that may not be what we need. I am ok to wait for intermediate response synchronously from backend. If that is 201, I will get a requestId. Now, until the backend responds asynchronously (meaning it will be a different POST call from backend to a callbackUrl we can set from Apigee) I have to block original request and wait for a response for that particular requestId so I can attach the final response from backend tonoriginal request. So, some questions are

1. What callbackUrl will I set to get response

2. How will I block original request and wait for callback respose as response is a different POST from backend

3. How do I respond to the right requestId only

Thanks, Ashish

IDK, I am not able to see a convincing solution here. To me, these articles read how make a call asynchronously to more or less a synchronous backend so calls could be parallelized and later synchronized. That't not really what we need. We are looking for implementation of this facade described back in 2012

https://www.slideshare.net/apigee/essential-api-facade-patterns-synchronous-to-asynchronous-conversi...

I actually guessed this was a piece of cake but is becoming tricky 🙂

Not applicable

Will do. Thanks! I noticed a slideshare presentation by Santanu Dey which explains this facade pattern but has no code reference 😞