Skip to content

Stream resource buffer #2

@joeesteves

Description

@joeesteves
 def get_chunked(credentials, resource, action, params \\ nil) do
    %HTTPoison.AsyncResponse{id: ref} =
      HTTPoison.get!(
        url_factory(credentials, resource, action, params),
        [],
        recv_timeout: :infinity,
        stream_to: self()
      )

    Stream.resource(
      fn -> "" end,
      fn previus_chunk_remanent ->
        receive do
          %HTTPoison.AsyncChunk{chunk: chunk, id: ^ref} ->
            capture = Regex.named_captures(~r/\[?(?<complete>.*})?(?<incomplete>.*)\]?/, chunk)

            partial_content = previus_chunk_remanent <> capture["complete"]

            next = extract_maps_if_any(partial_content)

            {next, create_chunk_remanent(next, partial_content, capture["incomplete"])}

          %HTTPoison.AsyncEnd{id: ^ref} ->
            {:halt, ""}
        end
      end,
      fn _ -> nil end
    )
    |> Stream.map(&try_decode/1)
  end

Create an intermediary function that acts as buffer
HTTP will stream to buffer and buffer will pass item by item on demand to Stream resource..

This should support Enum.take

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions