K8s.Client.HTTPProvider (k8s v1.0.0-rc1) View Source

HTTPoison and Jason based K8s.Client.Provider

Link to this section Summary

Functions

Handle HTTPoison responses and errors

Generates HTTP headers from K8s.Conn.RequestOptions

Link to this section Functions

Link to this function

format_log_message(body, headers)

View Source

Specs

format_log_message(String.t(), [tuple()]) :: any()

Handle HTTPoison responses and errors

Examples

Parses successful JSON responses:

iex> body = ~s({"foo": "bar"})
...> 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}})
{:error, :unauthorized}

Handles not found responses:

iex> body = ~s({"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"namespaces not found","reason":"NotFound","details":{"name":"i-dont-exist","kind":"namespaces"},"code":404})
...> headers = [{"Content-Type", "application/json"}]
...> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 404, body: body, headers: headers}})
{:error, :not_found}

Passes through HTTPoison 4xx responses:

iex> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 410, body: "Gone"}})
{:error, %HTTPoison.Response{status_code: 410, body: "Gone"}}

Passes through HTTPoison error responses:

iex> K8s.Client.HTTPProvider.handle_response({:error, %HTTPoison.Error{reason: "Foo"}})
{:error, %HTTPoison.Error{reason: "Foo"}}

Generates HTTP headers from K8s.Conn.RequestOptions

  • Adds {"Accept", "application/json"} to all requests.
  • Adds Content-Type base on HTTP method.

Examples

Sets Content-Type to application/merge-patch+json for PATCH operations

iex> opts = %K8s.Conn.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.Conn.RequestOptions{headers: [{"Authorization", "Basic AF"}]}
...> K8s.Client.HTTPProvider.headers(:get, opts)
[{"Accept", "application/json"}, {"Content-Type", "application/json"}, {"Authorization", "Basic AF"}]