Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream resource buffer #2

Open
joeesteves opened this issue Sep 4, 2018 · 0 comments
Open

Stream resource buffer #2

joeesteves opened this issue Sep 4, 2018 · 0 comments

Comments

@joeesteves
Copy link
Owner

 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant