def create_user(args, admin_token_data): try: url = str(app.config['KEYCLOAK_URL'] + app.config['KEYCLOAK_PORT'] + app.config['KEYCLOAK_USERS']) headers = { 'Content-Type': 'application/json', 'Authorization': 'bearer ' + admin_token_data['access_token'], 'Connection': 'keep-alive' } # Dictionary for creating new user create_user_data = { "firstName": args['cnic'], "email": args['cnic'] + '@dirbs.com', "enabled": "true", "username": args['cnic'] } new_user_info = session.post(url=url, data=json.dumps(create_user_data), headers=headers) if new_user_info.status_code == 201: # 201 for user creation # print('New user created with status 201') return True else: app.logger.info( "create_user_and_return API failed due to status other than 201" ) return False except (ConnectionError, Exception) as e: # print("exception executed in third call") app.logger.exception(e)
def get_admin_token(): try: url = str(app.config['KEYCLOAK_URL'] + app.config['KEYCLOAK_PORT'] + app.config['KEYCLOAK_TOKEN']) # batch dict for token req_data = { "username": "******", "password": "******", "grant_type": "password", "client_id": "admin-cli" } headers = {'content-type': 'application/x-www-form-urlencoded'} admin_token_response = session.post(url=url, data=req_data, headers=headers) if admin_token_response.status_code == 200: admin_response_json = admin_token_response.json() return admin_response_json else: return app.logger.info( "Get Admin Token failed due to status other than 200") except (ConnectionError, Exception) as e: app.logger.exception(e)
def send_batch(messages_list, network): try: url = str(app.config['JASMIN_URL'] + app.config['JASMIN_PORT']+app.config['JASMIN_BATCH_SEND']) # get operator authorization token network = Ussd_helper.return_operator_token(network) sms_body = {"messages": messages_list} headers = {'content-type': 'application/json', 'Authorization': 'Basic ' + str(network)} # headers = {'content-type': 'application/json', 'Authorization': 'Basic Zm9vOmJhcg=='} jasmin_send_response = session.post( url=url, data=json.dumps(sms_body), headers=headers) if jasmin_send_response: if jasmin_send_response.status_code == 200: return jasmin_send_response else: app.logger.info(jasmin_send_response) return jasmin_send_response else: print(jasmin_send_response) return app.logger.info(jasmin_send_response) except (ConnectionError, Exception) as e: print("Jasmin Send API threw an Exception") app.logger.exception(e)
def send(sender, network, message): try: url = str(app.config['JASMIN_URL'] + app.config['JASMIN_PORT']+app.config['JASMIN_INDIVIDUAL_SEND']) # message body sms_body = { "to": sender, "from": "DIRBS-DRS", "coding": 8, "content": message } # get operator authorization token network = Ussd_helper.return_operator_token(network) headers = {'content-type': 'application/json', 'Authorization': 'Basic '+str(network)} app.logger.info(url) jasmin_send_response = session.post( url=url, data=json.dumps(sms_body), headers=headers) if jasmin_send_response.status_code == 200: return jasmin_send_response else: print(jasmin_send_response) return app.logger.info("Jasmin status code other than 200") except (ConnectionError, Exception) as e: print("Jasmin Send API threw an Exception") app.logger.exception(e)
def remote_call(self, request_url, json=None, data=None): service = self.service_name(request_url) try: self.logger.info("Calling service", request_url=request_url, service=service) if json: return session.post(request_url, json=json, verify=True) if data: return session.post(request_url, data=data, verify=True) return session.get(request_url, verify=True) except MaxRetryError: self.logger.error("Max retries exceeded (5)", request_url=request_url) raise RetryableError except ConnectionError: self.logger.error("Connection error occurred. Retrying") raise RetryableError
def remote_call(self, request_url, json=None, data=None, headers=None, verify=True, auth=None): service = self.service_name(request_url) try: self.logger.info("Calling service", request_url=request_url, service=service) r = None if json: r = session.post( request_url, json=json, headers=headers, verify=verify, auth=auth) elif data: r = session.post( request_url, data=data, headers=headers, verify=verify, auth=auth) else: r = session.get(request_url, headers=headers, verify=verify, auth=auth) return r except MaxRetryError: self.logger.error("Max retries exceeded (5)", request_url=request_url) raise RetryableError except ConnectionError: self.logger.error("Connection error occurred. Retrying") raise RetryableError
def get_records(imeis, records, unprocessed_imeis): """Compile IMEIs batch responses from DIRBS core system.""" try: while imeis: imei = imeis.pop(-1) # pop the last item from queue try: if imei: batch_req = {"imeis": imei} headers = { 'content-type': 'application/json', 'charset': 'utf-8', 'keep_alive': 'false' } app.logger.info('{}/imei-batch'.format( app.config['CORE_BASE_URL'])) imei_response = session.post( '{}/imei-batch'.format( app.config['CORE_BASE_URL']), data=json.dumps(batch_req), headers=headers) # dirbs core batch api call if imei_response.status_code == 200: imei_response = imei_response.json() records.extend(imei_response['results']) else: app.logger.info( "imei batch failed due to status other than 200" ) unprocessed_imeis.append( imei ) # in case of connection error append imei count to unprocessed IMEIs list else: continue except (ConnectionError, Exception) as e: unprocessed_imeis.append( imei ) # in case of connection error append imei count to unprocessed IMEIs list app.logger.exception(e) except Exception as error: raise error
async def make_post_req_bin(url, json): async with session.post(url, json=json) as resp: bytes = await resp.content.read() return bytes
async def make_post_req_json(url, json): async with session.post(url, json=json) as resp: json_resp = await resp.json() return json_resp
def get_records(imeis, records, unprocessed_imeis): """Compile IMEIs batch responses from DIRBS core system.""" try: while imeis: imei = imeis.pop(-1) # pop the last item from queue try: if imei: batch_req = { "imeis": imei, "include_registration_status": True, "include_stolen_status": True } headers = { 'content-type': 'application/json', 'charset': 'utf-8', 'keep_alive': 'false' } app.logger.info('{}/imei-batch'.format( app.config['CORE_BASE_URL'] + app.config['API_VERSION'])) imei_response = session.post( '{}/imei-batch'.format( app.config['CORE_BASE_URL'] + app.config['API_VERSION']), data=json.dumps(batch_req), headers=headers) # dirbs core batch api call if imei_response.status_code == 200: imei_response = imei_response.json() # if imei_per_device is not None: # imeis_for_multi_sim_check = [imei[x:x + imei_per_device] for x in range(0, len(imei), # imei_per_device)] # multi_sim_result = MultiSimCheck.validate_imeis_capacity(app.config['CORE_BASE_URL'], # app.config['API_VERSION'], # imeis_for_multi_sim_check) # # if len(multi_sim_result) > 0 and multi_sim_result[0] == False: # imei_response['results'][0]['multi_sim_matched'] = multi_sim_result[0] # imei_response['results'][0]['multi_sim_info'] = multi_sim_result[1] # elif len(multi_sim_result) > 0: # imei_response['results'][0]['multi_sim_matched'] = multi_sim_result[0] records.extend(imei_response['results']) else: app.logger.info( "imei batch failed due to status other than 200" ) unprocessed_imeis.append( imei ) # in case of connection error append imei count to unprocessed IMEIs list else: continue except (ConnectionError, Exception) as e: unprocessed_imeis.append( imei ) # in case of connection error append imei count to unprocessed IMEIs list app.logger.exception(e) except Exception as error: raise error
async def get_chat_info(token): url = await utils.join_uri((config.BOT_URI, "api", "chats", "info")) json = {"token": token} async with session.post(url, json=json) as resp: json_resp = await resp.json() return json_resp
async def verify_token(token: str): url = await utils.join_uri((config.BOT_URI, "api", "tokens", "verify")) json = {"token": token} async with session.post(url, json=json) as resp: json_resp = await resp.json() return json_resp["result"]