View Source K8s.Client.HTTPProvider (k8s v1.1.5)

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

Examples

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

Link to this section Functions

Handle HTTPoison responses and errors

examples

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,  %HTTPoison.Response{body: nil, headers: [], request: nil, request_url: nil, status_code: 401}}

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, %K8s.Client.APIError{message: "namespaces not found", reason: "NotFound"}}

Handles admission hook responses:

iex> body = ~s({"apiVersion":"v1","code":400,"kind":"Status","message":"admission webhook","metadata" :{}, "status":"Failure"})
...> headers = [{"Content-Type", "application/json"}]
...> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 404, body: body, headers: headers}})
{:error, %K8s.Client.APIError{message: "admission webhook", reason: "Failure"}}

Passes through HTTPoison 4xx responses:

iex> K8s.Client.HTTPProvider.handle_response({:ok, %HTTPoison.Response{status_code: 410, body: "Gone"}})
{:error,  %HTTPoison.Response{body: "Gone", headers: [], request: nil, request_url: nil, status_code: 410}}

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 if the header is not set.

examples

Examples

Sets Content-Type to application/json

iex> opts = %K8s.Conn.RequestOptions{headers: [Authorization: "Basic AF"]}
...> K8s.Client.HTTPProvider.headers(opts)
[Accept: "application/json", Authorization: "Basic AF"]
This function is deprecated. Use headers/1 instead.

examples

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"}]