I have to deal with an external API that is rate-limited. This is, if I call the API more than a number of times per second I'll get an exception. The cool thing is that inside the exception, the service specifies the number of seconds to wait before placing another call.
In my case, I have an observable that works against that service using a construct like this
obs.SelectMany(i => service.CallAsync(i))
How could I be aware of the RateLimitExceptions and delay the next call by the number of seconds that the service says into the exception?
Thank you!
This is about creating a sequence that autolimits itself, the other question proposed as duplicate is about ROUTING + limiting. While it sounds similar, I don't enter in the details on how this could be implement, while here it's exclusively about Rx, with no async/await involved.
EDIT: The API I'm consuming is Microsoft's Cognitive Services Face API (https://dev.projectoxford.ai/docs/services/563879b61984550e40cbbe8d/operations/563879b61984550f30395236)
MS provides a Nuget package (Microsoft.ProjectOxford.Face) with a client (FaceApiClient) with all the operations as async methods. For instance:
Task<Microsoft.ProjectOxford.Face.Contract.Face[]> DetectAsync(string imageUrl, bool returnFaceId = true, bool returnFaceLandmarks = false, IEnumerable<FaceAttributeType> returnFaceAttributes = null);
The exception is a FaceAPIException with has a Message like: "Rate limit is exceeded. Try again in 26 seconds." with an ErrorCode 429.
My objective is to take a bunch of images, detect the faces in each of them and identify each face.
identifications = foreach image in Images
select detection in Detect(image)
select identification in Identify(detection)