Beispiel #1
0
def subscribe_sms_dr_endpoint(endpoint_url, silent_failure=False):
    sender_address = get_config('sender_address')
    payload = {
        'deliveryReceiptSubscription': {
            'callbackReference': {
                'notifyURL': endpoint_url
            }
        }
    }
    url = "{api}/outbound/{addr}/subscriptions".format(
        api=get_config('smsmt_url'),
        addr=quote(sender_address))
    headers = get_standard_header()

    req = requests.post(url, headers=headers, json=payload)

    try:
        assert req.status_code == 201
        resp = req.json()
    except AssertionError:
        exp = OrangeAPIError.from_request(req)
        logger.error("Unable to subscribe an SMS-DR endpoint. {exp}"
                     .format(exp=exp))
        logger.exception(exp)
        if not silent_failure:
            raise exp
        return False

    subscription_id = resp['deliveryReceiptSubscription'] \
        .get('resourceURL', '').rsplit('/', 1)[-1]

    if subscription_id:
        update_config({'smsmtdr_subsription_id': subscription_id}, save=True)
    return bool(subscription_id)
Beispiel #2
0
def request_token(silent_failure=False):
    url = "{oauth_url}/token".format(oauth_url=get_config('oauth_url'))
    basic_header = b64encode(
        "{client_id}:{client_secret}".format(
            client_id=get_config('client_id'),
            client_secret=get_config('client_secret')))
    headers = {'Authorization': "Basic {b64}".format(b64=basic_header)}
    payload = {'grant_type': 'client_credentials'}
    req = requests.post(url, headers=headers, data=payload)
    resp = req.json()
    if "token_type" in resp:
        expire_in = int(resp['expires_in'])
        token_data = {
            'token': resp['access_token'],
            'token_expiry': datetime.datetime.now() + datetime.timedelta(
                days=expire_in / ONE_DAY,
                seconds=expire_in % ONE_DAY) - datetime.timedelta(
                days=0, seconds=60)}
        update_config(token_data, save=True)
        return token_data
    else:
        exp = OrangeAPIError.from_request(req)
        logger.error("Unable to retrieve token. {}".format(exp))
        logger.exception(exp)
        if not silent_failure:
            raise exp
        return False
Beispiel #3
0
def get_contracts(silent_failure=False):
    url = "{api}/contracts".format(api=get_config('smsadmin_url'))
    headers = get_standard_header()

    req = requests.get(url, headers=headers)
    try:
        assert req.status_code == 200
        return req.json()
    except AssertionError:
        exp = OrangeAPIError.from_request(req)
        logger.error("Unable to retrieve contracts. {exp}".format(exp=exp))
        logger.exception(exp)
        if not silent_failure:
            raise exp
Beispiel #4
0
def get_sms_dr_subscriptions(silent_failure=False):
    subscription_id = get_config('smsmtdr_subsription_id')
    if not subscription_id:
        return {}
    url = "{api}/outbound/subscriptions/{subscription}".format(
        api=get_config('smsmt_url'),
        subscription=subscription_id)
    headers = get_standard_header()

    req = requests.get(url, headers=headers)
    try:
        assert req.status_code == 200
        return req.json()
    except AssertionError:
        exp = OrangeAPIError.from_request(req)
        logger.error("Unable to retrieve contracts. {exp}".format(exp=exp))
        logger.exception(exp)
        if not silent_failure:
            raise exp
Beispiel #5
0
def unsubscribe_sms_dr_endpoint(subscription_id, silent_failure=False):
    sender_address = get_config('sender_address')
    url = "{api}/outbound/{addr}/subscriptions/{sub}".format(
        api=get_config('smsmt_url'),
        addr=quote(sender_address),
        sub=subscription_id)
    headers = get_standard_header()

    req = requests.delete(url, headers=headers)

    try:
        assert req.status_code == 204
        update_config({'smsmtdr_subsription_id': None}, save=True)
    except AssertionError:
        exp = OrangeAPIError.from_request(req)
        logger.error("Unable to unsubscribe an SMS-DR endpoint. {exp}"
                     .format(exp=exp))
        logger.exception(exp)
        if not silent_failure:
            raise exp
        return False

    return True
Beispiel #6
0
def do_submit_sms_mt_request(payload, message=None, silent_failure=False):
    ''' Use submit_sms_mt_request

    actual submission of API request for SMS-MT '''
    def update_status(msg, success):
        if msg is None:
            return
        msg.update_status(msg.SENT if success else msg.FAILED_TO_SEND)
        return success

    sender_address = payload['outboundSMSMessageRequest']['senderAddress']
    url = "{api}/outbound/{addr}/requests".format(
        api=get_config('smsmt_url'),
        addr=quote(sender_address))
    headers = get_standard_header()

    req = requests.post(url, headers=headers, json=payload)

    try:
        assert req.status_code == 201
        resp = req.json()
    except AssertionError:
        exp = OrangeAPIError.from_request(req)
        logger.error("Unable to transmit SMS-MT. {exp}".format(exp=exp))
        logger.exception(exp)
        update_status(message, False)
        if not silent_failure:
            raise exp
        return False

    rurl = resp['outboundSMSMessageRequest'] \
        .get('resourceURL', '').rsplit('/', 1)[-1]
    if message is not None and rurl:
        message.update_reference(rurl)
        return update_status(message, True)
    return bool(rurl)