ExMtnMomo.Util (ExMtnMomo v0.1.2)

View Source

Provides utility functions and configuration handling for the ExMtnMomo library.

This module handles:

  • Extracting configuration values with fallbacks
  • Creating authentication headers
  • Sending HTTP requests through the HttpRequest module

Most of these functions are used internally by the library and typically don't need to be called directly by library users.

Summary

Functions

Creates a Basic Authentication header value from a username and password.

Creates a Basic Authentication header value for collection API operations.

Creates a Basic Authentication header value for disbursement API operations.

Extracts the base URL from options or application configuration.

Extracts the collection API key from options or application configuration.

Extracts the collection primary key from options or application configuration.

Extracts the collection secondary key from options or application configuration.

Extracts the collection user ID from options or application configuration.

Extracts the disbursement API key from options or application configuration.

Extracts the disbursement primary key from options or application configuration.

Extracts the disbursement secondary key from options or application configuration.

Extracts the disbursement user ID from options or application configuration.

Extracts the sandbox key from options or application configuration.

Extracts the target environment from options or application configuration.

Sends a GET request to the specified URL.

Sends a POST request to the specified URL.

Sends a PUT request to the specified URL.

Functions

basic_auth(username, password)

Creates a Basic Authentication header value from a username and password.

Parameters

  • username - The username for authentication
  • password - The password or API key for authentication

Returns

  • The Basic Auth header value in the format "Basic {base64-encoded string}"

Examples

iex> ExMtnMomo.Util.basic_auth("user123", "pass456")
"Basic dXNlcjEyMzpwYXNzNDU2"

collection_auth(options \\ [])

Creates a Basic Authentication header value for collection API operations.

Parameters

  • options - Keyword list of options (optional)

Returns

  • The Basic Auth header value for collection operations

Examples

iex> ExMtnMomo.Util.collection_auth()
"Basic dXNlcmlkOmFwaWtleQ=="

disbursement_auth(options \\ [])

Creates a Basic Authentication header value for disbursement API operations.

Parameters

  • options - Keyword list of options (optional)

Returns

  • The Basic Auth header value for disbursement operations

Examples

iex> ExMtnMomo.Util.disbursement_auth()
"Basic dXNlcmlkOmFwaWtleQ=="

extract_base_url(options \\ [])

Extracts the base URL from options or application configuration.

Parameters

  • options - Keyword list of options that may include :base_url (optional)

Returns

  • The base URL for API requests

Examples

iex> ExMtnMomo.Util.extract_base_url()
"https://sandbox.momodeveloper.mtn.com"

iex> ExMtnMomo.Util.extract_base_url(base_url: "https://custom.api.url")
"https://custom.api.url"

extract_collection_api_key(options \\ [])

Extracts the collection API key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :api_key (optional)

Returns

  • The collection API key

extract_collection_primary_key(options \\ [])

Extracts the collection primary key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :primary_key (optional)

Returns

  • The collection primary key

extract_collection_secondary_key(options \\ [])

Extracts the collection secondary key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :secondary_key (optional)

Returns

  • The collection secondary key

extract_collection_user_id(options \\ [])

Extracts the collection user ID from options or application configuration.

Parameters

  • options - Keyword list of options that may include :user_id (optional)

Returns

  • The collection user ID

extract_disbursement_api_key(options \\ [])

Extracts the disbursement API key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :api_key (optional)

Returns

  • The disbursement API key

extract_disbursement_primary_key(options \\ [])

Extracts the disbursement primary key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :primary_key (optional)

Returns

  • The disbursement primary key

extract_disbursement_secondary_key(options \\ [])

Extracts the disbursement secondary key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :secondary_key (optional)

Returns

  • The disbursement secondary key

extract_disbursement_user_id(options \\ [])

Extracts the disbursement user ID from options or application configuration.

Parameters

  • options - Keyword list of options that may include :user_id (optional)

Returns

  • The disbursement user ID

extract_sand_box_key(options \\ [])

Extracts the sandbox key from options or application configuration.

Parameters

  • options - Keyword list of options that may include :sandbox_key (optional)

Returns

  • The sandbox key for authentication

extract_x_target_environment(options \\ [])

Extracts the target environment from options or application configuration.

Parameters

  • options - Keyword list of options that may include :x_target_environment (optional)

Returns

  • The target environment (e.g., "sandbox", "mtnzambia")

Examples

iex> ExMtnMomo.Util.extract_x_target_environment()
"sandbox"

iex> ExMtnMomo.Util.extract_x_target_environment(x_target_environment: "mtnzambia")
"mtnzambia"

send_get(url, attrs, header)

Sends a GET request to the specified URL.

Parameters

  • url - The URL to send the request to
  • attrs - Query parameters to append to the URL
  • header - HTTP headers to include with the request

Returns

  • {:ok, response_body} on success
  • {:error, error_message} on failure

send_post(url, attrs, headers)

Sends a POST request to the specified URL.

Parameters

  • url - The URL to send the request to
  • attrs - Request body payload
  • headers - HTTP headers to include with the request

Returns

  • {:ok, response_body} on success
  • {:error, error_message} on failure

send_put(url, attrs, encrypt_api_key)

Sends a PUT request to the specified URL.

Parameters

  • url - The URL to send the request to
  • attrs - Request body payload
  • encrypt_api_key - API key for authentication

Returns

  • {:ok, response_body} on success
  • {:error, error_message} on failure