k8s v0.5.2 K8s.Client.HTTPProvider

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

handle_response(resp)

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> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 404}})
{: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"}}
Link to this function

headers(method, opts)

Generates HTTP headers from K8s.Conn.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.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"}]