Skip to content

Commit a82be7d

Browse files
committed
Change HTTPoison to Req
1 parent d1efd63 commit a82be7d

File tree

14 files changed

+61
-41
lines changed

14 files changed

+61
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $ mix dockers.build elixir
6868
$ mix dockers.pull # all
6969
$ mix dockers.pull elixir
7070

71-
$ mix issues.upload # Upsert issues by name in db
71+
$ mix asserts.upload # Pulls from battle_asserts all issues and upserts into DB
7272

7373
#If you use docker in dev env, run commands in make compose-bash
7474
```

make-compose.mk

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ASSERTS_PATH = "tmp/battle_asserts"
1+
ASSERTS_PATH = "/tmp/battle_asserts"
22

33
compose:
44
docker compose up app
@@ -69,11 +69,11 @@ compose-stop:
6969
compose-logs:
7070
docker compose logs -f --tail=100
7171

72-
compose-upload-asserts:
73-
rm -rf $(ASSERTS_PATH)
74-
git clone "https://github.com/hexlet-codebattle/battle_asserts.git" $(ASSERTS_PATH)
75-
cd $(ASSERTS_PATH) && make generate-from-docker
76-
docker compose run --rm --name codebattle_app -v $(CURDIR)/tmp:/app/tmp app mix issues.upload $(ASSERTS_PATH)/issues
72+
compose-compile:
73+
docker compose run --rm --name codebattle_app app mix compile
74+
75+
compose-upload-battle-asserts:
76+
docker compose run --rm --name codebattle_app app mix asserts.upload
7777

7878
compose-build-dockers:
7979
docker compose run --rm --name codebattle_app app mix dockers.build ${lang}

services/app/apps/codebattle/lib/codebattle/asserts/executor/remote.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ defmodule Codebattle.AssertsService.Executor.Remote do
3333
headers = [{"content-type", "application/json"}, {"x-auth-key", api_key()}]
3434
body = Jason.encode!(params)
3535

36-
case HTTPoison.post("#{runner_url()}/api/v1/generate", body, headers,
36+
case Req.post("#{runner_url()}/api/v1/generate", body: body, headers: headers,
3737
timeout: 30_000,
3838
recv_timeout: 30_000
3939
) do
40-
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
40+
{:ok, %Req.Response{status: 200, body: body}} ->
4141
AtomizedMap.load(body)
4242

43-
{:ok, %HTTPoison.Response{body: body}} ->
43+
{:ok, %Req.Response{body: body}} ->
4444
Logger.error("RemoteExecutor failure: #{inspect(body)}")
4545
{:error, %{base: "RemoteExecutor failure: #{inspect(body)}"}}
4646

47-
{:error, %HTTPoison.Error{reason: reason}} ->
47+
{:error, reason} ->
4848
Logger.error("RemoteExecutor failure: #{inspect(reason)}")
4949
{:error, "RemoteExecutor failure: #{inspect(reason)}"}
5050
end

services/app/apps/codebattle/lib/codebattle/auth/discord.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Codebattle.Auth.Discord do
1010
@http_client Application.compile_env(:codebattle, :discord_oauth_client)
1111

1212
@doc """
13-
`http_client/0` injects a TestDouble of HTTPoison in Test
13+
`http_client/0` injects a TestDouble of Req in Test
1414
"""
1515
def http_client, do: @http_client
1616

services/app/apps/codebattle/lib/codebattle/auth/discord_mock.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Codebattle.Auth.DiscordMock do
88
"""
99

1010
@doc """
11-
`get/3` stubs the HTTPoison get! function when parameters match test vars.
11+
`get/3` stubs the Req get! function when parameters match test vars.
1212
"""
1313
@valid_body %{
1414
"accent_color" => nil,
@@ -75,7 +75,7 @@ defmodule Codebattle.Auth.DiscordMock do
7575
end
7676

7777
@doc """
78-
`post/3` stubs the HTTPoison post! function when parameters match test vars.
78+
`post/3` stubs the Req post! function when parameters match test vars.
7979
"""
8080
def post!(url, body, headers \\ [], options \\ [])
8181

services/app/apps/codebattle/lib/codebattle/auth/firebase_user.ex

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,19 @@ defmodule Codebattle.Auth.User.FirebaseUser do
5252
end
5353

5454
defp create_in_firebase(%{email: email, password: password}) do
55-
case HTTPoison.post(
55+
case Req.post(
5656
"#{firebase_url()}:signUp?key=#{api_key()}",
57-
Jason.encode!(%{email: email, password: password})
57+
json: %{email: email, password: password}
5858
) do
59-
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
59+
{:ok, %Req.Response{status: 200, body: body}} ->
6060
firebase_uid =
6161
body
6262
|> Jason.decode!()
6363
|> Map.get("localId")
6464

6565
{:ok, firebase_uid}
6666

67-
{:ok, %HTTPoison.Response{status_code: 400, body: body}} ->
67+
{:ok, %Req.Response{status: 400, body: body}} ->
6868
error_message =
6969
body
7070
|> Jason.decode!()
@@ -73,28 +73,28 @@ defmodule Codebattle.Auth.User.FirebaseUser do
7373

7474
{:error, %{base: error_message}}
7575

76-
{:ok, %HTTPoison.Response{body: body}} ->
76+
{:ok, %Req.Response{body: body}} ->
7777
{:error, %{base: "Something went wrong, pls, try again later. #{inspect(body)}"}}
7878

79-
{:error, %HTTPoison.Error{reason: reason}} ->
79+
{:error, reason} ->
8080
{:error, %{base: "Something went wrong, pls, try again later. #{inspect(reason)}"}}
8181
end
8282
end
8383

8484
defp find_in_firebase(%{email: email, password: password}) do
85-
case HTTPoison.post(
85+
case Req.post(
8686
"#{firebase_url()}:signInWithPassword?key=#{api_key()}",
87-
Jason.encode!(%{email: email, password: password, returnSecureToken: true})
87+
json: %{email: email, password: password, returnSecureToken: true}
8888
) do
89-
{:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
89+
{:ok, %Req.Response{status: 200, body: body}} ->
9090
firebase_uid =
9191
body
9292
|> Jason.decode!()
9393
|> Map.get("localId")
9494

9595
{:ok, firebase_uid}
9696

97-
{:ok, %HTTPoison.Response{status_code: 400, body: body}} ->
97+
{:ok, %Req.Response{status: 400, body: body}} ->
9898
error_message =
9999
body
100100
|> Jason.decode!()
@@ -103,23 +103,23 @@ defmodule Codebattle.Auth.User.FirebaseUser do
103103

104104
{:error, %{base: error_message}}
105105

106-
{:ok, %HTTPoison.Response{body: body}} ->
106+
{:ok, %Req.Response{body: body}} ->
107107
{:error, %{base: "Something went wrong, pls, try again later. #{inspect(body)}"}}
108108

109-
{:error, %HTTPoison.Error{reason: reason}} ->
109+
{:error, reason} ->
110110
{:error, %{base: "Something went wrong, pls, try again later. #{inspect(reason)}"}}
111111
end
112112
end
113113

114114
defp reset_in_firebase(%{email: email}) do
115-
case HTTPoison.post(
115+
case Req.post(
116116
"#{firebase_url()}:sendOobCode?key=#{api_key()}",
117-
Jason.encode!(%{email: email, requestType: "PASSWORD_RESET"})
117+
json: %{email: email, requestType: "PASSWORD_RESET"}
118118
) do
119-
{:ok, %HTTPoison.Response{status_code: 200}} ->
119+
{:ok, %Req.Response{status: 200}} ->
120120
:ok
121121

122-
{:ok, %HTTPoison.Response{status_code: 400, body: body}} ->
122+
{:ok, %Req.Response{status: 400, body: body}} ->
123123
error_message =
124124
body
125125
|> Jason.decode!()
@@ -128,11 +128,11 @@ defmodule Codebattle.Auth.User.FirebaseUser do
128128

129129
{:error, %{base: error_message}}
130130

131-
{:ok, %HTTPoison.Response{body: body}} ->
131+
{:ok, %Req.Response{body: body}} ->
132132
Logger.error(inspect(body))
133133
{:error, %{base: "Something went wrong, pls, try again later. #{inspect(body)}"}}
134134

135-
{:error, %HTTPoison.Error{reason: reason}} ->
135+
{:error, reason} ->
136136
Logger.error(inspect(reason))
137137
{:error, %{base: "Something went wrong, pls, try again later. #{inspect(reason)}"}}
138138
end

services/app/apps/codebattle/lib/codebattle/auth/github.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Codebattle.Auth.Github do
1010
@http_client Application.compile_env(:codebattle, :github_oauth_client)
1111

1212
@doc """
13-
`http_client/0` injects a TestDouble of HTTPoison in Test
13+
`http_client/0` injects a TestDouble of Req in Test
1414
"""
1515
def http_client, do: @http_client
1616

services/app/apps/codebattle/lib/codebattle/auth/github_mock.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Codebattle.Auth.GithubMock do
88
"""
99

1010
@doc """
11-
`get/3` stubs the HTTPoison get! function when parameters match test vars.
11+
`get/3` stubs the Req get! function when parameters match test vars.
1212
"""
1313
@valid_body %{
1414
access_token: "12345",
@@ -84,7 +84,7 @@ defmodule Codebattle.Auth.GithubMock do
8484
end
8585

8686
@doc """
87-
`post/3` stubs the HTTPoison post! function when parameters match test vars.
87+
`post/3` stubs the Req post! function when parameters match test vars.
8888
"""
8989
def post!(url, body, headers \\ [], options \\ [])
9090

services/app/apps/codebattle/lib/codebattle/tasks_importer.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ defmodule Codebattle.TasksImporter do
1818
GenServer.cast(__MODULE__, :run)
1919
end
2020

21+
def run_sync do
22+
fetch_issues() |> upsert()
23+
end
24+
2125
# SERVER
2226
def init(state) do
2327
Logger.debug("Start Tasks Importer")
@@ -36,11 +40,14 @@ defmodule Codebattle.TasksImporter do
3640
{:noreply, state}
3741
end
3842

43+
3944
def fetch_issues do
4045
File.rm_rf(@tmp_basedir)
4146
File.mkdir_p!(@tmp_basedir)
4247
dir_path = Temp.mkdir!(%{basedir: @tmp_basedir, prefix: to_string(:rand.uniform(10_000_000))})
43-
response = HTTPoison.get!(@issues_link, %{}, follow_redirect: true, timeout: 15_000)
48+
49+
response = Req.get!(@issues_link) |> dbg()
50+
4451
file_name = Path.join(dir_path, "issues.tar.gz")
4552
File.write!(file_name, response.body)
4653

services/app/apps/codebattle/lib/mix/get_contributors.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ defmodule Mix.Tasks.GetContributors do
1515
}
1616

1717
def run(_) do
18-
{:ok, _started} = Application.ensure_all_started(:httpoison)
18+
{:ok, _started} = Application.ensure_all_started(:req)
1919

2020
@repos
2121
|> Enum.each(fn {repo_name, url} ->
2222
content =
2323
url
24-
|> HTTPoison.get!()
24+
|> Req.get!()
2525
|> Map.get(:body)
2626
|> Jason.decode!()
2727
|> Enum.filter(fn params -> params["type"] == "User" end)

0 commit comments

Comments
 (0)