-
-
Save mazz/a741bd2f440d3ddc735f865021a0dfc5 to your computer and use it in GitHub Desktop.
Elixir using Finch to stream Stellar Horizon transaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Finch.start_link(name: MyFinch) | |
url = "https://horizon.stellar.org/accounts/GAFNBDOZBFUKHB4ZY5ANCQ25PM33YLTMWQKW25J7LXNZM3YSHRH3BELY/transactions?limit=200&cursor=119456526699307008&include_failed=true" | |
Finch.build(:get, url, [{"Accept", "text/event-stream"}]) | |
|> Finch.stream(MyFinch, nil, fn | |
{:status, status}, _acc -> | |
IO.inspect(status) | |
{:headers, headers}, _acc -> | |
IO.inspect(headers) | |
{:data, data}, _acc -> | |
[chunk | _]= String.split(data, "\n\n") | |
# ["retry: 1000", "event: open", "data: \"hello\""] | |
# [id, data] | |
# ["retry: 10", "event: close", "data: \"byebye\""] | |
case String.split(chunk, "\n", trim: true) do | |
["retry: " <> timeout, "event: " <> event, _data] -> | |
IO.inspect("event: #{event}") | |
["id: " <> id, "data: " <> data] -> | |
content = Jason.decode!(data) | |
%{"paging_token" => cursor} = content | |
IO.inspect("id: #{id} - cursor: #{cursor}") | |
end | |
end, receive_timeout: :infinity) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment