Mmg Python Library

The Mmg Python library provides convenient access to the Mmg APIs from Python.

Table of Contents

Installation

1pip install mobile-money-sdk

Usage

Instantiate and use the client with the following:

1from mmg import MMG
2
3client = MMG(
4 wss_mid="YOUR_WSS_MID",
5 wss_mkey="YOUR_WSS_MKEY",
6 wss_msecret="YOUR_WSS_MSECRET",
7 api_key="YOUR_API_KEY",
8 wss_correlationid="YOUR_WSS_CORRELATIONID",
9 wss_token="YOUR_WSS_TOKEN",
10)
11client.authentication.login_resource_token_api(
12 grant_type="password",
13 api_key="{{x-api-key}}",
14 username="{{x-wss-mid}}",
15 password="{{PASSWORD}}",
16)

Async Client

The SDK also exports an async client so that you can make non-blocking calls to our API. Note that if you are constructing an Async httpx client class to pass into this client, use httpx.AsyncClient() instead of httpx.Client() (e.g. for the httpx_client parameter of this client).

1import asyncio
2
3from mmg import AsyncMMG
4
5client = AsyncMMG(
6 wss_mid="YOUR_WSS_MID",
7 wss_mkey="YOUR_WSS_MKEY",
8 wss_msecret="YOUR_WSS_MSECRET",
9 api_key="YOUR_API_KEY",
10 wss_correlationid="YOUR_WSS_CORRELATIONID",
11 wss_token="YOUR_WSS_TOKEN",
12)
13
14
15async def main() -> None:
16 await client.authentication.login_resource_token_api(
17 grant_type="password",
18 api_key="{{x-api-key}}",
19 username="{{x-wss-mid}}",
20 password="{{PASSWORD}}",
21 )
22
23
24asyncio.run(main())

Exception Handling

When the API returns a non-success status code (4xx or 5xx response), a subclass of the following error will be thrown.

1from mmg.core.api_error import ApiError
2
3try:
4 client.authentication.login_resource_token_api(...)
5except ApiError as e:
6 print(e.status_code)
7 print(e.body)

Advanced

Access Raw Response Data

The SDK provides access to raw response data, including headers, through the .with_raw_response property. The .with_raw_response property returns a “raw” client that can be used to access the .headers and .data attributes.

1from mmg import MMG
2
3client = MMG(
4 ...,
5)
6response = client.authentication.with_raw_response.login_resource_token_api(...)
7print(response.headers) # access the response headers
8print(response.data) # access the underlying object

Retries

The SDK is instrumented with automatic retries with exponential backoff. A request will be retried as long as the request is deemed retryable and the number of retry attempts has not grown larger than the configured retry limit (default: 2).

A request is deemed retryable when any of the following HTTP status codes is returned:

  • 408 (Timeout)
  • 429 (Too Many Requests)
  • 5XX (Internal Server Errors)

Use the max_retries request option to configure this behavior.

1client.authentication.login_resource_token_api(..., request_options={
2 "max_retries": 1
3})

Timeouts

The SDK defaults to a 60 second timeout. You can configure this with a timeout option at the client or request level.

1from mmg import MMG
2
3client = MMG(
4 ...,
5 timeout=20.0,
6)
7
8
9# Override timeout for a specific method
10client.authentication.login_resource_token_api(..., request_options={
11 "timeout_in_seconds": 1
12})

Custom Client

You can override the httpx client to customize it for your use-case. Some common use-cases include support for proxies and transports.

1import httpx
2from mmg import MMG
3
4client = MMG(
5 ...,
6 httpx_client=httpx.Client(
7 proxy="http://my.test.proxy.example.com",
8 transport=httpx.HTTPTransport(local_address="0.0.0.0"),
9 ),
10)