Skip to content

Commit

Permalink
use hex_repo to fetch data inside Hex.Repo
Browse files Browse the repository at this point in the history
  • Loading branch information
cgerling committed May 4, 2023
1 parent 538577d commit f61c269
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 32 deletions.
56 changes: 27 additions & 29 deletions lib/hex/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -181,30 +181,39 @@ defmodule Hex.Repo do
defp merge_values(left, _right), do: left

def get_package(repo, package, etag) do
headers = Map.merge(etag_headers(etag), auth_headers(repo))
config =
HTTP.config()
|> put_auth_config(repo)
|> put_etag_config(etag)

HTTP.request(:get, package_url(repo, package), headers, nil)
:mix_hex_repo.get_package(config, package)
|> handle_response()
end

def get_docs(repo, package, version) do
headers = auth_headers(repo)
config =
HTTP.config()
|> put_auth_config(repo)

HTTP.request(:get, docs_url(repo, package, version), headers, nil)
:mix_hex_repo.get_docs(config, package, version)
|> handle_response()
end

def get_tarball(repo, package, version) do
headers = auth_headers(repo)
config =
HTTP.config()
|> put_auth_config(repo)

HTTP.request(:get, tarball_url(repo, package, version), headers, nil)
:mix_hex_repo.get_tarball(config, package, version)
|> handle_response()
end

def get_public_key(repo) do
headers = auth_headers(repo)
config =
HTTP.config()
|> put_auth_config(repo)

HTTP.request(:get, public_key_url(repo), headers, nil)
:mix_hex_repo.get_public_key(config)
|> handle_response()
end

Expand Down Expand Up @@ -242,38 +251,27 @@ defmodule Hex.Repo do
|> version_latest()
end

defp package_url(repo, package) do
config = get_repo(repo)
config.url <> "/packages/#{URI.encode(package)}"
end

defp docs_url(repo, package, version) do
config = get_repo(repo)
config.url <> "/docs/#{URI.encode(package)}-#{URI.encode(version)}.tar.gz"
end

def tarball_url(repo, package, version) do
config = get_repo(repo)
config.url <> "/tarballs/#{URI.encode(package)}-#{URI.encode(version)}.tar"
end

defp public_key_url(repo), do: repo.url <> "/public_key"
defp put_auth_config(config, repo) when is_binary(repo) or is_nil(repo) do
repo = get_repo(repo)

defp etag_headers(nil), do: %{}
defp etag_headers(etag), do: %{"if-none-match" => etag}
put_auth_config(config, repo)
end

defp auth_headers(repo) when is_binary(repo) or repo == nil do
repo
|> get_repo()
|> auth_headers()
defp put_auth_config(config, %{trusted: true} = repo) do
%{config | repo_key: repo.auth_key, repo_url: repo.url}
end

defp auth_headers(%{trusted: true, auth_key: key}) when is_binary(key) do
%{"authorization" => key}
defp put_auth_config(config, %{trusted: _}) do
config
end

defp auth_headers(%{trusted: _, auth_key: _}) do
%{}
defp put_etag_config(config, etag) do
%{config | http_etag: etag}
end

defp parse_csv(body) do
Expand Down
14 changes: 11 additions & 3 deletions test/hex/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ defmodule Hex.RepoTest do

@private_key File.read!(Path.join(__DIR__, "../fixtures/test_priv.pem"))

test "get_package" do
test "get_package/3" do
assert {:ok, {200, _, _}} = Hex.Repo.get_package("hexpm", "postgrex", "")

assert_raise Mix.Error, ~r"Unknown repository \"bad\"", fn ->
Hex.Repo.get_package("bad", "postgrex", "")
end
end

test "get_tarball/3" do
assert {:ok, {200, _, _}} = Hex.Repo.get_tarball("hexpm", "postgrex", "0.2.1")

assert_raise Mix.Error, ~r"Unknown repository \"bad\"", fn ->
Hex.Repo.get_tarball("bad", "postgrex", "0.2.1")
end
end

test "verify signature" do
message = :mix_hex_registry.sign_protobuf("payload", @private_key)
assert Hex.Repo.verify(message, "hexpm") == "payload"
Expand Down Expand Up @@ -69,7 +77,7 @@ defmodule Hex.RepoTest do
Plug.Conn.resp(conn, 200, hexpm.public_key)

"/not_found/public_key" ->
assert Plug.Conn.get_req_header(conn, "authorization") == []
assert Plug.Conn.get_req_header(conn, "authorization") == ["key"]
Plug.Conn.resp(conn, 404, "not found")
end
end)
Expand All @@ -78,7 +86,7 @@ defmodule Hex.RepoTest do
assert {:ok, {200, public_key, _}} = Hex.Repo.get_public_key(config)
assert public_key == hexpm.public_key

config = %{url: "http://localhost:#{bypass.port}/not_found", auth_key: "key", trusted: false}
config = %{url: "http://localhost:#{bypass.port}/not_found", auth_key: "key", trusted: true}
assert {:ok, {404, "not found", _}} = Hex.Repo.get_public_key(config)
end

Expand Down

0 comments on commit f61c269

Please sign in to comment.