nkezhaya / on_flow

Elixir client for Flow
MIT License
5 stars 3 forks source link

Pass metadata #3

Closed dangdennis closed 2 years ago

dangdennis commented 2 years ago

Heya, we met in the Flow discord long ago when I myself started writing an elixir sdk!

I'd like to pass metadata into the grpc connection to utilize Alchemy's flow node. Their grpc api requires an additional api key in the metadata.

I figured the config api can take in additional keywords, one of which would be :metadata. But for the life of me, I can't figure out how to use the pass :metadata to the GRPC.Stub.connect. The lib did implement here - see issue. Ultimately metadata key-vals are passed along as header values.

Example API I'm playing with in my fork.

config :on_flow,
  host: "flow-mainnet.g.alchemy.com:443",
  connect_on_start: true,
  metadata: %{:api_key => "oiwpscm8wr2gvrrkfb1mcmhvpsomcuwd"}
dangdennis commented 2 years ago

If I start my server without authentication, I get

[error] Unexpected event in state :domain_lookup of type :info:
{:timeout, #Reference<0.2556804254.3388211201.221625>, {:cow_http2_machine, :preface_timeout}}
{:state, #PID<0.487.0>, {:up, #Reference<0.2556804254.3388211201.221607>},
 'flow-mainnet.g.alchemy.com', 443, "http", 'flow-mainnet.g.alchemy.com', 443,
 [],
 %{
   http2_opts: %{settings_timeout: :infinity},
   protocols: [:http2],
   retry: 100,
   retry_fun: &GRPC.Adapter.Gun.retry_fun/2,
   tcp_opts: [nodelay: true],
   transport: :tcp
 }, :undefined, :undefined, :gun_tcp, true, {:tcp, :tcp_closed, :tcp_error},
 :undefined, :undefined, :gun_default_event_h, :undefined}

Hardly helpful.

nkezhaya commented 2 years ago

Hey @dangdennis! I pushed a branch called metadata, which should support the :metadata config option exactly like you have it in your original post.

Can you point your project to the new branch in the repo here and let me know if that works for you?

dangdennis commented 2 years ago

Thank you! I’ll try asap. I did this exact same thing in my fork but maybe the root connect isn’t doing what I expected. We shall see!

nkezhaya commented 2 years ago

@dangdennis Just pushed another update and tested it with Alchemy. You'll need to add secure: true to your config, but it should work now.

Pushing the update to hex.

dangdennis commented 2 years ago

Works for me too.