Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 63d9362

Browse files
authored
refactor(audit-bot): use tesla for http request (#448)
1 parent e9af42e commit 63d9362

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lib/helper/audit_bot.ex

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,16 @@ defmodule Helper.AuditBot do
1818
audit_failed_reason: ""
1919
}
2020
"""
21+
use Tesla, only: [:post]
2122
import Helper.Utils, only: [get_config: 2]
2223

24+
@timeout_limit 4000
25+
26+
plug(Tesla.Middleware.Headers, [{"Content-Type", "application/x-www-form-urlencoded"}])
27+
plug(Tesla.Middleware.Retry, delay: 300, max_retries: 3)
28+
plug(Tesla.Middleware.Timeout, timeout: @timeout_limit)
29+
plug(Tesla.Middleware.FormUrlencoded)
30+
2331
# conclusionType === 1
2432
@conclusionOK 1
2533
@conclusionMaybe 3
@@ -35,26 +43,26 @@ defmodule Helper.AuditBot do
3543
{"Content-Type", "application/x-www-form-urlencoded"}
3644
]
3745

38-
text = text |> HtmlSanitizeEx.strip_tags()
46+
query = %{
47+
text: text |> HtmlSanitizeEx.strip_tags()
48+
}
3949

40-
with {:ok, result} <- HTTPoison.post(get_endpoint(), {:form, [text: text]}, headers) do
50+
with {:ok, result} <- post(get_endpoint(), query) do
4151
parse_result(result)
4252
end
4353
end
4454

4555
def analysis_wrong(:text, text) do
46-
headers = [
47-
{"Content-Type", "application/x-www-form-urlencoded"}
48-
]
49-
50-
text = text |> HtmlSanitizeEx.strip_tags()
56+
query = %{
57+
text: text |> HtmlSanitizeEx.strip_tags()
58+
}
5159

52-
with {:ok, result} <- HTTPoison.post(@wrong_endpoint, {:form, [text: text]}, headers) do
60+
with {:ok, result} <- post(@wrong_endpoint, query) do
5361
parse_result(result)
5462
end
5563
end
5664

57-
defp parse_result(%HTTPoison.Response{body: body, status_code: 200}) do
65+
defp parse_result(%Tesla.Env{body: body, status: 200}) do
5866
with {:ok, result} <- Jason.decode(body),
5967
{:ok, result} <- is_request_ok?(result) do
6068
conclusion = result["conclusionType"]

0 commit comments

Comments
 (0)