def patch_custom_endpoints(self): try: custom_endpoints = settings.custom_endpoints url = settings.webhook_url data = {'quote_actions': {}} for endpoint in custom_endpoints: data['quote_actions'].update({ endpoint['namespace']: f"{settings.schema_pub}://{settings.host_pub}/" f"{settings.api_version}{endpoint['uri']}" }) if data['quote_actions']: logger.debug(f"[patch_custom_endpoints] Initiated PATCH - {url}") logger.verbose("\n{}\n".format(json.dumps(data, indent=4, sort_keys=True))) resp = requests.patch(url, data=json.dumps(data), headers=self.session.headers) logger.verbose("[patch_{}] Received response code[{}]".format(endpoint['namespace'], resp.status_code)) logger.verbose("\n{}\n".format(json.dumps(resp.json(), indent=4, sort_keys=True))) if int(resp.status_code) == 200: logger.notice(f"[patch_custom_endpoints] {endpoint['namespace']} setup successful!") else: raise Exception(f"[patch_custom_endpoints] {endpoint['namespace']} setup not successful!") except Exception: logger.alert("Failed at patch endpoint! {}".format(traceback.format_exc(limit=5))) raise
def get_access(): """ To send authorization request with 0Auth2.0 to Muzzley platform """ logger.verbose("Trying to authorize with Muzzley...") data = { "client_id": settings.client_id, "client_secret": settings.client_secret, "response_type": settings.grant_type, "scope": settings.scope, "state": "active" } url = settings.auth_url try: logger.debug("Initiated POST - {}".format(url)) resp = requests.post(url, data=data) if resp.status_code == 200: logger.notice("Manager succesfully Authorized with Muzzley") store_info(resp.json()) start_refresher() else: error_msg = format_response(resp) raise Exception(error_msg) except Exception: logger.alert("Unexpected error during authorization {}".format( traceback.format_exc(limit=5))) raise
def on_connect(self, client, userdata, flags, rc): try: if rc == 0: logger.debug("Mqtt - Connected , result code {}".format(rc)) topic = "/{api_version}/{mqtt_topic}/{client_id}/channels/#".format( mqtt_topic=settings.mqtt_topic, api_version=settings.api_version, client_id=settings.client_id) if self.subscribe: logger.notice("Mqtt - Will subscribe to {}".format(topic)) self.mqtt_client.subscribe(topic, qos=0) if self._on_connect_callback: self._on_connect_callback.__call__( **self._on_connect_callback_params) elif 0 < rc < 6: raise Exception(RC_LIST[rc]) except Exception: logger.error("Mqtt Exception- {}".format( traceback.format_exc(limit=5))) os._exit(1)
def clear_hash(self): try: self.delete(settings.redis_db) logger.notice("[DB] Redis database shutdown.") except Exception: logger.error("[DB] Failed to clear redis database, {}".format( traceback.format_exc(limit=5)))
def save_n_exit(self): """ To safely exit the opened client """ try: self.shutdown() logger.notice("[DB] Redis database shutdown.") except Exception: logger.error("[DB] Failed to shutdown redis database, {}".format( traceback.format_exc(limit=5)))
def set_confirmation_hash(self): webhook_data = self.get_webhook_data() if "confirmation_hash" in webhook_data: self.confirmation_hash = webhook_data['confirmation_hash'] logger.notice( "[set_confirmation_hash] Confirmation Hash : {}".format( self.confirmation_hash)) else: raise Exception
def get_application(self): try: logger.debug(f"[get_application] Trying to get application data - {settings.webhook_url}") resp = requests.get(settings.webhook_url, headers=self.session.headers) logger.verbose("[get_application] Received response code[{}]".format(resp.status_code)) if int(resp.status_code) == 200: logger.notice("[get_application] Get application successful!") return resp.json() else: raise Exception('[get_application] Error getting application!') except Exception: logger.alert("Failed while get application! {}".format(traceback.format_exc(limit=5))) raise
def send_request(self, conf_data, channel_id): try: # validate if channel exists credentials_list = self.db.full_query( 'credential-owners/*/channels/{}'.format(channel_id)) logger.info('[Polling] {} results found for channel_id: {}'.format( len(credentials_list), channel_id)) for credential_dict in credentials_list: # try until we find valid credentials cred_key = credential_dict['key'] credentials = credential_dict['value'] is_valid = self.validate_channel(cred_key) if not is_valid: logger.debug( '[Polling] Invalid channel {}'.format(cred_key)) continue # Validate if token is valid before the request now = int(time.time()) token_expiration_date = credentials['expiration_date'] if now > token_expiration_date and not token_expiration_date == 0: logger.debug( "[Polling] access token expired {} - now:{}, expiration:{}" .format(cred_key, now, token_expiration_date)) continue resp_list = [] results = self.pool_requests.starmap( self.get_response, zip(conf_data, repeat(credentials), repeat(channel_id), repeat(cred_key))) resp_list.extend([result for result in results if result]) if resp_list: return resp_list except requests.exceptions.RequestException as e: logger.error('Request Error on polling.send_request {}'.format(e)) return False except Exception: logger.error( f'[Polling] Unknown error on polling.send_request {traceback.format_exc(limit=5)}' ) logger.notice( '[Polling] No valid credentials found for channel {}'.format( channel_id)) return False
def launch_server(self): logger.notice('Starting TCP server') tcp_settings = settings.config_tcp if 'ip_address' not in self.tcp_settings or 'port' not in self.tcp_settings: raise TCPServerNotFoundException( "TCP server address or port not found in config file") # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = (self.tcp_settings['ip_address'], int(self.tcp_settings['port'])) logger.info( f'starting up on {server_address[0]} port {server_address[1]}') try: sock.bind(server_address) # Listen for incoming connections sock.listen(1) thread_list = [] while True: if len(thread_list) >= self.tcp_settings.get( 'thread_pool_limit', DEFAULT_TCP_POOL_LIMIT): self._clear_threads(thread_list) # Wait for a connection logger.info('Waiting for connection') connection, client_address = sock.accept() thread_ = threading.Thread(target=self.handle_connection, args=(connection, client_address)) thread_.start() thread_list.append(thread_) self._clear_threads(thread_list) except OSError as e: logger.critical( f"Error connecting TCP. Probably because address already in use. " f"Will try to reconnect in {self.retry_wait}; Error: {e}") except Exception as e: logger.alert( f"Unexpected error while open TCP socket: {e}; {traceback.format_exc(limit=5)}" ) finally: time.sleep(self.retry_wait) logger.warning("Recreating TCP server") self.kickoff()
def webhook_registration(self): try: if self.watchdog_monitor: self.watchdog_monitor.start() if self.watchdog_monitor.thread is None else \ logger.notice("Watchdog thread alive? : {}".format(self.watchdog_monitor.thread.is_alive())) self.implementer.start() except Exception: logger.alert("Unexpected exception {}".format(traceback.format_exc(limit=5))) os._exit(1)
def patch_endpoints(self): try: _data = settings.services for _service in _data: try: if settings.config_boot.get('patch_services', True) is True: data = { 'activation_uri': '{}://{}/{}/services/{}/authorize'.format(settings.schema_pub, settings.host_pub, settings.api_version, _service['id']) } logger.debug("[patch_endpoints] Initiated PATCH - {}".format(_service.get('url'))) logger.verbose("\n{}\n".format(json.dumps(data, indent=4, sort_keys=True))) resp = requests.patch('{}/services/{}'.format(settings.api_server_full, _service['id']), data=json.dumps(data), headers=self.session.headers) logger.verbose("[patch_endpoints] Received response code[{}]".format(resp.status_code)) logger.verbose("\n{}\n".format(json.dumps(resp.json(), indent=4, sort_keys=True))) if int(resp.status_code) == 200: logger.notice("[patch_endpoints] Service setup successful!") else: raise Exception('Service setup not successful!') except Exception as e: logger.alert("[patch_endpoints] Failed to set service!\n{}".format(e)) os._exit(1) self.patch_custom_endpoints() self.set_confirmation_hash() except Exception: logger.alert("[patch_endpoints] Failed at patch endpoints! {}".format(traceback.format_exc(limit=5))) raise
def renew_token(): logger.verbose("Trying to refresh Tokens...") url = settings.renew_url header = {"Content-Type": "application/json"} data = { "client_id": settings.client_id, "refresh_token": settings.block['refresh_token'], "grant_type": settings.grant_type } try: logger.debug("Initiated POST - {}".format(url)) resp = requests.get(url, params=data, headers=header) if resp.status_code == 200: logger.notice("Manager succesfully performed Token refresh") store_info(resp.json()) start_refresher() else: error_msg = format_response(resp) raise Exception(error_msg) except Exception: logger.alert("Unexpected error during token renewal: {}".format( traceback.format_exc(limit=5))) os._exit(1)