Packages
k8s
2.2.0
2.8.0
2.7.0
2.6.2
2.6.1
2.6.0
2.5.0
2.4.2
2.4.1
2.4.0
2.3.0
2.2.0
2.1.1
2.1.0
2.0.3
2.0.2
2.0.1
2.0.0
2.0.0-rc.6
2.0.0-rc.5
2.0.0-rc.4
2.0.0-rc.3
2.0.0-rc.2
2.0.0-rc.1
2.0.0-rc.0
1.2.1
1.2.0
1.1.10
1.1.9
1.1.8
1.1.7
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-rc1
0.5.2
0.5.1
0.5.0
0.5.0-rc.2
0.5.0-rc.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
retired
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.3
Kubernetes API Client for Elixir
Current section
Files
Jump to
Current section
Files
lib/k8s/client/dynamic_http_provider.ex
defmodule K8s.Client.DynamicHTTPProvider do
@moduledoc """
Allows for registration of `K8s.Client.Provider` handlers per-process.
Used internally by the test suite for testing/mocking Kubernetes responses.
"""
use GenServer
@behaviour K8s.Client.Provider
@doc "Starts this provider."
@spec start_link(any) :: GenServer.on_start()
def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
@doc "Locate the handler module for this process or any ancestor"
@spec locate(pid) :: module() | function() | nil
def locate(nil), do: nil
def locate(pid) do
case GenServer.call(__MODULE__, {:locate, pid}) do
nil ->
pid
|> Process.info(:links)
|> elem(1)
|> List.first()
|> locate()
handler ->
handler
end
end
@doc "List all registered handlers"
@spec list :: map()
def list, do: GenServer.call(__MODULE__, :list)
@doc "Register the handler mdoule for this process"
@spec register(pid(), module() | function()) :: map()
def register(this_pid, module_or_function) do
GenServer.call(__MODULE__, {:register, this_pid, module_or_function})
end
@doc """
Dispatch `request/5` to the module registered in the current process or any ancestor.
"""
@impl true
def request(method, url, body, headers, opts) do
locate_and_apply(:request, [method, url, body, headers, opts])
end
@doc """
Dispatch `stream_to/6` to the module registered in the current process or any ancestor.
"""
@impl true
def stream(method, url, body, headers, opts) do
locate_and_apply(:stream, [method, url, body, headers, opts])
end
@doc """
Dispatch `stream_to/6` to the module registered in the current process or any ancestor.
"""
@impl true
def stream_to(method, url, body, headers, opts, stream_to) do
locate_and_apply(:stream_to, [method, url, body, headers, opts, stream_to])
end
@doc """
Dispatch `request/5` to the module registered in the current process or any ancestor.
"""
@impl true
def websocket_request(url, headers, opts) do
locate_and_apply(:websocket_request, [url, headers, opts])
end
@doc """
Dispatch `request/5` to the module registered in the current process or any ancestor.
"""
@impl true
def websocket_stream(url, headers, opts) do
locate_and_apply(:websocket_stream, [url, headers, opts])
end
@doc """
Dispatch `request/5` to the module registered in the current process or any ancestor.
"""
@impl true
def websocket_stream_to(url, headers, opts, stream_to) do
locate_and_apply(:websocket_stream_to, [url, headers, opts, stream_to])
end
@spec locate_and_apply(atom(), list()) :: K8s.Client.Provider.response_t()
defp locate_and_apply(func, args) do
case locate(self()) do
nil ->
raise "No handler module registered for process #{inspect(self())} or parent."
module when is_atom(module) ->
apply(module, func, args)
callback when is_function(callback) ->
apply(callback, args)
end
end
@impl true
def init(:ok) do
{:ok, %{}}
end
@impl true
def handle_call(:list, _from, state) do
{:reply, state, state}
end
@impl true
def handle_call({:locate, this_pid}, _from, pids) do
{:reply, Map.get(pids, this_pid), pids}
end
@impl true
def handle_call({:register, this_pid, module}, _from, state) do
new_state = Map.put(state, this_pid, module)
{:reply, new_state, new_state}
end
end