Ejemplo n.º 1
0
def create_outbound_subscription(accountId, appApiKey, appShortName, appChannelId, verbose=False):
    """Creates an outbound subscription.

    Parameters
    ----------
    accountId: str
        The account ID that owns the application identified by appShortName
    appApiKey: str
        The API key of the application identified by appShortName
    appShortName: str
        The short name of an application
    appChannelId: str
        The ID of a notification channel associated with the application identified by appShortName
    verbose: bool, optional
        True to print verbose output.
    Returns
    -------
    dict
        A dict containing the subscription configuration, including the subscription ID.

    Raises
    ------
    An ApiException if there was a problem.
    """
    url = aerisconfig.get_aerframe_api_url()
    endpoint = url + '/smsmessaging/v2/' + accountId + '/outbound/' + appShortName + '/subscriptions'
    callbackReference = {
        'callbackData': appShortName + '-mt',
        'notifyURL': aerisconfig.get_aerframe_api_url() + '/notificationchannel/v2/'
        + accountId + '/channels/' + appChannelId + '/callback'
    }
    payload = {'callbackReference': callbackReference,
               'filterCriteria': 'SP:*',  # Could use SP:Aeris as example of service profile
               'destinationAddress': [appShortName]}
    myparams = {"apiKey": appApiKey}
    r = requests.post(endpoint, params=myparams, json=payload)
    aerisutils.vprint(verbose, "Response code: " + str(r.status_code))
    if r.status_code == 201:  # In this case, we get a 201 'created' for success
        subscriptionConfig = json.loads(r.text)
        print('Created outbound (MT-DR) subscription for ' + appShortName)
        aerisutils.vprint(verbose, 'Subscription info:\n' + json.dumps(subscriptionConfig, indent=4))
        return subscriptionConfig
    else:
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was: ' + str(r.status_code), r)
Ejemplo n.º 2
0
def send_mt_sms(accountId, apiKey, appShortName, imsiDestination, smsText, verbose=False):
    """Sends a Mobile-Terminated Short Message (MT-SM) to a device.

    Parameters
    ----------
    accountId: str
        The account ID that owns the destination device.
    apiKey: str
        An API key for the account.
    appShortName: str
        The application short name.
    imsiDestination: str
        The IMSI of the destination device.
    smsText: str
        The text payload to send to the device.
    verbose: bool, optional
        True to enable verbose printing.

    Returns
    -------
    dict
        A dict containing AerFrame's response, or None if the device was not found or does not support SMS.

    Raises
    ------
    ApiException
        if there was another problem with the API.
    """
    url = aerisconfig.get_aerframe_api_url()
    endpoint = f'{url}/smsmessaging/v2/{accountId}/outbound/{appShortName}/requests'
    address = [imsiDestination]
    outboundSMSTextMessage = {"message": smsText}
    payload = {'address': address,
               'senderAddress': appShortName,
               'outboundSMSTextMessage': outboundSMSTextMessage,
               'clientCorrelator': '123456',
               'senderName': appShortName}
    myparams = {"apiKey": apiKey}
    # print('Payload: \n' + json.dumps(payload, indent=4))
    r = requests.post(endpoint, params=myparams, json=payload)
    aerisutils.vprint(verbose, "Response code: " + str(r.status_code))
    if r.status_code == 201:  # In this case, we get a 201 'created' for success
        sendsmsresponse = json.loads(r.text)
        print('Sent SMS:\n' + json.dumps(sendsmsresponse, indent=4))
        return sendsmsresponse
    elif r.status_code == 404:  # Check if no matching device IMSI or IMSI not support SMS
        print('IMSI is not found or does not support SMS.')
        print(r.text)
        return None
    else:
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was ' + str(r.status_code), r)
Ejemplo n.º 3
0
def get_outbound_subscription_id_by_app_short_name(accountId, appApiKey, appShortName, verbose=False):
    """Gets the Subscription ID of the first outbound subscription for the application given by appShortName

    Parameters
    ----------
    accountId: str
        The account ID that owns the application
    appApiKey: str
        The application API key for the application
    appShortName: str
        The short name of the application
    verbose: bool
        True to print verbose output.

    Returns
    -------
    str
       The ID of the first subscription found, or None if no subscriptions were found for the application.

    Raises
    ------
    ApiException
        if there was another problem with the API.

    """
    url = aerisconfig.get_aerframe_api_url()
    endpoint = url + '/smsmessaging/v2/' + accountId + '/outbound/' + appShortName + '/subscriptions'
    myparams = {'apiKey': appApiKey}
    r = requests.get(endpoint, params=myparams)
    aerisutils.vprint(verbose, "Response code: " + str(r.status_code))
    if r.status_code == 200:
        subscriptions = json.loads(r.text)
        if 'deliveryReceiptSubscription' in subscriptions.keys():
            aerisutils.vprint(verbose, appShortName + ' has outbound (MT-DR) subscriptions.' + json.dumps(subscriptions,
                                                                                                          indent=4))
            subscriptionId \
                = subscriptions['deliveryReceiptSubscription'][0]['resourceURL'].split('/subscriptions/', 1)[1]
            print(appShortName + ' outbound subscription ID: ' + subscriptionId)
            return subscriptionId
        else:
            print(appShortName + ' has no outbound (MT-DR) subscriptions.')
            return None
    else:  # Response code was not 200
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was: ' + str(r.status_code), r)
Ejemplo n.º 4
0
def get_inbound_subscription_by_app_short_name(accountId, appApiKey, appShortName, verbose=False):
    """
    Prints and returns the subscription ID of the first inbound subscription for the application given by appShortName

    Parameters
    ----------
    accountId: str
        The account ID that owns the application
    appApiKey: str
        The application API key for the application
    appShortName: str
        The short name of the application
    verbose: bool
        True to print verbose output.

    Returns
    -------
    str
       The ID of the first subscription found, or None if no subscriptions were found for the application.

    Raises
    ------
    ApiException
        if there was another problem with the API.
    """
    endpoint = aerisconfig.get_aerframe_api_url() + '/smsmessaging/v2/' + accountId + '/inbound/subscriptions'
    myparams = {'apiKey': appApiKey}
    r = requests.get(endpoint, params=myparams)
    aerisutils.vprint(verbose, "Response code: " + str(r.status_code))
    if r.status_code == 200:
        subscriptions = json.loads(r.text)
        aerisutils.vprint(verbose, json.dumps(subscriptions['subscription'], indent=4))  # Print formatted json
        if 'subscription' not in subscriptions.keys():
            print(f'No inbound subscriptions for application short name {appShortName}')
            return None
        print('Inbound subscriptions:\n')
        for subscription in subscriptions['subscription']:  # Iterate subscriptions to try and find sdk application
            print(subscription['destinationAddress'])
            if appShortName in subscription['destinationAddress']:
                subscription_id = subscription['resourceURL'].split('/')[-1]
                print(appShortName + ' inbound subscription ID: ' + subscription_id)
                return subscription_id
    else:  # Response code was not 200
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was: ' + str(r.status_code), r)
Ejemplo n.º 5
0
def get_outbound_subscription(accountId, appApiKey, appShortName, subscriptionId, verbose=False):
    """Gets the details of an outbound subscription, given its subscription ID
    and the short name of the associated application.

    Parameters
    ----------
    accountId: str
        The account ID that owns the application
    appApiKey: str
        The application API key for the application
    appShortName: str
        The short name of the application
    subscriptionId: str
        The ID of the subscription
    verbose: bool, optional
        True to print verbose output.

    Returns
    -------
    dict
        A dict containing details of the subscription, or None if no subscription was found.

    Raises
    ------
    ApiException
        if there was a problem.
    """
    url = aerisconfig.get_aerframe_api_url()
    endpoint = url + '/smsmessaging/v2/' + accountId + '/outbound/' + appShortName + '/subscriptions/' + subscriptionId
    myparams = {'apiKey': appApiKey}
    r = requests.get(endpoint, params=myparams)
    aerisutils.vprint(verbose, "Response code: " + str(r.status_code))
    if r.status_code == 200:
        subscription = json.loads(r.text)
        return subscription
    if r.status_code == 404:
        return None
    else:  # Response code was not 200
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was: ' + str(r.status_code), r)
Ejemplo n.º 6
0
def delete_outbound_subscription(accountId, appApiKey, appShortName, subscriptionId, verbose=False):
    """Deletes an outbound subscription.

    Parameters
    ----------
    accountId: str
        The account ID that owns the application.
    appApiKey: str
        The API key of the application.
    appShortName: str
        The short name of the application.
    subscriptionId: str
        The ID of the subscription to delete.
    verbose: bool, optional
        True to print verbose output.

    Returns
    -------
    bool, optional
        True if the subscription was deleted, or False if no such subscription was found.

    Raises
    ------
    ApiException
        if there was another problem.
    """
    url = aerisconfig.get_aerframe_api_url()
    endpoint = url + '/smsmessaging/v2/' + accountId + '/outbound/' + appShortName + '/subscriptions/' + subscriptionId
    myparams = {"apiKey": appApiKey}
    r = requests.delete(endpoint, params=myparams)
    if r.status_code == 204:  # Check for 'no content' http response
        print('Subscription successfully deleted.')
        return True
    elif r.status_code == 404:  # Check if no matching subscription ID
        print('Subscription ID does not match current application.')
        return False
    else:
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was: ' + str(r.status_code), r)
Ejemplo n.º 7
0
def get_location(accountId, apiKey, deviceIdType, deviceId, verbose=False):
    """Gets information about the location of a device.

    Parameters
    ----------
    accountId: str
        The account ID that owns the device.
    apiKey: str
        An API key for the account ID.
    deviceIdType: str
        The type of device ID supplied. Must be 'MSISDN' or 'IMSI'
    deviceId: str
        The device ID.
    verbose: bool, optional
        True to verbosely print output.

    Returns
    -------
    dict
        representing the device's location.

    Raises
    ------
    ApiException
        if there was a problem.
    """
    url = aerisconfig.get_aerframe_api_url()
    endpoint = f'{url}/networkservices/v2/{accountId}/devices/{deviceIdType}/{deviceId}/networkLocation'
    myparams = {'apiKey': apiKey}
    r = requests.get(endpoint, params=myparams)
    aerisutils.vprint(verbose, "Response code: " + str(r.status_code))
    if r.status_code == 200:
        locationInfo = json.loads(r.text)
        return locationInfo
    else:  # Response code was not 200
        aerisutils.print_http_error(r)
        raise ApiException('HTTP status code was ' + str(r.status_code), r)
Ejemplo n.º 8
0
def get_channel_endpoint(accountId, channelId=None):
    endpoint_base = aerisconfig.get_aerframe_api_url()
    if channelId is None:
        return endpoint_base + '/notificationchannel/v2/' + accountId + '/channels'
    else:
        return endpoint_base + '/notificationchannel/v2/' + accountId + '/channels/' + channelId
Ejemplo n.º 9
0
def get_application_endpoint(accountId, appId=None):
    endpoint_base = aerisconfig.get_aerframe_api_url()
    if appId is None:
        return endpoint_base + '/registration/v2/' + accountId + '/applications'
    else:
        return endpoint_base + '/registration/v2/' + accountId + '/applications/' + appId