Package: reloadly-airtime
Airtime APIThe implementation is based on theAirtime API Docs.UsageCreate anAirtimeAPIinstance by providing the Application details (client id & secret) from
thedashboard.Some key things to keep in mind regarding the Airtime API :The API has 2 environments, SANDBOX (for development & testing) and LIVE.Environment.AIRTIMEis used for the Live environmnent andEnvironment.AIRTIME_SANDBOXis for the Sandbox.If neither environment is specified, the SDK defaults to SANDBOXEach environment has a set of credentials (client id & secret) that are different from the other.SANBOX credentials can only be used for SANDBOX environmentLIVE credentials can only be used for LIVE environmentIf not environment is specified the SDK defaults to SANDBOXYou MUST supply either the credentials, or an access token in order to call the APIAs stated above, requests to the Airtime API require authentication/authorization, there are several options :Option 1Set 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.fromreloadly_core.core.enums.EnvironmentimportEnvironmentfromreloadly_airtime.airtime.sdk.client.AirtimeAPIimportAirtimeAPIairtimeAPI=AirtimeAPI(clientId="*****",clientSecret="*****",environment=Environment.AIRTIME_SANDBOX)response=airtimeAPI.accounts().getBalance()print(response)Option 2You may alternatively acquire an access token from theAuthenticationAPIand then set it.fromreloadly_auth.authentication.client.AuthenticationAPIimportAuthenticationAPIfromreloadly_core.core.enums.ServiceimportServicefromreloadly_core.core.enums.EnvironmentimportEnvironmentfromreloadly_airtime.airtime.sdk.client.AirtimeAPIimportAirtimeAPIsample=AuthenticationAPI()a=sample.clientCredentials(clientId="*****",clientSecret="*****",service=Service.AIRTIME_SANDBOX).getAccessToken(Service.AIRTIME_SANDBOX)airtimeAPI=AirtimeAPI(accessToken=a,Environment.AIRTIME_SANDBOX)response=airtimeAPI.accounts().getBalance()print(response)Note : Access tokens obtain for Reloadly APIs have a finite
lifetime.See the API docsUsing the example above has some benefits and drawbacks:ProAPI 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.ConsHowever, 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 handlingfromreloadly_core.core.enums.EnvironmentimportEnvironmentfromreloadly_airtime.airtime.sdk.client.AirtimeAPIimportAirtimeAPI# Retrieve token using AuthenticationAPIsample=AuthenticationAPI()a=sample.clientCredentials(clientId="*****",clientSecret="*****"service=Service.AIRTIME_SANDBOX).getAccessToken(Service.AIRTIME_SANDBOX)airtimeAPI=AirtimeAPI(accessToken=a,Environment.AIRTIME_SANDBOX)try:request=airtimeAPI.accounts().getBalance()ifresponse['errorCode']=="TOKEN_EXPIRED":response=airtimeAPI.refreshAccessToken(request)returnresponseelse:# add logicexcept:raiseException("ReloadlyException")Logging request & responseTo enable API request/response logging, setenableloggingto true when intiaiting the AirtimeAPI classairtimeAPI=AirtimeAPI(clientId="*****",clientSecret="*****",environment=Environment.AIRTIME_SANDBOX,enablelogging=True)....Customizing The API Client InstanceConfiguring TimeoutsUsed to configure additional options, connect and read timeouts can be configured globally:airtimeAPI=AirtimeAPI(clientId="*****",clientSecret="*****",environment=Environment.AIRTIME_SANDBOX,options=HttpOptions(readTimeout=60,writeTimeout=60,connectTimeout=60))....Proxy ConfigurationproxyPort=8085;# Your proxy portproxyUsername="your-proxy-authentication-username";# Optional proxy username if your proxy requires authenticationproxyPassword="your-proxy-authentication-password";# Optional proxy password if your proxy requires authenticationproxyHost="you-proxy-host-name.com";Proxyproxy=newProxy(Proxy.Type.HTTP,newInetSocketAddress(proxyHost,proxyPort));airtimeAPI=AirtimeAPI(clientId="*****",clientSecret="*****",environment=Environment.AIRTIME_SANDBOX,options=HttpOptions()....)....# If proxy does NOT require authentication....Request latency telemetryBy 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:airtimeAPI=AirtimeAPI(clientId="*****",clientSecret="*****",environment=Environment.AIRTIME_SANDBOX,enableTelemetry=False)....