ExMtnMomo.HttpRequest (ExMtnMomo v0.1.2)

View Source

Provides HTTP request functionality for the ExMtnMomo library.

This module handles the low-level HTTP operations needed to communicate with the MTN Mobile Money API, including:

  • Making POST, PUT, and GET requests
  • Handling responses and errors
  • Parsing JSON responses

This module is used internally by the ExMtnMomo library and typically doesn't need to be used directly by library users.

Summary

Functions

Sends a GET request to the specified URL.

Handles HTTP responses and standardizes the return format.

Creates standard headers for HTTP requests with an authentication token.

Sends a POST request to the specified URL.

Sends a PUT request to the specified URL.

Functions

get(url, attrs \\ %{}, headers \\ [])

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 (optional)
  • headers - Custom headers to include with the request (optional)

Returns

  • The response from the HTTP client (HTTPoison)

Examples

iex> ExMtnMomo.HttpRequest.get("https://api.example.com/endpoint", %{filter: "value"}, [{"X-Custom", "Header"}])
{:ok, %HTTPoison.Response{...}}

handle_response(arg)

Handles HTTP responses and standardizes the return format.

This function processes the raw response from HTTPoison and converts it into a more usable format for the library.

Parameters

  • response - The response tuple from an HTTP request

Returns

  • {:ok, body} - For successful responses, with body parsed from JSON
  • {:error, message} - For error responses, with error message

Response Status Codes

  • 200, 201, 202 - Success responses
  • 400, 401, 405, 408, 409, 422, 500, 503 - Error responses with specific handling

header(key)

Creates standard headers for HTTP requests with an authentication token.

Parameters

  • key - The authentication token or key to include in the Authorization header

Returns

  • A list of HTTP headers with Content-Type, Origin and Authorization

Examples

iex> ExMtnMomo.HttpRequest.header("eyJ0eXAi...")
[
  {"Content-Type", "application/json"},
  {"Origin", "*"},
  {"Authorization", "Bearer eyJ0eXAi..."},
]

post(url, body, headers \\ [])

Sends a POST request to the specified URL.

Parameters

  • url - The URL to send the request to
  • body - The request payload, will be encoded as JSON
  • headers - Custom headers to include with the request (optional)

Returns

  • The response from the HTTP client (HTTPoison)

Examples

iex> ExMtnMomo.HttpRequest.post("https://api.example.com/endpoint", %{key: "value"}, [{"X-Custom", "Header"}])
{:ok, %HTTPoison.Response{...}}

put(url, body, headers \\ [])

Sends a PUT request to the specified URL.

Parameters

  • url - The URL to send the request to
  • body - The request payload, will be encoded as JSON
  • headers - Custom headers to include with the request (optional)

Returns

  • The response from the HTTP client (HTTPoison)

Examples

iex> ExMtnMomo.HttpRequest.put("https://api.example.com/endpoint", %{key: "value"}, [{"X-Custom", "Header"}])
{:ok, %HTTPoison.Response{...}}