async def add(self, request: web.Request): if request.method == "POST": logging.info("Received request to ADD bot") if "Authorization" not in request.headers: raise web.HTTPUnauthorized(reason="Failed to provide token!") # Token was omitted from the headers raise web.HTTPServiceUnavailable() else: raise web.HTTPServiceUnavailable()
class CameraView(HomeAssistantView): """Base CameraView.""" requires_auth = False def __init__(self, component: EntityComponent) -> None: """Initialize a basic camera view.""" self.component = component async def get(self, request: web.Request, entity_id: str) -> web.StreamResponse: """Start a GET request.""" if (camera := self.component.get_entity(entity_id)) is None: raise web.HTTPNotFound() camera = cast(Camera, camera) authenticated = (request[KEY_AUTHENTICATED] or request.query.get("token") in camera.access_tokens) if not authenticated: # Attempt with invalid bearer token, raise unauthorized # so ban middleware can handle it. if hdrs.AUTHORIZATION in request.headers: raise web.HTTPUnauthorized() # Invalid sigAuth or camera access token raise web.HTTPForbidden() if not camera.is_on: _LOGGER.debug("Camera is off") raise web.HTTPServiceUnavailable() return await self.handle(request, camera)
async def do_chat(self, chat, dev_id, ai_id, topic, history, ai_hash, entities): item = self.collection.get_item(dev_id, ai_id) if item is None: # one attempt to load data ai_hash = None item = await self.collection.load_training_data_async( dev_id, ai_id) if item is None: raise web.HTTPNotFound() try: resp = await item.chat(chat, topic, history, ai_hash, entities) except ait_common.ChatStateError as exc: ait_http.raise_bad_request_status(exc.message, exc.state, exc.aiid) except ait_common.ChatAiHashError as exc: ait_http.raise_bad_request(str(exc)) except ait_common.ChatOverloadedError as exc: error_data = exc.error_data error_data['message'] = exc.message data = {'status': '503', 'error': error_data} data_str = json.dumps(data) raise web.HTTPServiceUnavailable(text=data_str) return resp
async def create_cluster_handler(request: web.Request) -> web.Response: await extract_and_validate(request) user_id: UserID = request[RQT_USERID_KEY] body = await request.json() assert body # nosec try: new_cluster = ClusterCreate( name=body.get("name", ""), description=body.get("description"), type=body.get("type"), endpoint=body.get("endpoint"), authentication=body.get("authentication"), owner=None, thumbnail=body.get("thumbnail", None), ) created_cluster = await director_v2_api.create_cluster( request.app, user_id=user_id, new_cluster=new_cluster) return web.json_response( data={"data": created_cluster}, status=web.HTTPCreated.status_code, dumps=json_dumps, ) except ValidationError as exc: raise web.HTTPUnprocessableEntity( reason=f"Invalid cluster definition: {exc} ") from exc except DirectorServiceError as exc: raise web.HTTPServiceUnavailable(reason=f"{exc}") from exc
async def error_middleware(request, handler): resp = web.HTTPInternalServerError() try: resp = await handler(request) except web.HTTPException as ex: resp = ex except: traceback.print_exc() resp = web.HTTPInternalServerError() if resp is None or resp.status < 400: return resp if resp.status == 403: raise web.HTTPForbidden(text='( ・∀・)つ=≡≡ξ)Д`) 403') if resp.status == 404: raise web.HTTPNotFound(text='(゚ペ) 404') if resp.status == 412: raise web.HTTPPreconditionFailed(text='Not started yet. 412') if resp.status < 500: raise web.HTTPBadRequest(text='¯\_༼ ಥ ‿ ಥ ༽_/¯ 500') if resp.status == 503: raise web.HTTPServiceUnavailable( text='' 'Congrats\n' 'You might have crashed the server.\n' 'There is no flag on this server now.\n' '(σ゚∀゚)σ゚∀゚)σ゚∀゚)σ\n' '(σ゚∀゚)σ゚∀゚)σ゚∀゚)σ\n' '(σ゚∀゚)σ゚∀゚)σ゚∀゚)σ\n' '(σ゚∀゚)σ゚∀゚)σ゚∀゚)σ\n' '\n' 'Please contact admin if you stop all your exploits\n' 'but the problem still exists in the next round.\n') raise web.HTTPInternalServerError(text='((((;゚Д゚))) wtf')
class CameraView(HomeAssistantView): """Base CameraView.""" requires_auth = False def __init__(self, component: EntityComponent) -> None: """Initialize a basic camera view.""" self.component = component async def get(self, request: web.Request, entity_id: str) -> web.StreamResponse: """Start a GET request.""" if (camera := self.component.get_entity(entity_id)) is None: raise web.HTTPNotFound() camera = cast(Camera, camera) authenticated = (request[KEY_AUTHENTICATED] or request.query.get("token") in camera.access_tokens) if not authenticated: raise web.HTTPUnauthorized() if not camera.is_on: _LOGGER.debug("Camera is off") raise web.HTTPServiceUnavailable() return await self.handle(request, camera)
async def make_request_and_envelope_response( app: web.Application, method: str, url: URL, headers: Optional[Dict[str, str]] = None, data: Optional[bytes] = None, ) -> web.Response: """ Helper to forward a request to the catalog service """ session = get_client_session(app) try: async with session.request(method, url, headers=headers, data=data) as resp: payload = await resp.json() try: resp.raise_for_status() resp_data = wrap_as_envelope(data=payload) except ClientResponseError as err: if 500 <= err.status: raise err resp_data = wrap_as_envelope(error=payload["errors"]) return web.json_response(resp_data, status=resp.status) except (asyncio.TimeoutError, ClientConnectionError, ClientResponseError) as err: logger.warning("Catalog service errors upon request %s %s: %s", method, url.relative(), err) raise web.HTTPServiceUnavailable( reason="catalog is currently unavailable") from err
async def get_service( app: web.Application, user_id: int, service_key: str, service_version: str, product_name: str, ) -> Dict[str, Any]: session: ClientSession = get_client_session(app) settings: CatalogSettings = get_plugin_settings(app) url = (URL(settings.api_base_url) / f"services/{urllib.parse.quote_plus(service_key)}/{service_version}" ).with_query({"user_id": user_id}) try: async with session.get(url, headers={X_PRODUCT_NAME_HEADER: product_name}) as resp: resp.raise_for_status( ) # FIXME: error handling for session and response exceptions return await resp.json() except asyncio.TimeoutError as err: logger.warning("Catalog service connection timeout error") raise web.HTTPServiceUnavailable( reason="catalog is currently unavailable") from err
async def metrics(request): dht11 = DHT11(pin=request.app['gpio_pin']) retries = 0 while True: result = dht11.read() if result.is_valid(): break if retries > 3: raise web.HTTPServiceUnavailable() retries += 1 time.sleep(2) sensor_name = request.app['sensor_name'] out = list() out.append(f"# TYPE sensor_temp_celsius gauge") out.append( f"sensor_temp_celsius{{sensor=\"{sensor_name}\"}} {result.temperature}" ) out.append(f"# TYPE sensor_humidity_percent gauge") out.append( f"sensor_humidity_percent{{sensor=\"{sensor_name}\"}} {result.humidity}" ) return web.Response(text='\n'.join(out))
async def copy_data_folders_from_project(app, source_project, destination_project, nodes_map, user_id): # TODO: optimize if project has actualy data or not before doing the call client, api_endpoint = _get_storage_client(app) # /simcore-s3/folders: url = (api_endpoint / "simcore-s3/folders").with_query(user_id=user_id) async with client.post( url, json={ "source": source_project, "destination": destination_project, "nodes_map": nodes_map, }, ssl=False, ) as resp: payload = await resp.json() updated_project, error = unwrap_envelope(payload) if error: msg = "Cannot copy project data in storage: %s" % pformat(error) log.error(msg) # TODO: should reconstruct error and rethrow same exception as storage service? raise web.HTTPServiceUnavailable(reason=msg) return updated_project
def setup_statics(app: web.Application): log.debug("Setting up %s ...", __name__) # TODO: Should serving front-end ria be configurable? # Front-end Rich Interface Application (RIA) try: outdir = get_client_outdir(app) # Checks integrity of RIA source before serving EXPECTED_FOLDERS = ('qxapp', 'resource', 'transpiled') folders = [x for x in outdir.iterdir() if x.is_dir()] for name in EXPECTED_FOLDERS: folder_names = [path.name for path in folders] if name not in folder_names: raise web.HTTPServiceUnavailable( reason="Invalid front-end source-output folders" \ " Expected %s, got %s in %s" %(EXPECTED_FOLDERS, folder_names, outdir), text ="Front-end application is not available" ) # TODO: map ui to /ui or create an alias!? app.router.add_get("/", index, name=INDEX_RESOURCE_NAME) # NOTE: source-output and build-output have both the same subfolder structure # TODO: check whether this can be done at oncen for path in folders: app.router.add_static('/' + path.name, path) except web.HTTPServiceUnavailable as ex: log.exception(ex.text) return
async def send_list(request) -> json: """send a list of schemes and relevent data""" region = request.match_info['region'] try: KeyList=list(KEYS[region]) offset = -1 try: req_range = int(request.rel_url.query.get('range')) try: fromSchemeid = request.rel_url.query.get('fromSchemeid') offset = KeyList.index(fromSchemeid) except Exception as e: handle_exception(e) pass except Exception as e: handle_exception(e) req_range = 40 # len(app.config['shared_keys'][region]) li = [] try: for i in range(1, req_range + 1): li.append(json.load(open(os.path.join('DATA', region, KeyList[i + offset], 'index')))) except Exception as e: handle_exception(e) pass # process = psutil.Process(os.getpid()) # print(process.memory_info().rss) if len(li) == 0: raise web.HTTPServiceUnavailable(content_type='application/json') return web.json_response(li) except Exception as e: handle_exception(e) raise web.HTTPBadRequest(content_type='application/json')
def raise_http_unavailable_error(retry_state: RetryCallState): # TODO: mark incident on db to determine the quality of service. E.g. next time we do not stop. TIP: obj, query = retry_state.args; obj.app.register_incidents exc: DatabaseError = retry_state.outcome.exception() # StandardError # |__ Warning # |__ Error # |__ InterfaceError # |__ DatabaseError # |__ DataError # |__ OperationalError # |__ IntegrityError # |__ InternalError # |__ ProgrammingError # |__ NotSupportedError # # aiopg reuses DBAPI exceptions # SEE https://aiopg.readthedocs.io/en/stable/core.html?highlight=Exception#exceptions # SEE http://initd.org/psycopg/docs/module.html#dbapi-exceptions # TODO: add header with Retry-After https://tools.ietf.org/html/rfc7231#section-7.1.3 resp = web.HTTPServiceUnavailable() # logs msg = f"Postgres service non-responsive, responding {resp.status_code}: {str(exc) or repr(exc)}" log.error(msg) raise resp
async def _request_catalog( app: web.Application, method: str, url: URL, headers: Optional[Dict[str, str]] = None, data: Optional[bytes] = None, ) -> web.Response: session = get_client_session(app) try: async with session.request(method, url, headers=headers, data=data) as resp: is_error = resp.status >= 400 # catalog backend sometimes sends error in plan=in text try: payload: Dict = await resp.json() except ContentTypeError: payload = await resp.text() is_error = True if is_error: # Only if error, it wraps since catalog service does # not return (for the moment) enveloped data = wrap_as_envelope(error=payload) else: data = wrap_as_envelope(data=payload) return web.json_response(data, status=resp.status) except (CancelledError, TimeoutError) as err: raise web.HTTPServiceUnavailable( reason="unavailable catalog service") from err
async def get_request(self, request): ''' Handle the request from client if leader, otherwise redirect to the leader. ''' self._log.info("%d: on request", self._index) if not self._is_leader: if self._leader != None: raise web.HTTPTemporaryRedirect( self.make_url(self._nodes[self._leader], MessageType.REQUEST)) else: raise web.HTTPServiceUnavailable() else: # print(request.headers) # print(request.__dict__) json_data = await request.json() # print("\t\t--->node"+str(self._index)+": on request :") # print(json_data) await self.preprepare(json_data) return web.Response()
async def get_services_for_user_in_product( app: web.Application, user_id: int, product_name: str, *, only_key_versions: bool) -> List[Dict]: url = (URL(app[KCATALOG_ORIGIN]).with_path(app[KCATALOG_VERSION_PREFIX] + "/services").with_query({ "user_id": user_id, "details": f"{not only_key_versions}" })) session = get_client_session(app) try: async with session.get( url, headers={X_PRODUCT_NAME_HEADER: product_name}, ) as resp: if resp.status >= 400: logger.warning( "Error while retrieving services for user %s. Returning an empty list", user_id, ) return [] return await resp.json() except asyncio.TimeoutError as err: logger.warning("Catalog service connection timeout error") raise web.HTTPServiceUnavailable( reason="catalog is currently unavailable") from err
async def update_service( app: web.Application, user_id: int, service_key: str, service_version: str, product_name: str, update_data: Dict[str, Any], ) -> Dict[str, Any]: url = (URL(app[KCATALOG_ORIGIN]).with_path( app[KCATALOG_VERSION_PREFIX] + f"/services/{urllib.parse.quote_plus(service_key)}/{service_version}"). with_query({ "user_id": user_id, })) session = get_client_session(app) try: async with session.patch(url, headers={X_PRODUCT_NAME_HEADER: product_name}, json=update_data) as resp: resp.raise_for_status( ) # FIXME: error handling for session and response exceptions return await resp.json() except asyncio.TimeoutError as err: logger.warning("Catalog service connection timeout error") raise web.HTTPServiceUnavailable( reason="catalog is currently unavailable") from err
async def api(params): app.counter += 1 request_id = app.counter query_text = pretty(params) click.secho('[{}] {}'.format(request_id, query_text), fg='cyan') async with app.lock: await refresh_token() params.update(token=app.token, format='json_extended', app_id=APP_ID) data = await fetch_json(API_ENDPOINT, params=params) error, results = data.get('error'), data.get('torrent_results') if error: click.secho('[{}] {}'.format(request_id, error), fg='red') if 'get_token' in error: await refresh_token() else: return web.HTTPServiceUnavailable(text=error) for i in results: i.update( pubdate=formatdate(parser.parse(i['pubdate']).timestamp()), hsize=naturalsize(i['size'], gnu=True), hash=parse_qs(i['download'])['magnet:?xt'][0].split(':')[-1], ) click.secho('[{}] {} results'.format(request_id, len(results)), fg='green') result = TEMPLATE.render(title='rarbg', entries=results) return web.Response(text=result, content_type='text/xml')
async def ready_middleware(request: web.BaseRequest, handler: Coroutine): """Only continue if application is ready to take work.""" if (str(request.rel_url).rstrip("/") in ( "/status/live", "/status/ready", ) or request.app._state.get("ready")): try: return await handler(request) except (LedgerConfigError, LedgerTransactionError) as e: # fatal, signal server shutdown LOGGER.error("Shutdown with %s", str(e)) request.app._state["ready"] = False request.app._state["alive"] = False raise except web.HTTPFound as e: # redirect, typically / -> /api/doc LOGGER.info("Handler redirect to: %s", e.location) raise except asyncio.CancelledError: # redirection spawns new task and cancels old LOGGER.debug("Task cancelled") raise except Exception as e: # some other error? LOGGER.error("Handler error with exception: %s", str(e)) import traceback print("\n=================") traceback.print_exc() raise raise web.HTTPServiceUnavailable(reason="Shutdown in progress")
async def index(request): try: async with request.app['db'].acquire() as conn: currencies = await get_all_currency(conn) return web.Response(text=str(currencies)) except OperationalError: request.app['logger'].exception('index - error') return web.HTTPServiceUnavailable(text='Something went wrong while fetching the currencies')
async def list_clusters_handler(request: web.Request) -> web.Response: await extract_and_validate(request) user_id: UserID = request[RQT_USERID_KEY] try: data = await director_v2_api.list_clusters(request.app, user_id) return web.json_response(data={"data": data}, dumps=json_dumps) except DirectorServiceError as exc: raise web.HTTPServiceUnavailable(reason=f"{exc}") from exc
async def is_env_ready(request: web.Request): """Responds with 503 until all the services we wait for come online. Then it returns 204. """ if request.app[ENV_READY_FLAG]: return web.HTTPNoContent() else: return web.HTTPServiceUnavailable( text="The environment isn't ready yet.")
async def get_calibration_request(self, request): influx = request.app["influx-db"] _, calibration = await self.get_last(influx, "calibration") slope, offset = json.loads(calibration) if calibration is None: return web.HTTPServiceUnavailable(reason="No calibration data avialable.") return web.json_response({"slope": slope, "offset": offset})
async def request_raise_transient_errors(session, method, url, **kwargs): try: return await session.request(method, url, **kwargs) except Exception as e: if is_transient_error(e): log.exception('request failed with transient exception: {method} {url}') raise web.HTTPServiceUnavailable() raise
async def open_project(request: web.Request) -> web.Response: user_id: int = request[RQT_USERID_KEY] try: project_uuid = request.match_info["project_id"] client_session_id = await request.json() except KeyError as err: raise web.HTTPBadRequest( reason=f"Invalid request parameter {err}") from err except json.JSONDecodeError as exc: raise web.HTTPBadRequest(reason="Invalid request body") from exc try: project = await projects_api.get_project_for_user( request.app, project_uuid=project_uuid, user_id=user_id, include_templates=False, include_state=True, ) if not await projects_api.try_open_project_for_user( user_id, project_uuid=project_uuid, client_session_id=client_session_id, app=request.app, ): raise HTTPLocked(reason="Project is locked, try later") # user id opened project uuid await projects_api.start_project_interactive_services( request, project, user_id) # notify users that project is now opened project = await projects_api.add_project_states_for_user( user_id=user_id, project=project, is_template=False, app=request.app, ) await projects_api.notify_project_state_update(request.app, project) return web.json_response({"data": project}, dumps=json_dumps) except ProjectNotFoundError as exc: raise web.HTTPNotFound( reason=f"Project {project_uuid} not found") from exc except DirectorServiceError as exc: # there was an issue while accessing the director-v2/director-v0 # ensure the project is closed again await projects_api.try_close_project_for_user( user_id=user_id, project_uuid=project_uuid, client_session_id=client_session_id, app=request.app, ) raise web.HTTPServiceUnavailable( reason="Unexpected error while starting services.") from exc
async def get_weather(request): t_s = time() url_data = request.query town_name = url_data.get("town_name", None) if not isinstance(town_name, str): await log( request=request, time=time() - t_s, code=400 ) raise web.HTTPBadRequest() open_weather_request = \ f"https://api.openweathermap.org/data/2.5/weather?q=" \ f"{town_name}" \ f"&APPID=" \ f"{open_weather_config['api_key']}" \ f"&units=" \ f"{open_weather_config['units']}" weather_data = {} async with request_session.get(open_weather_request) as resp: if resp.status == 500: await log( request=request, time=time() - t_s, code=503 ) raise web.HTTPServiceUnavailable() elif resp.status == 404: await log( request=request, time=time() - t_s, code=400 ) raise web.HTTPBadRequest() else: weather_data = await resp.json() temp = weather_data.get("main", {}).get("temp", 0.0) result = { "town": town_name, "temp": temp, "units": "metric" } await log( request=request, result=result, time=time() - t_s ) return json_response(result)
async def register(request: web.Request): _, _, body = await extract_and_validate(request) # see https://aiohttp.readthedocs.io/en/stable/web_advanced.html#data-sharing-aka-no-singletons-please db = get_storage(request.app) email = body.email username = email.split('@')[0] password = body.password confirm = body.confirm await validate_registration(email, password, confirm, db) user = await db.create_user({ 'name': username, 'email': email, 'password_hash': encrypt_password(password), 'status': CONFIRMATION_PENDING if bool(cfg.REGISTRATION_CONFIRMATION_REQUIRED) else ACTIVE, 'role': USER, 'created_ip': get_client_ip(request), }) if not bool(cfg.REGISTRATION_CONFIRMATION_REQUIRED): # user is logged in identity = body.email response = flash_response(cfg.MSG_LOGGED_IN, "INFO") await remember(request, response, identity) return response confirmation_ = await db.create_confirmation(user, REGISTRATION) link = await make_confirmation_link(request, confirmation_) try: await render_and_send_mail( request, email, themed('registration_email.html'), { 'auth': { 'cfg': cfg, }, 'host': request.host, 'link': link, 'name': email.split("@")[0], }) except Exception: #pylint: disable=broad-except log.exception('Can not send email') await db.delete_confirmation(confirmation_) await db.delete_user(user) raise web.HTTPServiceUnavailable(reason=cfg.MSG_CANT_SEND_MAIL) response = flash_response( "You are registered successfully! To activate your account, please, " "click on the verification link in the email we sent you.", "INFO") return response
async def ready_middleware(request: web.BaseRequest, handler: Coroutine): """Only continue if application is ready to take work.""" if str(request.rel_url).rstrip("/") in ( "/status/live", "/status/ready", ) or request.app._state.get("ready"): return await handler(request) raise web.HTTPServiceUnavailable(reason="Shutdown in progress")
async def get_projects(request: web.Request): print("new_profile") post = await request.post() projects = await try_get_all(post, 'project', str) print(projects) projects_data = await database.get_data_from_projects(projects) if projects_data is False: raise web.HTTPServiceUnavailable( reason=f'You can have maximum 10 projects') return web.json_response(projects_data, dumps=dumps)
async def run_code(self, *args: Any, **kwargs: Any) -> _ResultType: if self.busy: raise web.HTTPServiceUnavailable( reason="No free containers. Try again later") self._running_containers += 1 try: return await self._run_container(*args, **kwargs) finally: self._running_containers -= 1