示例#1
0
文件: api.py 项目: ontiyonke/jmbo-neo
def _get_error(response):
    if response.status_code == 500:
        return Exception("Neo Web Services not responding")
    try:
        neo_resp = parseString(response.content)
        errors = None
        if isinstance(neo_resp, ResponseListType):
            errors = neo_resp.Response
        elif isinstance(neo_resp, ResponseType):
            errors = [neo_resp]
        else:
            return Exception(response.content)
        err_msg_list = []
        for error in errors:
            if error.ResponseCode == 'INVALID_APPID':
                return exceptions.ImproperlyConfigured("Neo App ID is invalid.")
            elif error.ResponseCode == 'INVALID_VERSION':
                return exceptions.ImproperlyConfigured("Neo API version is invalid.")
            elif error.ResponseCode == 'BAD_REQUEST' or response.request.method == 'POST' or \
                response.request.method == 'PUT':
                err_msg_list.append(_(error.ResponseMessage))
            else:
                return Exception(response.content)
        return exceptions.ValidationError(err_msg_list)
    except GDSParseError:
        return Exception(response.content)
示例#2
0
文件: api.py 项目: ontiyonke/jmbo-neo
def get_consumers(email_id, dob):
    dob_str = dob.strftime("%Y%m%d")
    response = requests.get("%s/consumers/" % (BASE_URL, ),
        params = {'dateofbirth': dob_str, 'emailid': email_id}, **r_kwargs)
    if response.status_code == 200:
        try:
            consumers = parseString(response.content).Consumer
            return [o.__dict__ for o in consumers]
        except GDSParseError:
            pass
    
    raise _get_error(response)
