ExMtnMomo.Util (ExMtnMomo v0.1.2)
View SourceProvides 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
Creates a Basic Authentication header value from a username and password.
Parameters
username
- The username for authenticationpassword
- 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"
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=="
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=="
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"
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
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
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
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
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
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
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
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
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
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"
Sends a GET request to the specified URL.
Parameters
url
- The URL to send the request toattrs
- Query parameters to append to the URLheader
- HTTP headers to include with the request
Returns
{:ok, response_body}
on success{:error, error_message}
on failure
Sends a POST request to the specified URL.
Parameters
url
- The URL to send the request toattrs
- Request body payloadheaders
- HTTP headers to include with the request
Returns
{:ok, response_body}
on success{:error, error_message}
on failure
Sends a PUT request to the specified URL.
Parameters
url
- The URL to send the request toattrs
- Request body payloadencrypt_api_key
- API key for authentication
Returns
{:ok, response_body}
on success{:error, error_message}
on failure