Skip to content

Instantly share code, notes, and snippets.

@csokun
Created August 11, 2023 02:45
Show Gist options
  • Save csokun/b7fadda2a247075374ac2ca131b32fe6 to your computer and use it in GitHub Desktop.
Save csokun/b7fadda2a247075374ac2ca131b32fe6 to your computer and use it in GitHub Desktop.
Elixir using Finch to stream Stellar Horizon transaction
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