Skip to content

Commit

Permalink
Merge 15dddf8 into 331b5ed
Browse files Browse the repository at this point in the history
  • Loading branch information
coryodaniel committed Aug 14, 2019
2 parents 331b5ed + 15dddf8 commit 5b4cb22
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Added
- text/plain response handling

## [0.3.0] - 2019-07-29

### Added
Expand Down
20 changes: 11 additions & 9 deletions lib/k8s/client.ex
Expand Up @@ -437,13 +437,14 @@ defmodule K8s.Client do
"""
@spec patch(map()) :: Operation.t()
def patch(%{} = resource), do: Operation.build(:patch, resource)

@doc """
Returns a `PATCH` operation to patch the given subresource given a resource's details and a subresource map.
"""
"""
@spec patch(binary, binary | atom, Keyword.t(), map()) :: Operation.t()
def patch(api_version, kind, path_params, subresource), do: Operation.build(:patch, api_version, kind, path_params, subresource)

def patch(api_version, kind, path_params, subresource),
do: Operation.build(:patch, api_version, kind, path_params, subresource)

@doc """
Returns a `PATCH` operation to patch the given subresource given a resource map and a subresource map.
"""
Expand All @@ -463,7 +464,7 @@ defmodule K8s.Client do
[namespace: ns, name: name],
subresource
)
end
end

@doc """
Returns a `PUT` operation to replace/update the given resource.
Expand Down Expand Up @@ -572,13 +573,14 @@ defmodule K8s.Client do
...> }
...> K8s.Client.update("apps/v1", "deployments/scale", [namespace: "default", name: "nginx"], scale)
%K8s.Operation{api_version: "apps/v1", data: %{"apiVersion" => "apps/v1beta1", "kind" => "Scale", "metadata" => %{"name" => "nginx", "namespace" => "default"}, "spec" => %{"replicas" => 3}}, method: :put, name: "deployments/scale", path_params: [namespace: "default", name: "nginx"], verb: :update}
"""
"""
@spec update(binary, binary | atom, Keyword.t(), map()) :: Operation.t()
def update(api_version, kind, path_params, subresource), do: Operation.build(:update, api_version, kind, path_params, subresource)

def update(api_version, kind, path_params, subresource),
do: Operation.build(:update, api_version, kind, path_params, subresource)

@doc """
Returns a `PUT` operation to replace/update the given subresource given a resource map and a subresource map.
Used for updating subresources like `Scale` or `Status`.
## Examples
Expand Down
2 changes: 1 addition & 1 deletion lib/k8s/client/behaviour.ex
Expand Up @@ -14,6 +14,6 @@ defmodule K8s.Client.Behaviour do
{:ok, HTTPoison.Response.t() | HTTPoison.AsyncResponse.t()}
| {:error, HTTPoison.Error.t()}
) ::
{:ok, map() | reference()}
{:ok, list(map()) | map() | reference() | binary() | list(binary())}
| {:error, atom | HTTPoison.Response.t() | HTTPoison.Error.t()}
end
18 changes: 14 additions & 4 deletions lib/k8s/client/http_provider.ex
Expand Up @@ -33,6 +33,12 @@ defmodule K8s.Client.HTTPProvider do
...> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 200, body: body}})
{:ok, %{"foo" => "bar"}}
Parses successful JSON responses:
iex> body = "line 1\\nline 2\\nline 3\\n"
...> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 200, body: body, headers: [{"Content-Type", "text/plain"}]}})
{:ok, "line 1\\nline 2\\nline 3\\n"}
Handles unauthorized responses:
iex> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 401}})
Expand All @@ -57,8 +63,10 @@ defmodule K8s.Client.HTTPProvider do
@impl true
def handle_response(resp) do
case resp do
{:ok, %HTTPoison.Response{status_code: code, body: body}} when code in 200..299 ->
{:ok, decode(body)}
{:ok, %HTTPoison.Response{status_code: code, body: body, headers: headers}}
when code in 200..299 ->
content_type = List.keyfind(headers, "Content-Type", 0)
{:ok, decode(body, content_type)}

{:ok, %HTTPoison.AsyncResponse{id: ref}} ->
{:ok, ref}
Expand Down Expand Up @@ -109,8 +117,10 @@ defmodule K8s.Client.HTTPProvider do
{"Content-Type", "application/json"}
end

@spec decode(binary()) :: list | map | nil
defp decode(body) do
@spec decode(binary(), {binary(), binary()} | nil) :: list | map | nil
defp decode(body, {_, "text/plain"}), do: body

defp decode(body, _default_json_decoder) do
case Jason.decode(body) do
{:ok, data} -> data
{:error, _} -> nil
Expand Down

0 comments on commit 5b4cb22

Please sign in to comment.