Class StreamingRateLimitedResponse<T>
- java.lang.Object
-
- no.digipost.api.useragreements.client.response.StreamingRateLimitedResponse<T>
-
- Type Parameters:
T- The type of the entities contained in the response.
public class StreamingRateLimitedResponse<T> extends Object
Represents a streamed response from the Digipost API, and offer mechanisms for consuming it incrementally. In addition, one should (must) inspectgetDelayUntilNextAllowedRequest()after consuming the response and not make any new request against the API resource until this duration of time has passed.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceStreamingRateLimitedResponse.ResponseElementHandler<T>
-
Constructor Summary
Constructors Constructor Description StreamingRateLimitedResponse(Stream<S> responseElements, Function<? super S,Stream<T>> flatMapper)StreamingRateLimitedResponse(Stream<T> elements, Supplier<Duration> delayUntilNextAllowedRequest)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Stream<T>asStream()Expose the retrieved elements as a JavaStream.<R> StreamingRateLimitedResponse<R>flatMap(Function<? super T,Stream<R>> mapper)voidforEach(StreamingRateLimitedResponse.ResponseElementHandler<T> handler)Consume each element of the response.DurationgetDelayUntilNextAllowedRequest()Get the duration until a new request is allowed against the API resource which returned this response.<R> StreamingRateLimitedResponse<R>map(Function<? super T,R> mapper)
-
-
-
Method Detail
-
map
public <R> StreamingRateLimitedResponse<R> map(Function<? super T,R> mapper)
-
flatMap
public <R> StreamingRateLimitedResponse<R> flatMap(Function<? super T,Stream<R>> mapper)
-
forEach
public void forEach(StreamingRateLimitedResponse.ResponseElementHandler<T> handler)
Consume each element of the response. This will properly terminate the response and close associated resources before the method returns, as opposed toasStream()where you must handle this yourself. When this method returns, it is thus also safe to invokegetDelayUntilNextAllowedRequest(), as this instant should have been captured from the end of the streaming response.- Parameters:
handler- The function to invoke for each element.
-
getDelayUntilNextAllowedRequest
public Duration getDelayUntilNextAllowedRequest()
Get the duration until a new request is allowed against the API resource which returned this response. If a new request is made before waiting this duration, the API will respond with an error, and requiring a new duration to wait before a new request will be allowed.This method must be invoked after the response has been consumed e.g. using
forEach(ResponseElementHandler), or acquiring the underlyingstreamand properly consuming that withtry-with-resources.- Returns:
- the durtaion until a new request is allowed against the API resource.
- Throws:
NextAllowedRequestTimeNotFoundException- if the duration has not yet been captured from the response.
-
asStream
public Stream<T> asStream()
Expose the retrieved elements as a JavaStream. Using this method requires proper resource handling and the stream must beclosedafter it has been consumed.Prefer
forEach(ResponseElementHandler)over this method.- Returns:
- the elements of the response as a Stream.
-
-