Skip to content

Ability to stream entities #1392

@raulpopadineti

Description

@raulpopadineti

Hi,

We'd like to build an API that is able to paginate entities and send them in chunks.

A really raw way of sending data in chunks would be:

require 'grape'

module Stream
  class API < Grape::API
    prefix 'api'
    format :txt
    default_format :txt

    class Streamer
      def initialize(pages:, page_size:, latency:)
        @pages = pages
        @page_size = page_size
        @latency = latency
      end

      def each
        (1..@pages).lazy.each do |p|
          yield "#{p}\n" * @page_size
          sleep(@latency)
        end
      end
    end

    resources :users do
      params do
        optional :pages, type: Integer, default: 10
        optional :page_size, type: Integer, default: 10
        optional :latency, type: Float, default: 1.0
      end

      get do
        status 200
        p = declared(params)
        stream Streamer.new(pages: p.pages, page_size: p.page_size, latency: p.latency)
      end
    end
  end
end

With this approach I get the deprecation warning:

[DEPRECATION] Argument as FileStreamer-like object is deprecated. Use path to file instead.

because Grape assumes the response is a file.

Will you support streaming of a Grape::Entity or any other non-file objects in the future?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions