Skip to content

Commit 913bd12

Browse files
committed
adjustment: for latest Tesla / Hackney
1 parent 63af835 commit 913bd12

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

config/dev.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import Config
22

33
config :ex_urlbox,
44
api_key: {:system, "URLBOX_API_KEY"},
5-
api_secret: {:system, "URLBOX_API_SECRET"}
5+
api_secret: {:system, "URLBOX_API_SECRET"},
6+
env: :dev

config/test.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ config :tesla, ExUrlbox, adapter: Tesla.Mock
44

55
config :ex_urlbox,
66
api_key: "fakeAPIkey",
7-
api_secret: "fakeAPIsecret"
7+
api_secret: "fakeAPIsecret",
8+
env: :test
89

910
config :logger, level: :info

lib/config.ex

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,18 @@ defmodule ExUrlbox.Config do
5454

5555

5656
# Determine the Tesla adapter to use
57-
@doc false
58-
def get_client_adapter, do: Application.get_env(:tesla, ExUrlbox, [])[:adapter] || Tesla.Adapter.Hackney
57+
@doc """
58+
Returns the appropriate Tesla adapter based on the environment.
59+
In test environment, returns Tesla.Mock.
60+
In other environments, returns the configured adapter or defaults to Hackney.
61+
"""
62+
def get_client_adapter(opts \\ []) do
63+
env = Application.get_env(:ex_urlbox, :env, :prod)
64+
65+
case env do
66+
:test -> Tesla.Mock
67+
_ -> {Application.get_env(:tesla, ExUrlbox, [])[:adapter] || Tesla.Adapter.Hackney, opts}
68+
end
69+
end
5970

6071
end

lib/ex_urlbox.ex

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule ExUrlbox do
44
55
**Compatible with Urlbox API Version:** `v1`
66
7-
A list of options that you can pass into `ExUrlBox.get/3`, `ExUrlBox.post/3`, `ExUrlBox.head/3`, and `ExUrlBox.delete/3` can be found here:
7+
A list of options that you can pass into `ExUrlbox.get/3`, `ExUrlbox.post/3`, `ExUrlbox.head/3`, and `ExUrlbox.delete/3` can be found here:
88
https://urlbox.io/docs/options
99
"""
1010

@@ -20,7 +20,7 @@ defmodule ExUrlbox do
2020
Refer to the official documentation for all the available options: https://urlbox.io/docs/options
2121
2222
**This action is `synchronous`. **
23-
If you want to use an `async` flow that uses webhooks, use `ExUrlBox.post/3`.
23+
If you want to use an `async` flow that uses webhooks, use `ExUrlbox.post/3`.
2424
2525
**Function signature is: `url, [options], timeout`**
2626
@@ -170,9 +170,16 @@ defmodule ExUrlbox do
170170
]
171171
|> determine_endpoint(is_post?)
172172

173-
Tesla.client(middleware, Config.get_client_adapter)
173+
# Pass timeout to Hackney adapter
174+
adapter_opts = [recv_timeout: timeout]
175+
176+
# Use Config module to determine the appropriate adapter
177+
adapter = ExUrlbox.Config.get_client_adapter(adapter_opts)
178+
179+
Tesla.client(middleware, adapter)
174180
end
175181

182+
176183
@doc false
177184
@spec determine_endpoint(list(), boolean()) :: list()
178185
defp determine_endpoint(middleware, is_post?) do

test/ex_urlbox_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
defmodule ExUrlboxTest do
22
use ExUnit.Case, async: false
3-
alias ExUrlbox.Config
43
alias ExUrlbox.Utils
54

65
doctest ExUrlbox
@@ -12,7 +11,7 @@ defmodule ExUrlboxTest do
1211
height: 768
1312
]
1413

15-
@assembled_request_url "https://api.urlbox.io/v1/fakeAPIkey/1783c14642a8a56a36f6492f73f524bfe132ed91/png?url=https%3A%2F%google.com&width=1024&height=768"
14+
@assembled_request_url "https://api.urlbox.io/v1/fakeAPIkey/3e2c5515f8a2de570445a8b4b5c041809fb9b97e/png?url=https%3A%2F%2Fgoogle.com&width=1024&height=768"
1615

1716
# Mock requests
1817
setup_all do
@@ -40,6 +39,7 @@ defmodule ExUrlboxTest do
4039
options = [{:webhook_url, "https://example.com/webook"} | @test_options]
4140
{:ok, screenshot_request} = ExUrlbox.post(@test_url, options)
4241

42+
4343
# Headers
4444
assert Tesla.get_headers(screenshot_request, "authorization") === ["Bearer #{Application.get_env(:ex_urlbox, :api_secret)}"]
4545
assert Tesla.get_headers(screenshot_request, "content-type") === ["application/json"]

0 commit comments

Comments
 (0)