def imei_api(imei: str, include_seen_with: bool = False, include_paired_with: bool = False) -> jsonify: """ IMEI API handler. Arguments: imei: IMEI number in format [15, 16] digits include_seen_with: bool to include seen with information in response (default False) include_paired_with: bool to include paired with information in response (default False) Returns: JSON response """ imei_norm = validate_imei(imei) tac = imei_norm[:8] with get_db_connection() as db_conn, db_conn.cursor() as cursor: cursor.execute('SELECT NOT EXISTS (SELECT * FROM gsma_data WHERE tac = %s) AS not_in_gsma', [tac]) rt_gsma_not_found = cursor.fetchone()[0] condition_results = get_conditions(cursor, imei_norm) resp = { 'imei_norm': imei_norm, 'classification_state': { 'blocking_conditions': {k: v['result'] for k, v in condition_results.items() if v['blocking']}, 'informative_conditions': {k: v['result'] for k, v in condition_results.items() if not v['blocking']} }, 'realtime_checks': { 'invalid_imei': False if re.match(r'^\d{14}$', imei_norm) else True, 'gsma_not_found': rt_gsma_not_found } } # add a real-time check for the registration list resp['realtime_checks']['in_registration_list'] = is_in_registration_list(db_conn, cursor, imei_norm) # add a real-time check for if IMEI was ever observed on the network resp['realtime_checks']['ever_observed_on_network'] = ever_observed_on_network(cursor, imei_norm) resp['is_paired'] = is_paired(cursor, imei_norm) if include_seen_with: resp['seen_with'] = get_subscribers(cursor, imei_norm) if include_paired_with: cursor.execute("""SELECT imsi FROM pairing_list WHERE imei_norm = %(imei_norm)s AND virt_imei_shard = calc_virt_imei_shard(%(imei_norm)s)""", {'imei_norm': imei_norm}) resp['paired_with'] = [x.imsi for x in cursor] return jsonify(IMEI().dump(resp).data)
def imei_batch_api(**kwargs: dict) -> jsonify: """ IMEI API POST method handler for IMEI-Batch request. Arguments: kwargs: required arguments (list of IMEIs) Returns: JSON response """ imeis = kwargs.get('imeis') include_registration_status = kwargs.get('include_registration_status') include_stolen_status = kwargs.get('include_stolen_status') data = [] with get_db_connection() as db_conn, db_conn.cursor() as cursor: for imei in imeis: imei_norm = validate_imei(imei) tac = imei_norm[:8] condition_results = get_conditions(cursor, imei_norm) first_seen_date = first_seen(cursor, imei_norm) cursor.execute( 'SELECT NOT EXISTS (SELECT * FROM gsma_data WHERE tac = %s) AS not_in_gsma', [tac]) rt_gsma_not_found = cursor.fetchone()[0] response = { 'imei_norm': imei_norm, 'block_date': block_date(cursor, imei_norm), 'first_seen': first_seen_date, 'classification_state': { 'blocking_conditions': [ dict({ 'condition_name': key, 'condition_met': value['result'] }) for key, value in condition_results.items() if value['blocking'] ], 'informative_conditions': [ dict({ 'condition_name': key, 'condition_met': value['result'] }) for key, value in condition_results.items() if not value['blocking'] ] }, 'realtime_checks': { 'ever_observed_on_network': True if first_seen_date else False, 'invalid_imei': False if re.match(r'^\d{14}$', imei_norm) else True, 'is_paired': is_paired(cursor, imei_norm), 'is_exempted_device': is_exempted_device(cursor, imei_norm), 'gsma_not_found': rt_gsma_not_found, 'in_registration_list': is_in_registration_list(db_conn, cursor, imei_norm) } } if include_registration_status: response['registration_status'] = registration_list_status( cursor, imei_norm) if include_stolen_status: response['stolen_status'] = stolen_list_status( cursor, imei_norm) data.append(IMEI().dump(response).data) return jsonify({'results': data})
def imei_api(imei: str, include_registration_status: bool = False, include_stolen_status: bool = False) -> jsonify: """ IMEI API handler. Arguments: imei: value of the IMEI include_registration_status: boolean weather to include reg status or not (default False) include_stolen_status: boolean weather to include stolen status or not (default False) Returns: JSON response """ imei_norm = validate_imei(imei) tac = imei_norm[:8] tac = imei_norm[:8] with get_db_connection() as db_conn, db_conn.cursor() as cursor: cursor.execute( 'SELECT NOT EXISTS (SELECT * FROM gsma_data WHERE tac = %s) AS not_in_gsma', [tac]) rt_gsma_not_found = cursor.fetchone()[0] first_seen_date = first_seen(cursor, imei_norm) condition_results = get_conditions(cursor, imei_norm) response = { 'imei_norm': imei_norm, 'block_date': block_date(cursor, imei_norm), 'first_seen': first_seen_date, 'classification_state': { 'blocking_conditions': [ dict({ 'condition_name': key, 'condition_met': value['result'] }) for key, value in condition_results.items() if value['blocking'] ], 'informative_conditions': [ dict({ 'condition_name': key, 'condition_met': value['result'] }) for key, value in condition_results.items() if not value['blocking'] ] }, 'realtime_checks': { 'ever_observed_on_network': True if first_seen_date else False, 'invalid_imei': False if re.match(r'^\d{14}$', imei_norm) else True, 'is_paired': is_paired(cursor, imei_norm), 'is_exempted_device': is_exempted_device(cursor, imei_norm), 'in_registration_list': is_in_registration_list(db_conn, cursor, imei_norm), 'gsma_not_found': rt_gsma_not_found } } if include_registration_status: response['registration_status'] = registration_list_status( cursor, imei_norm) if include_stolen_status: response['stolen_status'] = stolen_list_status(cursor, imei_norm) return jsonify(IMEI().dump(response).data)