Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added text/plain response handling #37

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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