Skip to content

Commit

Permalink
added method as arg to HTTPProvider.headers/2
Browse files Browse the repository at this point in the history
  • Loading branch information
coryodaniel committed Jul 3, 2019
1 parent 68e02c2 commit e6964e6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Refactored tests on DynamicHTTPProvider
- Refactored discovery to use `K8s.Cluster.Discovery`
- Set correct content-type for patch operations (https://github.com/coryodaniel/k8s/issues/32)

## [0.2.13] - 2019-06-27

Expand Down
2 changes: 1 addition & 1 deletion lib/k8s/client/behaviour.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule K8s.Client.Behaviour do
@moduledoc "HTTP Request / Response provider behaviour"

@doc "Generate headers for HTTP Requests"
@callback headers(K8s.Conf.RequestOptions.t()) :: list({binary, binary})
@callback headers(atom(), K8s.Conf.RequestOptions.t()) :: list({binary, binary})

@doc "Perform HTTP Requests"
@callback request(atom, binary, binary, keyword, keyword) ::
Expand Down
2 changes: 1 addition & 1 deletion lib/k8s/client/dynamic_http_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule K8s.Client.DynamicHTTPProvider do
@behaviour K8s.Client.Behaviour

@impl true
defdelegate headers(request_options), to: K8s.Client.HTTPProvider
defdelegate headers(method, request_options), to: K8s.Client.HTTPProvider

@impl true
defdelegate handle_response(resp), to: K8s.Client.HTTPProvider
Expand Down
38 changes: 23 additions & 15 deletions lib/k8s/client/http_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule K8s.Client.HTTPProvider do

@impl true
def request(method, url, body, headers, opts) do
content_type = content_type_header(method)
headers = [content_type | headers]
{duration, response} = :timer.tc(HTTPoison, :request, [method, url, body, headers, opts])
measurements = %{duration: duration}
metadata = %{method: method}
Expand All @@ -24,15 +22,6 @@ defmodule K8s.Client.HTTPProvider do
end
end

@spec content_type_header(atom()) :: {binary(), binary()}
defp content_type_header(:patch) do
{"Content-Type", "application/merge-patch+json"}
end

defp content_type_header(_http_method) do
{"Content-Type", "application/json"}
end

@doc """
Handle HTTPoison responses and errors
Expand Down Expand Up @@ -90,15 +79,34 @@ defmodule K8s.Client.HTTPProvider do

@doc """
Generates HTTP headers from `K8s.Conf.RequestOptions`
* Adds `{"Accept", "application/json"}` to all requests.
* Adds `Content-Type` base on HTTP method.
## Example
Sets `Content-Type` to `application/merge-patch+json` for PATCH operations
iex> opts = %K8s.Conf.RequestOptions{headers: [{"Authorization", "Basic AF"}]}
...> K8s.Client.HTTPProvider.headers(:patch, opts)
[{"Accept", "application/json"}, {"Content-Type", "application/merge-patch+json"}, {"Authorization", "Basic AF"}]
Sets `Content-Type` to `application/json` for all other operations
iex> opts = %K8s.Conf.RequestOptions{headers: [{"Authorization", "Basic AF"}]}
...> K8s.Client.HTTPProvider.headers(opts)
[{"Accept", "application/json"}, {"Authorization", "Basic AF"}]
...> K8s.Client.HTTPProvider.headers(:get, opts)
[{"Accept", "application/json"}, {"Content-Type", "application/json"}, {"Authorization", "Basic AF"}]
"""
@impl true
def headers(%RequestOptions{} = opts) do
[{"Accept", "application/json"} | opts.headers]
def headers(method, %RequestOptions{} = opts) do
defaults = [{"Accept", "application/json"}, content_type_header(method)]
defaults ++ opts.headers
end

@spec content_type_header(atom()) :: {binary(), binary()}
defp content_type_header(:patch) do
{"Content-Type", "application/merge-patch+json"}
end

defp content_type_header(_http_method) do
{"Content-Type", "application/json"}
end

@spec decode(binary()) :: list | map | nil
Expand Down
2 changes: 1 addition & 1 deletion lib/k8s/client/runner/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ defmodule K8s.Client.Runner.Base do
{:ok, conf} <- Cluster.conf(cluster_name),
{:ok, request_options} <- RequestOptions.generate(conf),
{:ok, http_body} <- encode(body, operation.method) do
http_headers = K8s.http_provider().headers(request_options)
http_headers = K8s.http_provider().headers(operation.method, request_options)
http_opts = Keyword.merge([ssl: request_options.ssl_options], opts)

K8s.http_provider().request(
Expand Down
2 changes: 1 addition & 1 deletion lib/k8s/cluster/discovery/http_driver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule K8s.Cluster.Discovery.HTTPDriver do
defp get(url, conf, opts) do
case RequestOptions.generate(conf) do
{:ok, request_options} ->
headers = K8s.http_provider().headers(request_options)
headers = K8s.http_provider().headers(:get, request_options)
opts = Keyword.merge([ssl: request_options.ssl_options], opts)

K8s.http_provider().request(:get, url, "", headers, opts)
Expand Down
4 changes: 4 additions & 0 deletions test/k8s/client/http_provider_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule K8s.Client.HTTPProviderTest do
use ExUnit.Case, async: true
doctest K8s.Client.HTTPProvider
end
2 changes: 0 additions & 2 deletions test/k8s/client_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
defmodule K8s.ClientTest do
use ExUnit.Case, async: true
doctest K8s.Client

doctest K8s.Client.HTTPProvider
end

0 comments on commit e6964e6

Please sign in to comment.