Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 3
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