Geminex.Utils (geminex v0.1.0)

Utility functions for handling common tasks within the Geminex library.

This module provides helper functions to manage payload transformations, handle HTTP responses, and merge maps with keyword lists.

Summary

Functions

Handles HTTP responses by returning for successful responses or for failures.

Conditionally adds a key-value pair to a map if the value is not nil.

Merges a map with a keyword list, ensuring all keys from the keyword list are converted to strings.

Functions

Link to this function

handle_binary_response(arg)

Link to this function

handle_response(arg)

@spec handle_response({:ok, Tesla.Env.t()} | {:error, any()}) ::
  {:ok, any()} | {:error, any()}

Handles HTTP responses by returning for successful responses or for failures.

Parameters

  • response: The HTTP response tuple returned by the Tesla client.

Returns

  • if the HTTP status is in the 200–299 range.
  • } if the status is outside of the success range.
  • if there is a client error.

Examples

iex> Utils.handle_response({:ok, %Tesla.Env{status: 200, body: %{"result" => "success"}}})
{:ok, %{"result" => "success"}}

iex> Utils.handle_response({:ok, %Tesla.Env{status: 400, body: %{"error" => "invalid_request"}}})
{:error, %{status: 400, body: %{"error" => "invalid_request"}}}

iex> Utils.handle_response({:error, :timeout})
{:error, :timeout}
Link to this function

maybe_put(map, key, value)

@spec maybe_put(map(), any(), any()) :: map()

Conditionally adds a key-value pair to a map if the value is not nil.

Parameters

  • map: The original map to which the key-value pair may be added.
  • key: The key to be added to the map.
  • value: The value to be associated with the key. If this is nil, the key-value pair is not added.

Returns

  • The original map if value is nil, or a new map with the key-value pair added if value is not nil.

Examples

iex> Utils.maybe_put(%{"a" => 1}, "b", 2)
%{"a" => 1, "b" => 2}

iex> Utils.maybe_put(%{"a" => 1}, "b", nil)
%{"a" => 1}
Link to this function

merge_map_with_string_keys(map, keyword_list)

@spec merge_map_with_string_keys(
  map(),
  keyword()
) :: map()

Merges a map with a keyword list, ensuring all keys from the keyword list are converted to strings.

Parameters

  • map: The base map to merge into.
  • keyword_list: A keyword list with atom keys that need to be converted to strings.

Returns

  • A new map with all keys from keyword_list as strings, merged into map.

Examples

iex> Utils.merge_map_with_string_keys(%{"a" => 1, "b" => 2}, [a: 10, c: 3])
%{"a" => 10, "b" => 2, "c" => 3}