def create_it_now(body, params=dict()): headers = { 'X-API-USER': '******', 'X-API-TOKEN': 'kuchbhi' } r = make_api_call('http://api-service04.production.askme.com:9933/vouchers/v1/create', method='POST', headers=headers, body=body, params=params) return r
def fetch_user_details(order): customer_id = order.customer_id user_info_url = config.USERFROMMOBILEURL + str(customer_id) + '/' headers = {'Authorization': config.TOKEN} response = make_api_call(user_info_url, headers=headers) return get_user_details(response)
def fetch_location_dict(area_id): key = GROCERY_LOCATION_KEY + u'{}'.format(area_id) location_dict = cache.get(key) if not location_dict: location_url = config.LOCATIONURL + str(area_id) + '/' headers = {'Authorization': config.TOKEN} response = make_api_call(location_url, headers=headers) try: data_list = json.loads(response.text) except Exception as e: logger.exception(e) return False, None, u'Unable to fetch area details' if not data_list: return False, None, u'Area Does not exist' data = data_list[0] location_dict = dict() location_dict['area'] = data.get('areaid') location_dict['country'] = [data.get('countryid')] location_dict['state'] = [data.get('stateid')] location_dict['city'] = [data.get('cityid')] location_dict['zone'] = [data.get('zoneid')] cache.set(key, location_dict, ex=GROCERY_CACHE_TTL) return True, location_dict, None
def fetch_ibl(body, params=dict()): headers = { 'X-API-USER': '******', 'X-API-TOKEN': 'kuchbhi' } r = make_api_call('http://api-service04.production.askme.com:9933/vouchers/v1/fetchDetail', method='POST', headers=headers, body=body, params=params) # r = make_api_call('http://pyservice01.staging.askme.com:9933/vouchers/v1/fetchDetail', method='POST', headers=headers, body=body, params=params) return r
def fetch_phone_no_from_session_id(session_id): url = config.SESSIONPHONEAPI + str(session_id) headers = config.USERPHONENOAPIHEADERS response = make_api_call(url=url, method='GET', headers=headers) if response.status_code != 200: return False, None try: data = json.loads(response.text) if 'result' in data and data['result'] == 'failure': return False, None if 'error' in data: return False, None return data['user']['is_phone_verified'], data['user']['phone'] except Exception as e: logger.exception(e) return False, None
def fetch_phone_no(user_id): url = config.USERPHONENOAPI + str(user_id) headers = config.USERPHONENOAPIHEADERS response = make_api_call(url=url, method='GET', headers=headers) if response.status_code != 200: return False, None try: data = json.loads(response.text) value_list = data.get('contact', dict()).get('data') if not value_list: return False, None for value in value_list: if value.get('type') == 'phone': if value.get('verified') == True: return True, value.get('value') return False, value.get('value') except Exception as e: logger.exception(e) return False, None
def replay_test(group_no): try: client = KafkaClient(hosts=KAFKAHOST) except Exception as e: return group_id = TEST_TOPIC_KAFKA + u'{}'.format(group_no) try: topic = client.topics[TEST_TOPIC_KAFKA] balanced_consumer = topic.get_balanced_consumer( consumer_group=str(group_id), auto_commit_enable=True, reset_offset_on_start=True, auto_offset_reset=common.OffsetType.LATEST, use_rdkafka=False, zookeeper_connect=ZOOKEEPER) for message in balanced_consumer: if message is not None: data = json.loads(message.value) end_point = data['url'] body = data['body'] if end_point.endswith(('check', 'apply')): body_data = json.loads(body) for product in body_data.get('products', list()): product['subscription_id'] = product.get('item_id') body = json.dumps(body_data) params = data['query'] response = data['response'] url = HOST + end_point headers = { 'X-API-USER': TEST_USER, 'X-API-TOKEN': TEST_TOKEN, 'Content-Type': 'Application/Json' } response_on_staging_obj = make_api_call(url=url, body=json.loads(body), method='POST', headers=headers, params=params) response_on_staging = response_on_staging_obj.text data = { 'url': end_point, 'body': body, 'params': json.dumps(params), 'prod_response': response, 'staging_response': response_on_staging, 'body_varchar': body, 'prod_response_varchar': response, 'staging_response_varchar': response_on_staging, 'match': response_on_staging == response } db = CouponsAlchemyDB() db.begin() try: try: db.insert_row("auto_tester", **data) except DataError: del data['body_varchar'] del data['prod_response_varchar'] del data['staging_response_varchar'] db.insert_row("auto_tester", **data) db.commit() except Exception as e: logger.exception(e) db.rollback() except Exception as e: logger.exception(e)
def fetch_user_details(order): customer_id = order.customer_id user_info_url = config.USERINFOURL + '?user_id=' + str(customer_id) response = make_api_call(user_info_url) return get_user_details(response)
def fetch_location_dict(id): key = GROCERY_LOCATION_KEY + u'{}'.format(id) location_dict = cache.get(key) if not location_dict: location_url = config.LOCATIONURL + str(id) response = make_api_call(location_url) try: raw_data = json.loads(response.text) except Exception as e: logger.exception(e) return False, None, u'Unable to fetch details for geo id={}'.format( id) if not raw_data.get('locations'): return False, None, u'geo id={} does not exist'.format(id) locations = raw_data.get('locations') data = None for location in locations: if 'tags' in location and location['tags']: if 'grocery' in location['tags']: data = location break if not data and not ( ('tags' in locations[0]) and locations[0]['tags'] and ('grocery' not in locations[0]['tags'])): data = locations[0] if not data or not data['types']: return False, None, u'{} is not a valid geo Id'.format(id) geo_types_ordered = [ 'area', 'pincode', 'zone', 'city', 'state', 'country' ] id_types = data['types'] id_type = None for geo_type in geo_types_ordered: if geo_type in id_types: id_type = geo_type break if not id_type: return False, None, u'{} is not a valid geo Id'.format(id) location_dict = { 'area': list(), 'state': list(), 'city': list(), 'pincode': list(), 'zone': list(), 'country': list() } for container in data.get('containers'): for geo_type in geo_types_ordered: if geo_type in container['types']: location_dict[geo_type].append(container['gid']) location_dict[id_type].append(id) if not location_dict['country']: location_dict['country'].append( 1 ) # TODO remove this, once geo service starts returning country also cache.set(key, location_dict, ex=GROCERY_CACHE_TTL) return True, location_dict, None
def fetch_items(subscription_id_list, item_map): # item_map is a subscription id to a list dicts of item ids and their resp quantities # we must cache subscription id to subscription dict and fetch the rest from the api # and set the cache for them. # While iterating over subscription ids, build the list of verification item dicts # and create the item and add it to final resultant list. item_list = list() to_fetch_subscription_list = list() for subscription_id in subscription_id_list: key = GROCERY_ITEM_KEY + u'{}'.format(subscription_id) subscription_dict = cache.get(key) if subscription_dict: for item in item_map.get(subscription_id): item_id = item.get('item_id') quantity = item.get('quantity') item_dict = copy.deepcopy(subscription_dict) item_dict['quantity'] = quantity item_dict['subscription_id'] = subscription_id item_dict['item_id'] = item_id item_obj = VerificationItemData(**item_dict) item_list.append(item_obj) else: to_fetch_subscription_list.append(subscription_id) if to_fetch_subscription_list: to_fetch_subscription_list = [ int(to_fetch_item_id) for to_fetch_item_id in to_fetch_subscription_list ] body = { "query": { "type": ["grocery"], "filters": { "id": to_fetch_subscription_list }, "select": [ "sellerId", "variantId", "productId", "categories", "storeFronts", "brandId" ] }, "count": len(to_fetch_subscription_list), "offset": 0 } headers = config.SUBSCRIPTIONHEADERS response = make_api_call(config.SUBSCRIPTIONURL, method='POST', headers=headers, body=body) try: response_data = json.loads(response.text) except Exception as e: logger.exception(e) return False, None, u'Unable to fetch Items' try: count = response_data['results'][0]['items'][0]['count'] if count != len(to_fetch_subscription_list): return False, None, u'Invalid Subscription Ids provided' raw_data_list = response_data['results'][0]['items'][0]['items'] except Exception as e: logger.exception(e) logger.error(u'Invalid Response for items {} recieved {}'.format( to_fetch_subscription_list, response_data)) return False, None, u'Unknown Error. Please contact tech support' for raw_data in raw_data_list: data = { 'variant': raw_data['variantId'], 'price': raw_data['offerPrice'], 'brand': raw_data['brandId'], 'product': [raw_data['productId']], 'seller': raw_data['sellerId'] } category_list = list() for category in raw_data['categories']: category_list.append(category['id']) data['category'] = category_list storefront_list = list() for storefront in raw_data['storeFronts']: storefront_list.append(storefront['id']) data['storefront'] = storefront_list key = GROCERY_ITEM_KEY + u'{}'.format(raw_data.get('id')) cache.set(key, data, ex=GROCERY_CACHE_TTL) for item in item_map.get(u'{}'.format(raw_data.get('id'))): item_id = item.get('item_id') quantity = item.get('quantity') item_dict = copy.deepcopy(data) item_dict['quantity'] = quantity item_dict['subscription_id'] = raw_data.get('id') item_dict['item_id'] = item_id item_obj = VerificationItemData(**item_dict) item_list.append(item_obj) return True, item_list, None
def fetch_items(subscription_id_list, item_map): # item_map is a subscription id to a list dicts of item ids and their resp quantities # we must cache subscription id to subscription dict and fetch the rest from the api # and set the cache for them. # While iterating over subscription ids, build the list of verification item dicts # and create the item and add it to final resultant list. item_list = list() to_fetch_subscription_list = list() for subscription_id in subscription_id_list: key = GROCERY_ITEM_KEY + u'{}'.format(subscription_id) subscription_dict = cache.get(key) if subscription_dict: for item in item_map.get(subscription_id): item_id = item.get('item_id') quantity = item.get('quantity') item_dict = dict() item_dict['brand'] = subscription_dict.get('brandid') item_dict['category'] = [subscription_dict.get('categoryid')] item_dict['product'] = [subscription_dict.get('productid')] item_dict['seller'] = subscription_dict.get('sellerid') item_dict['storefront'] = subscription_dict.get( 'storefront_id') item_dict['variant'] = subscription_dict.get('variantid') item_dict['price'] = subscription_dict.get('offerprice') item_dict['quantity'] = quantity item_dict['subscription_id'] = subscription_id item_dict['item_id'] = item_id item_obj = VerificationItemData(**item_dict) item_list.append(item_obj) else: to_fetch_subscription_list.append(subscription_id) if to_fetch_subscription_list: subscription_id_list_str = ','.join( u'{}'.format(v) for v in to_fetch_subscription_list) item_url = config.SUBSCRIPTIONURL + subscription_id_list_str headers = {'Authorization': config.TOKEN} response = make_api_call(item_url, headers=headers) try: data_list = json.loads(response.text) except Exception as e: logger.exception(e) return False, None, u'Unable to fetch Items' if not isinstance( data_list, list) or len(data_list) != len(to_fetch_subscription_list): return False, None, u'Invalid Item ids provided' for data in data_list: key = GROCERY_ITEM_KEY + u'{}'.format(data.get('itemid')) cache.set(key, data, ex=GROCERY_CACHE_TTL) for item in item_map.get(u'{}'.format(data.get('itemid'))): item_id = item.get('item_id') quantity = item.get('quantity') item_dict = dict() item_dict['brand'] = data.get('brandid') item_dict['category'] = [data.get('categoryid')] item_dict['product'] = [data.get('productid')] item_dict['seller'] = data.get('sellerid') item_dict['storefront'] = data.get('storefront_id') item_dict['variant'] = data.get('variantid') item_dict['price'] = data.get('offerprice') item_dict['quantity'] = quantity item_dict['subscription_id'] = data.get('itemid') item_dict['item_id'] = item_id item_obj = VerificationItemData(**item_dict) item_list.append(item_obj) return True, item_list, None