-
Notifications
You must be signed in to change notification settings - Fork 19
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
experimenting with docker api #133
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
# NOTE: This file is auto generated by OpenAPI Generator 7.0.1 (https://openapi-generator.tech). | ||
# Do not edit this file manually. | ||
|
||
defmodule Testcontainers.Docker.RequestBuilder do | ||
@moduledoc """ | ||
Helper functions for building Tesla requests | ||
""" | ||
|
||
@doc """ | ||
Specify the request `method` when building a request. | ||
|
||
Does not override the `method` if one has already been specified. | ||
|
||
### Parameters | ||
|
||
- `request` (Map) - Collected request options | ||
- `method` (atom) - Request method | ||
|
||
### Returns | ||
|
||
Map | ||
""" | ||
@spec method(map(), atom()) :: map() | ||
def method(request, method) do | ||
Map.put_new(request, :method, method) | ||
end | ||
|
||
@doc """ | ||
Specify the request URL when building a request. | ||
|
||
Does not override the `url` if one has already been specified. | ||
|
||
### Parameters | ||
|
||
- `request` (Map) - Collected request options | ||
- `url` (String) - Request URL | ||
|
||
### Returns | ||
|
||
Map | ||
""" | ||
@spec url(map(), String.t()) :: map() | ||
def url(request, url) do | ||
Map.put_new(request, :url, url) | ||
end | ||
|
||
@doc """ | ||
Add optional parameters to the request. | ||
|
||
### Parameters | ||
|
||
- `request` (Map) - Collected request options | ||
- `definitions` (Map) - Map of parameter name to parameter location. | ||
- `options` (KeywordList) - The provided optional parameters | ||
|
||
### Returns | ||
|
||
Map | ||
""" | ||
@spec add_optional_params(map(), %{optional(atom) => atom()}, keyword()) :: map() | ||
def add_optional_params(request, _, []), do: request | ||
|
||
def add_optional_params(request, definitions, [{key, value} | tail]) do | ||
case definitions do | ||
%{^key => location} -> | ||
request | ||
|> add_param(location, key, value) | ||
|> add_optional_params(definitions, tail) | ||
|
||
_ -> | ||
add_optional_params(request, definitions, tail) | ||
end | ||
end | ||
|
||
@doc """ | ||
Add non-optional parameters to the request. | ||
|
||
### Parameters | ||
|
||
- `request` (Map) - Collected request options | ||
- `location` (atom) - Where to put the parameter | ||
- `key` (atom) - The name of the parameter | ||
- `value` (any) - The value of the parameter | ||
|
||
### Returns | ||
|
||
Map | ||
""" | ||
@spec add_param(map(), atom(), atom(), any()) :: map() | ||
def add_param(request, :body, :body, value), do: Map.put(request, :body, value) | ||
|
||
def add_param(request, :body, key, value) do | ||
request | ||
|> Map.put_new_lazy(:body, &Tesla.Multipart.new/0) | ||
|> Map.update!(:body, fn multipart -> | ||
Tesla.Multipart.add_field( | ||
multipart, | ||
key, | ||
Jason.encode!(value), | ||
headers: [{:"Content-Type", "application/json"}] | ||
) | ||
end) | ||
end | ||
|
||
def add_param(request, :headers, key, value) do | ||
headers = | ||
request | ||
|> Map.get(:headers, []) | ||
|> List.keystore(key, 0, {key, value}) | ||
|
||
Map.put(request, :headers, headers) | ||
end | ||
|
||
def add_param(request, :file, name, path) do | ||
request | ||
|> Map.put_new_lazy(:body, &Tesla.Multipart.new/0) | ||
|> Map.update!(:body, &Tesla.Multipart.add_file(&1, path, name: name)) | ||
end | ||
|
||
def add_param(request, :form, name, value) do | ||
Map.update(request, :body, %{name => value}, &Map.put(&1, name, value)) | ||
end | ||
|
||
def add_param(request, location, key, value) do | ||
Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}])) | ||
end | ||
|
||
@doc """ | ||
This function ensures that the `body` parameter is always set. | ||
|
||
When using Tesla with the `httpc` adapter (the default adapter), there is a | ||
bug where POST, PATCH and PUT requests will fail if the body is empty. | ||
|
||
### Parameters | ||
|
||
- `request` (Map) - Collected request options | ||
|
||
### Returns | ||
|
||
Map | ||
""" | ||
@spec ensure_body(map()) :: map() | ||
def ensure_body(%{body: nil} = request) do | ||
%{request | body: ""} | ||
end | ||
|
||
def ensure_body(request) do | ||
Map.put_new(request, :body, "") | ||
end | ||
|
||
@type status_code :: :default | 100..599 | ||
@type response_mapping :: [{status_code, false | %{} | module()}] | ||
|
||
@doc """ | ||
Evaluate the response from a Tesla request. | ||
Decode the response for a Tesla request. | ||
|
||
### Parameters | ||
|
||
- `result` (Tesla.Env.result()): The response from Tesla.request/2. | ||
- `mapping` ([{http_status, struct}]): The mapping for status to struct for decoding. | ||
|
||
### Returns | ||
|
||
- `{:ok, struct}` or `{:ok, Tesla.Env.t()}` on success | ||
- `{:error, term}` on failure | ||
""" | ||
@spec evaluate_response(Tesla.Env.result(), response_mapping) :: | ||
{:ok, struct()} | Tesla.Env.result() | ||
def evaluate_response({:ok, %Tesla.Env{} = env}, mapping) do | ||
resolve_mapping(env, mapping, nil) | ||
end | ||
|
||
def evaluate_response({:error, _} = error, _), do: error | ||
|
||
defp resolve_mapping(%Tesla.Env{status: status} = env, [{mapping_status, struct} | _], _) | ||
when status == mapping_status do | ||
decode(env, struct) | ||
end | ||
|
||
defp resolve_mapping(env, [{:default, struct} | tail], _), | ||
do: resolve_mapping(env, tail, struct) | ||
|
||
defp resolve_mapping(env, [_ | tail], struct), do: resolve_mapping(env, tail, struct) | ||
|
||
defp resolve_mapping(env, [], nil), do: {:error, env} | ||
|
||
defp resolve_mapping(env, [], struct), do: decode(env, struct) | ||
|
||
defp decode(%Tesla.Env{} = env, false), do: {:ok, env} | ||
|
||
defp decode(%Tesla.Env{body: body}, %{}) do | ||
DockerEngineAPI.Deserializer.jason_decode(body) | ||
end | ||
|
||
defp decode(%Tesla.Env{body: body}, module) do | ||
DockerEngineAPI.Deserializer.jason_decode(body, module) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# NOTE: This file is auto generated by OpenAPI Generator 7.0.1 (https://openapi-generator.tech). | ||
# Do not edit this file manually. | ||
|
||
defmodule Testcontainers.Docker.Connection do | ||
@moduledoc """ | ||
Handle Tesla connections for DockerEngineAPI. | ||
""" | ||
|
||
@default_options [ | ||
base_url: "http://localhost/v1.43", | ||
adapter: Tesla.Adapter.Hackney | ||
] | ||
|
||
@typedoc """ | ||
The list of options that can be passed to new/1. | ||
|
||
- `base_url`: Overrides the base URL on a per-client basis. | ||
- `user_agent`: Overrides the User-Agent header. | ||
""" | ||
@type options :: [ | ||
{:base_url, String.t()}, | ||
{:user_agent, String.t()} | ||
] | ||
|
||
@doc "Forward requests to Tesla." | ||
@spec request(Tesla.Client.t(), [Tesla.option()]) :: Tesla.Env.result() | ||
defdelegate request(client, options), to: Tesla | ||
|
||
@doc """ | ||
Configure a client that may have authentication. | ||
|
||
### Parameters | ||
|
||
- `options`: a keyword list of OpenAPIPetstore.Connection.options. | ||
|
||
### Returns | ||
|
||
Tesla.Env.client | ||
""" | ||
@spec new(options) :: Tesla.Env.client() | ||
|
||
def new(options \\ []) when is_list(options) do | ||
options = @default_options |> Keyword.merge(options) | ||
|
||
options | ||
|> Keyword.merge(options) | ||
|> middleware() | ||
|> Tesla.client(adapter(options)) | ||
end | ||
|
||
@doc """ | ||
Returns fully configured middleware for passing to Tesla.client/2. | ||
""" | ||
@spec middleware(options) :: [Tesla.Client.middleware()] | ||
def middleware(options \\ []) do | ||
base_url = Keyword.get(options, :base_url) | ||
|
||
tesla_options = get_tesla_options() | ||
middleware = Keyword.get(tesla_options, :middleware, []) | ||
json_engine = Keyword.get(tesla_options, :json, Jason) | ||
|
||
user_agent = | ||
Keyword.get( | ||
options, | ||
:user_agent, | ||
Keyword.get( | ||
tesla_options, | ||
:user_agent, | ||
"openapi-generator - DockerEngineAPI 1.43 - elixir" | ||
) | ||
) | ||
|
||
[ | ||
{Tesla.Middleware.BaseUrl, base_url}, | ||
{Tesla.Middleware.Headers, [{"user-agent", user_agent}]}, | ||
{Tesla.Middleware.EncodeJson, engine: json_engine} | ||
| middleware | ||
] | ||
end | ||
|
||
def get_tesla_options, do: Application.get_env(:tesla, __MODULE__, []) | ||
|
||
@doc """ | ||
Returns the default adapter for this API. | ||
""" | ||
def adapter(options \\ []) do | ||
case Keyword.get( | ||
options, | ||
:adapter, | ||
Keyword.get(get_tesla_options(), :adapter, nil) | ||
) do | ||
Tesla.Adapter.Hackney -> | ||
{Tesla.Adapter.Hackney, [recv_timeout: Keyword.get(options, :recv_timeout, 1_000)]} | ||
|
||
other -> | ||
other | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the current experimentation