-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored Base to use a middleware-based approach to handling requests. This is still a WIP. * [ ] Cluster middleware registry * [ ] Middleware error handling
- Loading branch information
1 parent
1d2fb37
commit bfb4d52
Showing
10 changed files
with
150 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule K8s.Middleware.Request.EncodeBody do | ||
@moduledoc """ | ||
Naive JSON body encoder. | ||
Encodes JSON payloads when given an modifiying HTTP verb, otherwise returns an empty string. | ||
""" | ||
@behaviour K8s.Middleware.Request | ||
alias K8s.Middleware.Request | ||
|
||
@impl true | ||
def call(%Request{method: method, body: body} = req) do | ||
case encode(body, method) do | ||
{:ok, encoded_body} -> | ||
req = %Request{req | body: encoded_body} | ||
{:ok, req} | ||
|
||
error -> | ||
error | ||
end | ||
end | ||
|
||
@spec encode(any(), atom()) :: {:ok, binary} | {:error, any} | ||
defp encode(body, http_method) when http_method in [:put, :patch, :post], do: Jason.encode(body) | ||
defp encode(_, _), do: {:ok, ""} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
defmodule K8s.Middleware.Request.EncodeBodyTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "encode JSON payloads when given a modifying HTTP verb" do | ||
data = %{"hello" => "world"} | ||
request = %K8s.Middleware.Request{body: data, method: :put} | ||
{:ok, %{body: body}} = K8s.Middleware.Request.EncodeBody.call(request) | ||
|
||
assert body == ~s({"hello":"world"}) | ||
end | ||
|
||
test "returns an empty string if not a modifying verb" do | ||
data = %{"hello" => "world"} | ||
request = %K8s.Middleware.Request{body: data, method: :get} | ||
{:ok, %{body: body}} = K8s.Middleware.Request.EncodeBody.call(request) | ||
|
||
assert body == "" | ||
end | ||
|
||
# TODO: handle error return here type | ||
# test "failure" do | ||
# data = [should: :fail] | ||
# request = %K8s.Middleware.Request{body: data, method: :post} | ||
# {:ok, %{body: body}} = K8s.Middleware.Request.EncodeBody.call(request) | ||
|
||
# assert body == "" | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule K8s.Middleware.Request.InitializeTest do | ||
use ExUnit.Case, async: true | ||
|
||
test "initializes a request headers from K8s.Conn.RequestOptions" do | ||
conn = K8s.Conn.from_file("./test/support/kube-config.yaml") | ||
K8s.Cluster.Registry.add(:test_cluster, conn) | ||
|
||
request = %K8s.Middleware.Request{cluster: :test_cluster} | ||
{:ok, %{headers: headers}} = K8s.Middleware.Request.Initialize.call(request) | ||
|
||
assert headers == [{"Accept", "application/json"}, {"Content-Type", "application/json"}] | ||
end | ||
|
||
test "initializes a HTTPoison options from K8s.Conn.RequestOptions" do | ||
conn = K8s.Conn.from_file("./test/support/kube-config.yaml") | ||
K8s.Cluster.Registry.add(:test_cluster, conn) | ||
|
||
request = %K8s.Middleware.Request{cluster: :test_cluster} | ||
{:ok, %{opts: opts}} = K8s.Middleware.Request.Initialize.call(request) | ||
|
||
assert Keyword.has_key?(opts, :ssl) | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.