Skip to main content

The Official Python library for Reloadly GiftCards APIs

Project description

GiftCards API

The implementation is based on the GiftCards API Docs.

Usage

Create an GiftcardAPI instance by providing the Application details (client id & secret) from the dashboard.

Some key things to keep in mind regarding the Airtime API :

  • The API has 2 environments, SANDBOX (for development & testing) and LIVE. Environment.GIFTCARD is used for the Live environmnent and Environment.GIFTCARD_SANDBOX is for the Sandbox.
  • If neither environment is specified, the SDK defaults to SANDBOX
  • Each environment has a set of credentials (client id & secret) that are different from the other.
    • SANBOX credentials can only be used for SANDBOX environment
    • LIVE credentials can only be used for LIVE environment
  • If not environment is specified the SDK defaults to SANDBOX
  • You MUST supply either the credentials, or an access token in order to call the API

As stated above, requests to the Airtime API require authentication/authorization, there are several options :

Option 1

Set the client id & client secret; this is probably the most straight-forward or simplest way. An access token will be acquired automatically before the API call is made.

from reloadly_core.core.enums.Environment  import Environment
from reloadly_giftcard.giftcard.sdk.client.giftcardAPI import GiftCards

giftcardAPI = GiftCards(clientId="*****", clientSecret="*****", environment=Environment.GIFTCARD_SANDBOX)  
response = giftcardAPI.products().List_without_filter()
print (response)

Option 2

You may alternatively acquire an access token from the AuthenticationAPI and then set it.

from reloadly_auth.authentication.client.AuthenticationAPI import AuthenticationAPI
from reloadly_core.core.enums.Service import Service
from reloadly_core.core.enums.Environment  import Environment
from reloadly_giftcard.giftcard.sdk.client.giftcardAPI import GiftCards

sample = AuthenticationAPI()
a = sample.clientCredentials(clientId="*****", clientSecret="*****" service=Service.GIFTCARD_SANDBOX).getAccessToken(Service.GIFTCARD_SANDBOX)

giftcardAPI = GiftCards(accessToken = a, Environment.GIFTCARD_SANDBOX)
response = giftcardAPI.products().List_without_filter()

print (response)

Note : Access tokens obtain for Reloadly APIs have a finite lifetime. See the API docs

Using the example above has some benefits and drawbacks:

Pro

  • API requests become efficient & performant.
  • Setting the access token skips the automatic token acquisition API calls that would have otherwise been made before each Airtime API service calls.

Cons

  • However, because access tokens have a finite lifetime, you now have to manage or handle the expiration of the access token in your application code.
  • In the sample above, the AirtimeAPI will continue using the same access token until it expires. Therefore, the responsibility falls on you to handle token renewal when the token expires.

Sample token expiration handling

from reloadly_core.core.enums.Environment  import Environment
from reloadly_giftcard.giftcard.sdk.client.giftcardAPI import GiftCards

# Refresh token using AuthenticationAPI
sample = AuthenticationAPI()
a = sample.clientCredentials(clientId="*****", clientSecret="*****" service=Service.GIFTCARD_SANDBOX).getAccessToken(Service.GIFTCARD_SANDBOX)

giftcardAPI = GiftCards(accessToken = a, Environment.GIFTCARD_SANDBOX)
try:
    request = giftcardAPI.products().List_without_filter()
    if response['errorCode'] == "TOKEN_EXPIRED":
        response = giftcardAPI.refreshAccessToken(request)
        return response
    else:
        # add logic 
except:
    raise Exception("ReloadlyException")

Logging request & response

To enable API request/response logging, set enablelogging to true when intiaiting the AirtimeAPI class

    giftcardAPI = GiftCards(clientId="*****", clientSecret="*****", environment=Environment.GIFTCARD_SANDBOX, enablelogging=True)
    .... 

Customizing The API Client Instance

Configuring Timeouts

Used to configure additional options, connect and read timeouts can be configured globally:

giftcardAPI = GiftCards(
    clientId="*****", 
    clientSecret="*****", 
    environment=Environment.GIFTCARD_SANDBOX, 
    options = HttpOptions(readTimeout=60, writeTimeout=60, connectTimeout=60)
    )
    ....

Proxy Configuration

proxyPort = 8085; # Your proxy port
proxyUsername = "your-proxy-authentication-username"; # Optional proxy username if your proxy requires authentication
proxyPassword = "your-proxy-authentication-password"; # Optional proxy password if your proxy requires authentication
proxyHost = "you-proxy-host-name.com";
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

giftcardAPI = GiftCards(
    clientId="*****", 
    clientSecret="*****", 
    environment=Environment.GIFTCARD_SANDBOX, 
    options = HttpOptions()
    )
    ....
        ....
        
# If proxy does NOT require authentication


        ....                

Request latency telemetry

By default, the library sends request latency telemetry to Reloadly. These numbers help Reloadly improve the overall latency of its API for all users.

You can disable this behavior if you prefer:

giftcardAPI = GiftCards(clientId="*****", clientSecret="*****", environment=Environment.GIFTCARD_SANDBOX, enableTelemetry=False)
....

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reloadly_giftcard-1.1.6.tar.gz (14.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page