Attempts
This commit is contained in:
parent
9c24e1651b
commit
bac33b304e
@ -1,4 +1,5 @@
|
|||||||
defmodule MicrosoftTranslator.Client do
|
defmodule MicrosoftTranslator.Client do
|
||||||
|
@attempt_delay 3_000
|
||||||
@base_host "api.cognitive.microsofttranslator.com"
|
@base_host "api.cognitive.microsofttranslator.com"
|
||||||
@base_path "/"
|
@base_path "/"
|
||||||
@api_methods %{
|
@api_methods %{
|
||||||
@ -103,12 +104,22 @@ defmodule MicrosoftTranslator.Client do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def fetch(api_method, headers, body, params) do
|
def fetch(api_method, headers, body, params, attempt \\ 0) do
|
||||||
method = @api_methods[api_method].method
|
method = @api_methods[api_method].method
|
||||||
url = "https://#{@base_host}#{@base_path}#{@api_methods[api_method].path}#{params}"
|
url = "https://#{@base_host}#{@base_path}#{@api_methods[api_method].path}#{params}"
|
||||||
|
|
||||||
{:ok, response} = Mojito.request(method, url, headers, body)
|
case Mojito.request(method, url, headers, body) do
|
||||||
|
{:ok, response} ->
|
||||||
|
response
|
||||||
|
|
||||||
response
|
{:error, %Mojito.Error{reason: :timeout} = response} ->
|
||||||
|
if attempt < 2 do
|
||||||
|
:timer.sleep(@attempt_delay)
|
||||||
|
IO.inspect(url, label: "Attempt #{attempt + 1}")
|
||||||
|
fetch(api_method, headers, body, params, attempt + 1)
|
||||||
|
else
|
||||||
|
response
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user