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: "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

Well omitting the hackney_options key when calling Stripe.API.request/5, and just putting the list of opts worked, i.e.
Stripe.API.request(params, :post, "tokens", %{}, ssl_options: [cacertfile: "/Users/apple/.ssh/CERT.pem"], proxy: System.get_env("PROXY_SANDBOX_BASE_URL"))

Hope it helps someone else.

2 Likes