Class 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) inspect getDelayUntilNextAllowedRequest() after consuming the response and not make any new request against the API resource until this duration of time has passed.
    • Constructor Detail

      • StreamingRateLimitedResponse

        public StreamingRateLimitedResponse​(Stream<S> responseElements,
                                            Function<? super S,​Stream<T>> flatMapper)
      • StreamingRateLimitedResponse

        public StreamingRateLimitedResponse​(Stream<T> elements,
                                            Supplier<Duration> delayUntilNextAllowedRequest)
    • Method Detail

      • 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 to asStream() where you must handle this yourself. When this method returns, it is thus also safe to invoke getDelayUntilNextAllowedRequest(), 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 underlying stream and properly consuming that with try-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 Java Stream. Using this method requires proper resource handling and the stream must be closed after it has been consumed.

        Prefer forEach(ResponseElementHandler) over this method.

        Returns:
        the elements of the response as a Stream.