示例#3
0
文件: api.py 项目: ontiyonke/jmbo-neo
def get_consumer_profile(consumer_id):
    if CONFIG.get('USE_MCAL', False):
        raise NotImplementedError("Consumer requests not supported via MCAL")
    response = requests.get("%s/consumers/%s/profile" % (BASE_URL, consumer_id), \
        **r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass
    
    raise _get_error(response)
示例#4
0
文件: api.py 项目: ontiyonke/jmbo-neo
def get_consumer_preferences(consumer_id, category_id=None):
    uri = "%s/consumers/%s/preferences" % (BASE_URL, consumer_id)
    if category_id:
        uri += "/category/%s" % category_id
    response = requests.get(uri, **r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass

    raise _get_error(response)
示例#5
0
文件: api.py 项目: ontiyonke/jmbo-neo
def get_forgot_password_token(username):
    params = {
        'loginname': username,
        'temptoken': 0
    }
    response = requests.get("%s/consumers/useraccount" % (BASE_URL, ), \
        params=params, **r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass

    raise _get_error(response)
示例#6
0
文件: api.py 项目: praekelt/jmbo-neo
def get_consumer_profile(consumer_id, username=None, password=None, promo_code=None):
    '''
    Get a consumer's profile
    '''
    response = requests.get("%s/consumers/%s/profile" % (BASE_URL, consumer_id),
        **get_kwargs(username=username, password=password, promo_code=promo_code))
    if response.status_code == 200:
        try:
            obj_from_xml = parseString(response.content)
            log_api_call()
            return obj_from_xml
        except GDSParseError:
            pass

    raise _get_error(response)
示例#7
0
文件: api.py 项目: ontiyonke/jmbo-neo
def get_country(country_code=None, ip_address=None):
    if country_code:
        params = {'countrycode': country_code}
    elif ip_address:
        params = {'ipaddress': ip_address}
    else:
        raise ValueError("Either the country code or ip address needs to be specified.")
    response = requests.get("%s/country/" % (BASE_URL, ), \
        params=params, **r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass

    raise _get_error(response)
示例#8
0
文件: api.py 项目: ontiyonke/jmbo-neo
def get_consumer(consumer_id, username=None, password=None, promo_code=None):
    new_r_kwargs = r_kwargs
    if CONFIG.get('USE_MCAL', False):
        if username and password:
            new_r_kwargs = copy.deepcopy(r_kwargs)
            new_r_kwargs['headers']['Authorization'] = _get_auth_header(username, password, promo_code)
        else:
            raise NotImplementedError("Consumer requests not supported via MCAL")
    response = requests.get("%s/consumers/%s/all" % (BASE_URL, consumer_id), \
        **new_r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass
    
    raise _get_error(response)
示例#9
0
文件: api.py 项目: praekelt/jmbo-neo
def get_consumers(email_id, dob):
    '''
    Retrieves a list of consumers' identified by email/mobile id and DOB
    Returns a list of dicts like
    [{'ConsumerID': val, 'LoginName': val, 'ApplicationName': val}, ...]
    '''
    dob_str = dob.strftime("%Y%m%d")
    response = requests.get("%s/consumers/" % (BASE_URL, ),
        params = {'dateofbirth': dob_str, 'emailid': email_id}, **get_kwargs())
    if response.status_code == 200:
        try:
            consumers = parseString(response.content).Consumer
            log_api_call()
            return [o.__dict__ for o in consumers]
        except GDSParseError:
            pass
    
    raise _get_error(response)
示例#10
0
文件: api.py 项目: ontiyonke/jmbo-neo
def do_age_check(dob, country_code, gateway_id, language_code=None):
    dob_str = dob.strftime("%Y%m%d")
    params = {
        'dateofbirth': dob_str,
        'countrycode': country_code,
        'gatewayid': gateway_id
    }
    if language_code:
        params['language_code'] = language_code
    response = requests.get("%s/consumers/affirmage" % (BASE_URL, ), \
        params=params, **r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass
        
    raise _get_error(response)
示例#11
0
文件: api.py 项目: praekelt/jmbo-neo
def _get_error(response):
    '''
    Determine the appropriate error
    '''
    exception = None
    if response.status_code == 500:
        exception = Exception("Neo Web Services not responding")
    else:
        try:
            neo_resp = parseString(response.content)
            errors = None
            if isinstance(neo_resp, ResponseListType):
                errors = neo_resp.Response
            elif isinstance(neo_resp, ResponseType):
                errors = [neo_resp]
            else:
                exception = Exception(response.content)

            if not exception:
                err_msg_list = []
                for error in errors:
                    if error.ResponseCode == 'INVALID_APPID':
                        exception = exceptions.ImproperlyConfigured(
                            "Neo App ID is invalid."
                        )
                    elif error.ResponseCode == 'INVALID_VERSION':
                        exception = exceptions.ImproperlyConfigured(
                            "Neo API version is invalid."
                        )
                    elif (error.ResponseCode == 'BAD_REQUEST' or
                          response.request.method == 'POST' or
                          response.request.method == 'PUT'):
                        err_msg_list.append(_(error.ResponseMessage))
                    else:
                        exception = Exception(response.content)
                if not exception:
                    exception = exceptions.ValidationError(err_msg_list)
        except GDSParseError:
            exception = Exception(response.content)

    log_api_call(function_call_tup=inspect.stack()[1],
                 log_level=logging.ERROR,
                 exception=exception)
    return exception
示例#12
0
文件: api.py 项目: ontiyonke/jmbo-neo
def link_consumer(consumer_id, username, password, promo_code=None, acq_src=None):
    if CONFIG.get('USE_MCAL', False):
        raise NotImplementedError("Consumer requests not supported via MCAL")
    params = {
        'loginname': username,
        'password': password,
        'promocode': promo_code if promo_code else CONFIG['PROMO_CODE']
    }
    if acq_src:
        params['acquisitionsource'] = acq_src
    response = requests.put("%s/consumers/%s/registration/" % (BASE_URL, consumer_id), \
        params=params, **r_kwargs)
    if response.status_code == 200:
        try:
            return parseString(response.content)
        except GDSParseError:
            pass

    raise _get_error(response)
示例#13
0
文件: api.py 项目: praekelt/jmbo-neo
def get_forgot_password_token(username):
    '''
    Gets an ID token to change a forgotten password
    '''
    params = {
        'loginname': username,
        'temptoken': 0
    }
    response = requests.get("%s/consumers/useraccount" % (BASE_URL, ),
        params=params, **get_kwargs())
    if response.status_code == 200:
        try:
            obj_from_xml = parseString(response.content)
            log_api_call()
            return obj_from_xml
        except GDSParseError:
            pass

    raise _get_error(response)
示例#14
0
文件: api.py 项目: praekelt/jmbo-neo
def get_consumer_preferences(consumer_id, category_id=None,
    username=None, password=None, promo_code=None):
    '''
    Get a consumer's preferences
    Specify category_id to get preferences for a category,
    otherwise all preferences are returned
    '''
    uri = "%s/consumers/%s/preferences" % (BASE_URL, consumer_id)
    if category_id:
        uri += "/category/%s" % category_id
    response = requests.get(uri, **get_kwargs(username=username, password=password, promo_code=promo_code))
    if response.status_code == 200:
        try:
            obj_from_xml = parseString(response.content)
            log_api_call()
            return obj_from_xml
        except GDSParseError:
            pass

    raise _get_error(response)
示例#15
0
文件: api.py 项目: praekelt/jmbo-neo
def get_country(country_code=None, ip_address=None):
    '''
    Get country details
    '''
    if country_code:
        params = {'countrycode': country_code}
    elif ip_address:
        params = {'ipaddress': ip_address}
    else:
        raise ValueError("Either the country code or ip address needs to be specified.")
    response = requests.get("%s/country/" % (BASE_URL, ),
        params=params, **get_kwargs())
    if response.status_code == 200:
        try:
            obj_from_xml = parseString(response.content)
            log_api_call()
            return obj_from_xml
        except GDSParseError:
            pass

    raise _get_error(response)
示例#16
0
文件: api.py 项目: praekelt/jmbo-neo
def do_age_check(dob, country_code, gateway_id, language_code=None):
    '''
    Check if the user is of allowable age
    '''
    dob_str = dob.strftime("%Y%m%d")
    params = {
        'dateofbirth': dob_str,
        'countrycode': country_code,
        'gatewayid': gateway_id
    }
    if language_code:
        params['language_code'] = language_code
    response = requests.get("%s/consumers/affirmage" % (BASE_URL, ),
        params=params, **get_kwargs())
    if response.status_code == 200:
        try:
            obj_from_xml = parseString(response.content)
            log_api_call()
            return obj_from_xml
        except GDSParseError:
            pass

    raise _get_error(response)
示例#17
0
文件: api.py 项目: praekelt/jmbo-neo
def link_consumer(consumer_id, username, password, promo_code=None, acq_src=None):
    '''
    Links a consumer account from another app with this app
    '''
    #if CONFIG.get('USE_MCAL', False):
    #    raise NotImplementedError("Consumer requests not supported via MCAL")
    params = {
        'loginname': username,
        'password': password,
        'promocode': promo_code if promo_code else CONFIG['PROMO_CODE']
    }
    if acq_src:
        params['acquisitionsource'] = acq_src
    response = requests.put("%s/consumers/%s/registration/" % (BASE_URL, consumer_id),
        params=params, **get_kwargs())
    if response.status_code == 200:
        try:
            obj_from_xml = parseString(response.content)
            log_api_call()
            return obj_from_xml
        except GDSParseError:
            pass

    raise _get_error(response)