Tab Key
Tab Key

Reputation: 171

Elixir - Dynamically setting request headers for stripity_stripe library

I am using stripity_stripe library and want to send some requests to stripe through a proxy server but not the others. So, I am trying to dynamically send hackney options on some requests. I looked into the code and documentation of stripity_stripe library but couldn’t find any example to send the hackney options related to proxy. I figured out to set the proxy related information in the config.ex file and it is working fine, but since it is set in the configuration, it will be applied to all the requests.

config :stripity_stripe,
  api_key: System.get_env("STRIPE_API_KEY"),
  hackney_opts: [
    {:ssl_options, [cacertfile: "CERT.pem"]},
    {:proxy, System.get_env("PROXY_SANDBOX_BASE_URL")},
    {:proxy_auth,
     {System.get_env("PROXY_SANDBOX_USERNAME"), System.get_env("PROXY_SANDBOX_PASSWORD")}}
  ]

I found a function request() in stripity_stripe to dynamically send headers and options but could not succeed to send these options successfully. Here is how I tried it:

Stripe.API.request(params, :post, "tokens", %{},
      hackney_opts: [
        {:ssl_options, [cacertfile: "/Users/apple/.ssh/CERT.pem"]},
        {:proxy, System.get_env("PROXY_SANDBOX_BASE_URL")},
        {:proxy_auth,
         {System.get_env("PROXY_SANDBOX_USERNAME"), System.get_env("PROXY_SANDBOX_PASSWORD")}}
      ]
    )

Any help would be appreciated. Thanks

Upvotes: 1

Views: 370

Answers (1)

Everett
Everett

Reputation: 9578

Omit the hackney_options key when you call Stripe.API.request/5 -- just pass the list of opts directly, e.g.

Stripe.API.request(
  params, 
  :post, 
  "tokens", 
  %{}, 
  ssl_options: [cacertfile: "/Users/apple/.ssh/CERT.pem"],  
  proxy: System.get_env("VGS_SANDBOX_BASE_URL")
) 

Remember that when the last argument is a keyword list, the brackets are often omitted. You could include them to help make it easier to see which things belong to that final argument e.g.

Stripe.API.request(
  params, 
  :post, 
  "tokens", 
  %{}, 
  [
    ssl_options: [cacertfile: "/Users/apple/.ssh/CERT.pem"],  
    proxy: System.get_env("VGS_SANDBOX_BASE_URL")
  ]
) 

Upvotes: 1

Related Questions