Skip to content

Commit

Permalink
Move data internals knowledge outside HTTPClient (#119)
Browse files Browse the repository at this point in the history
* Update external tests and setup
* Remove deprecated `Mix.Config` module
* Add missing enforced checks for behavior
  • Loading branch information
Strech authored Jun 27, 2024
1 parent d2f2103 commit b11472d
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 33 deletions.
38 changes: 18 additions & 20 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 13,8 @@ jobs:
timeout-minutes: 30
name: Internal Typespecs
env:
OTP: "23.3"
ELIXIR: "1.12"
OTP: "25.3"
ELIXIR: "1.13"
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
Expand Down Expand Up @@ -50,8 50,8 @@ jobs:
runs-on: ubuntu-20.04
name: Code Style
env:
OTP: "23.3"
ELIXIR: "1.12"
OTP: "25.3"
ELIXIR: "1.13"
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
Expand All @@ -78,8 78,8 @@ jobs:
runs-on: ubuntu-20.04
name: Code Formatting
env:
OTP: "23.3"
ELIXIR: "1.12"
OTP: "25.3"
ELIXIR: "1.13"
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
Expand Down Expand Up @@ -107,8 107,8 @@ jobs:
timeout-minutes: 30
name: External Typespecs
env:
OTP: "23.3"
ELIXIR: "1.12"
OTP: "25.3"
ELIXIR: "1.13"
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
Expand Down Expand Up @@ -151,19 151,17 @@ jobs:
# NOTE: We are going to support 4 version from the official list of 5
# https://hexdocs.pm/elixir/compatibility-and-deprecations.html
matrix:
otp: ["24.3"]
elixir: ["1.12"]
otp: ["25.3"]
elixir: ["1.13.4"]
include:
- otp: "24.3"
elixir: "1.12"
- otp: "24.3"
elixir: "1.13.3"
- otp: "25.3"
elixir: "1.13"
- otp: "25.3"
elixir: "1.14"
- otp: "26.0"
elixir: "1.15"
- otp: "26.2"
elixir: "1.14.5"
- otp: "26.2"
elixir: "1.15.8"
- otp: "26.2"
elixir: "1.16.3"
- otp: "27.0"
elixir: "1.17.1"
steps:
- uses: actions/checkout@v2
- uses: erlef/setup-beam@v1
Expand Down
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
use Mix.Config
import Config

config :avrora,
schemas_path: Path.expand("./test/fixtures/schemas"),
Expand Down
4 changes: 2 additions & 2 deletions lib/avrora/http_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 18,9 @@ defmodule Avrora.HTTPClient do
end

@doc false
@spec post(String.t(), String.t(), keyword(String.t())) :: {:ok, map()} | {:error, term()}
@spec post(String.t(), term(), keyword(String.t())) :: {:ok, map()} | {:error, term()}
def post(url, payload, options \\ []) when is_binary(payload) do
with {:ok, body} <- Jason.encode(%{"schema" => payload}),
with {:ok, body} <- Jason.encode(payload),
{:ok, content_type} <- Keyword.fetch(options, :content_type),
{:ok, headers} <- extract_headers(options),
{:ok, ssl_options} <- extract_ssl(options),
Expand Down
3 changes: 3 additions & 0 deletions lib/avrora/storage/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 36,15 @@ defmodule Avrora.Storage.File do
iex> schema.full_name
"io.acme.Payment"
"""
@impl true
def get(key) when is_binary(key) do
with {:ok, body} <- read_schema_file_by_name(key),
do: SchemaEncoder.from_json(body, &read_schema_file_by_name/1)
end

def get(key) when is_integer(key), do: {:error, :unsupported}

@impl true
def put(_key, _value), do: {:error, :unsupported}

defp read_schema_file_by_name(name) do
Expand Down
4 changes: 3 additions & 1 deletion lib/avrora/storage/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 31,7 @@ defmodule Avrora.Storage.Registry do
...> schema.full_name
"io.acme.Payment"
"""
@impl true
def get(key) when is_binary(key) do
with {:ok, schema_name} <- Name.parse(key),
{name, version} <- {schema_name.name, schema_name.version || "latest"},
Expand Down Expand Up @@ -67,9 68,10 @@ defmodule Avrora.Storage.Registry do
...> schema.full_name
"io.acme.Payment"
"""
@impl true
def put(key, value) when is_binary(key) and is_binary(value) do
with {:ok, schema_name} <- Name.parse(key),
{:ok, response} <- http_client_post("subjects/#{schema_name.name}/versions", value),
{:ok, response} <- http_client_post("subjects/#{schema_name.name}/versions", %{schema: value}),
{:ok, id} <- Map.fetch(response, "id"),
{:ok, schema} <- SchemaEncoder.from_json(value) do
unless is_nil(schema_name.version) do
Expand Down
14 changes: 7 additions & 7 deletions test/avrora/storage/registry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 290,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, options ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == json_schema()
assert payload == %{schema: json_schema()}
assert Keyword.fetch!(options, :content_type) == "application/vnd.schemaregistry.v1 json"
assert Keyword.fetch!(options, :ssl_options) == [verify: :verify_none]

Expand All @@ -308,7 308,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, _ ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == json_schema()
assert payload == %{schema: json_schema()}

{:ok, %{"id" => 1}}
end)
Expand All @@ -329,7 329,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, _ ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == ~s({"type":"string"})
assert payload == %{schema: ~s({"type":"string"})}

{:error, schema_incompatible_parsed_error()}
end)
Expand All @@ -343,7 343,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, options ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == json_schema()
assert payload == %{schema: json_schema()}
assert Keyword.fetch!(options, :authorization) == "Basic bG9naW46cGFzc3dvcmQ="

{:ok, %{"id" => 1}}
Expand All @@ -358,7 358,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, options ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == json_schema()
assert payload == %{schema: json_schema()}
assert Keyword.fetch!(options, :user_agent) == "Avrora/0.0.1 Elixir"

{:ok, %{"id" => 1}}
Expand All @@ -374,7 374,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, options ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == json_schema()
assert payload == %{schema: json_schema()}
assert Keyword.fetch!(options, :ssl_options) == [verify: :verify_peer, cacerts: [<<48, 130, 3, 201>>]]

{:ok, %{"id" => 1}}
Expand All @@ -389,7 389,7 @@ defmodule Avrora.Storage.RegistryTest do
Avrora.HTTPClientMock
|> expect(:post, fn url, payload, options ->
assert url == "http://reg.loc/subjects/io.acme.Payment/versions"
assert payload == json_schema()
assert payload == %{schema: json_schema()}
assert Keyword.fetch!(options, :ssl_options) == [verify: :verify_peer, cacertfile: "path/to/file"]

{:ok, %{"id" => 1}}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/lib/interfaces.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 60,7 @@ defmodule Interfaces do

defmodule MyClient do
@moduledoc false
use Avrora.Client
use Avrora.Client, otp_app: :integration
end

alias Interfaces.PrivateClientTest.MyClient
Expand Down
2 changes: 1 addition & 1 deletion test/integration/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,7 @@ defmodule Integration.MixProject do
defp deps do
[
{:avrora, path: "../../"},
{:dialyxir, "~> 1.0.0", runtime: false}
{:dialyxir, "~> 1.1", runtime: false}
]
end
end

0 comments on commit b11472d

Please sign in to comment.