def __init__(self): self.name = 'Not Found' self.description = ( 'The requested URL was not found on the server. ' 'If you entered the URL manually please check your spelling and ' 'try again.' ) self.code = type(self).status_code self.empty_body = True HTTPNotFound.__init__(self, reason=self.name)
async def review(request: Request): app: Application = request.app course_id = int(request.match_info['course_id']) async with app['db'].acquire() as conn: course = await Course.get(conn, course_id) if not course: raise HTTPNotFound() if request.method == 'POST': data = await request.post() review_text = data.get('review_text') if not review_text: return { 'course': course, 'errors': { 'review_text': 'this is required field', }, } await Review.create(conn, course_id, review_text) raise HTTPFound(f'/courses/{course_id}') return {'course': course, 'errors': {}}
async def evaluate(request: Request): app: Application = request.app student_id = int(request.match_info['student_id']) course_id = int(request.match_info['course_id']) data = await request.post() async with app['db'].acquire() as conn: student = await Student.get(conn, student_id) course = await Course.get(conn, course_id) if not student or not course: raise HTTPNotFound() try: data = EVALUATE_SCHEMA.check_and_return(data) except DataError as e: return { 'errors': e.as_dict(), 'course': course, 'student': student } await Mark.create(conn, student_id, course_id, data['points']) raise HTTPFound(f'/courses/{course_id}')
async def player_info(self, request): """ /channels/<channel_id>/player/<player_id> Get players data, ordered by experience """ channel = self.bot.get_channel(int(request.match_info['channel_id'])) # await self.authenticate_request(request, channel=channel) player = await Player\ .filter(channel__discord_id=channel.id, member__user__discord_id=int(request.match_info['player_id']))\ .first()\ .prefetch_related("member__user") if not player: raise HTTPNotFound(reason="Unknown player/channel/user") return web.json_response(player.serialize())
async def _stream_content_artifact(self, request, response, content_artifact): """ Stream and optionally save a ContentArtifact by requesting it using the associated remote. If a fatal download failure occurs while downloading and there are additional :class:`~pulpcore.plugin.models.RemoteArtifact` objects associated with the :class:`~pulpcore.plugin.models.ContentArtifact` they will also be tried. If all :class:`~pulpcore.plugin.models.RemoteArtifact` downloads raise exceptions, an HTTP 502 error is returned to the client. Args: request(:class:`~aiohttp.web.Request`): The request to prepare a response for. response (:class:`~aiohttp.web.StreamResponse`): The response to stream data to. content_artifact (:class:`~pulpcore.plugin.models.ContentArtifact`): The ContentArtifact to fetch and then stream back to the client Raises: :class:`~aiohttp.web.HTTPNotFound` when no :class:`~pulpcore.plugin.models.RemoteArtifact` objects associated with the :class:`~pulpcore.plugin.models.ContentArtifact` returned the binary data needed for the client. """ def get_remote_artifacts_blocking(): return list(content_artifact.remoteartifact_set.all()) remote_artifacts = await loop.run_in_executor(None, get_remote_artifacts_blocking) for remote_artifact in remote_artifacts: try: response = await self._stream_remote_artifact(request, response, remote_artifact) return response except (ClientResponseError, UnsupportedDigestValidationError) as e: log.warning( _("Could not download remote artifact at '{}': {}").format( remote_artifact.url, str(e) ) ) continue raise HTTPNotFound()
def ingress_panel(self, request: web.Request): slug = request.match_info.get("slug") if slug != self.supervisor._addon_slug: raise HTTPNotFound() body = """ <html> <head> <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <meta content="utf-8" http-equiv="encoding"> <title>Simulated Supervisor Ingress Panel</title> <style type="text/css" > iframe {{ display: block; width: 100%; height: 100%; border: 0; }} </style> </head> <body> <div> The Web-UI below is loaded through an iframe. <a href='startingress'>Start a new ingress session</a> if you get permission errors. </div> <iframe src="api/hassio_ingress/{0}/"> <html> <head></head> <body></body> </html> </iframe> </body> </html> """.format(self.addon_token) resp = web.Response(body=body, content_type="text/html") resp.set_cookie(name=COOKIE_INGRESS, value=self.cookie_value, expires="Session", domain=request.url.host, path="/api/hassio_ingress/", httponly="false", secure="false") return resp
async def DELETE_Attribute(request): """HTTP DELETE method for /(obj)/<id>/attributes/<name> """ log.request(request) app = request.app params = request.rel_url.query obj_id = get_obj_id(request) if "bucket" in params: bucket = params["bucket"] else: bucket = None attr_name = request.match_info.get('name') log.info(f"DELETE attribute {attr_name} in {obj_id} bucket: {bucket}") validateAttributeName(attr_name) obj_json = await get_metadata_obj(app, obj_id, bucket=bucket) log.debug(f"DELETE attribute obj_id: {obj_id} got json") if "attributes" not in obj_json: msg = f"unexpected data for obj id: {obj_id}" msg.error(msg) raise HTTPInternalServerError() # return a list of attributes based on sorted dictionary keys attributes = obj_json["attributes"] if attr_name not in attributes: msg = f"Attribute {attr_name} not found in objid: {obj_id}" log.warn(msg) raise HTTPNotFound() del attributes[attr_name] await save_metadata_obj(app, obj_id, obj_json, bucket=bucket) resp_json = { } resp = json_response(resp_json) log.response(request, resp=resp) return resp
async def post(self, request: web.Request, provider: str) -> web.Response: """Convert Speech (audio) to text.""" if provider not in self.providers: raise HTTPNotFound() stt_provider: Provider = self.providers[provider] # Get metadata metadata = self._metadata_from_header(request) if not metadata: raise HTTPBadRequest() # Check format if not stt_provider.check_metadata(metadata): raise HTTPUnsupportedMediaType() # Process audio stream result = await stt_provider.async_process_audio_stream( metadata, request.content) # Return result return self.json(attr.asdict(result))
async def download(self, disposition=None): if disposition is None: disposition = self.request.GET.get('disposition', 'attachment') file = self.field.get(self.field.context or self.context) if not isinstance(file, self.file_class) or not file.valid: return HTTPNotFound(text='No file found') cors_renderer = app_settings['cors_renderer'](self.request) headers = await cors_renderer.get_headers() headers.update({ 'CONTENT-DISPOSITION': f'{disposition}; filename="%s"' % file.filename }) download_resp = StreamResponse(headers=headers) download_resp.content_type = file.guess_content_type() if file.size: download_resp.content_length = file.size await download_resp.prepare(self.request) resp = await file.download(self.context, download_resp) return resp
async def getObjectJson(app, obj_id, bucket=None, refresh=False, include_links=False, include_attrs=False): """ Return top-level json (i.e. excluding attributes or links by default) for a given obj_id. If refresh is False, any data present in the meta_cache will be returned. If not the DN will be queries, and any resultant data added to the meta_cache. Note: meta_cache values may be stale, but use of immutable data (e.g. type of a dataset) is always valid """ meta_cache = app['meta_cache'] obj_json = None if include_links or include_attrs: # links and attributes are subject to change, so always refresh refresh = True log.info(f"getObjectJson {obj_id}") if obj_id in meta_cache and not refresh: log.debug(f"found {obj_id} in meta_cache") obj_json = meta_cache[obj_id] else: req = getDataNodeUrl(app, obj_id) collection = getCollectionForId(obj_id) params = {} if include_links: params["include_links"] = 1 if include_attrs: params["include_attrs"] = 1 if bucket: params["bucket"] = bucket req += '/' + collection + '/' + obj_id obj_json = await http_get(app, req, params=params) # throws 404 if doesn't exist meta_cache[obj_id] = obj_json if obj_json is None: msg = f"Object: {obj_id} not found" log.warn(msg) raise HTTPNotFound() return obj_json
def resolve(self, request): try: resource, tail = yield from self.traverse(request) exc = None except Exception as _exc: resource = None tail = None exc = _exc request.resource = resource request.tail = tail request.exc = exc if resource is not None: try: view = self.resolve_view(request, resource, tail) except ViewNotResolved: return TraversalExceptionMatchInfo(request, HTTPNotFound()) return MatchInfo(request, resource, tail, view) else: return TraversalExceptionMatchInfo(request, exc)
async def resolve(self, request: IRequest) -> MatchInfo: ''' Resolve a request ''' request.record('start') result = None try: result = await self.real_resolve(request) except HTTPException as exc: await abort(request) return BasicMatchInfo(request, exc) except Exception as e: logger.error("Exception on resolve execution", exc_info=e, request=request) await abort(request) raise e if result is not None: return result else: await abort(request) return BasicMatchInfo(request, HTTPNotFound())
async def DELETE_Attribute(request): """HTTP DELETE method for /(obj)/<id>/attributes/<name> """ log.request(request) app = request.app obj_id = get_obj_id(request) attr_name = request.match_info.get('name') log.info("DELETE attribute {} in {}".format(attr_name, obj_id)) validateAttributeName(attr_name) obj_json = await get_metadata_obj(app, obj_id) log.debug("DELETE attribute obj_id: {} got json".format(obj_id)) if "attributes" not in obj_json: msg = "unexpected data for obj id: {}".format(obj_id) msg.error(msg) raise HTTPInternalServerError() # return a list of attributes based on sorted dictionary keys attributes = obj_json["attributes"] if attr_name not in attributes: msg = "Attribute {} not found in id: {}".format(attr_name, obj_id) log.warn(msg) raise HTTPNotFound() del attributes[attr_name] await save_metadata_obj(app, obj_id, obj_json) resp_json = { } resp = json_response(resp_json) log.response(request, resp=resp) return resp
async def copy(self, to_storage_manager, to_dm): file = self.field.get(self.field.context or self.context) if file is None or file.uri is None: raise HTTPNotFound( reason='To copy a uri must be set on the object') util = get_utility(IS3BlobStore) new_uri = generate_key(self.request, self.context) await util._s3aioclient.copy_object(CopySource={ 'Bucket': file._bucket_name, 'Key': file.uri }, Bucket=file._bucket_name, Key=new_uri) await to_dm.finish( values={ 'content_type': file.content_type, 'size': file.size, 'uri': new_uri, 'filename': file.filename or 'unknown' })
async def GET_Link(request): """HTTP GET method to return JSON for a link """ log.request(request) app = request.app params = request.rel_url.query group_id = get_obj_id(request) log.info(f"GET link: {group_id}") if not isValidUuid(group_id, obj_class="group"): log.error(f"Unexpected group_id: {group_id}") raise HTTPInternalServerError() link_title = request.match_info.get('title') validateLinkName(link_title) if "bucket" in params: bucket = params["bucket"] else: bucket = None group_json = await get_metadata_obj(app, group_id, bucket=bucket) log.info(f"for id: {group_id} got group json: {group_json}") if "links" not in group_json: log.error(f"unexpected group data for id: {group_id}") raise HTTPInternalServerError() links = group_json["links"] if link_title not in links: log.warn(f"Link name {link_title} not found in group: {group_id}") raise HTTPNotFound() link_json = links[link_title] resp = json_response(link_json) log.response(request, resp=resp) return resp
async def channel_info(self, request): """ /channels/<channel_id> Get information about a specific channel ID """ channel = self.bot.get_channel(int(request.match_info['channel_id'])) if channel is None: raise HTTPNotFound(reason="Unknown channel") try: await self.authenticate_request(request, channel=channel) except HTTPForbidden: return web.json_response({ 'id': channel.id, 'name': channel.name, 'authentication': False, }) ducks_spawned = self.bot.ducks_spawned[channel] ducks = self.bot.enabled_channels[channel] return web.json_response({ 'id': channel.id, 'name': channel.name, 'ducks': [duck.serialize() for duck in ducks_spawned], 'ducks_left_today': ducks.ducks_left, 'ducks_left_day': ducks.day_ducks, 'ducks_left_night': ducks.night_ducks, 'authentication': True, })
async def archivate(request: BaseRequest) -> StreamResponse: archive_hash = request.match_info.get('archive_hash') root_photos_dir = 'test_photos' photos_dir_path = Path(root_photos_dir, archive_hash) if not os.path.exists(photos_dir_path): logging.error(f'Photos archive "{archive_hash}" doesn\'t exist') raise HTTPNotFound( body=f'Архив "{archive_hash}" не существует или был удален', ) response = web.StreamResponse() archive_filename = 'archive.zip' response.headers[ 'Content-Disposition'] = f'attachment; filename="{archive_filename}"' await response.prepare(request) cmd = ('zip', '-r', '-', archive_hash) logging.info(f'Start archiving "{archive_hash}" folder') archiving = await asyncio.create_subprocess_exec( *cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=root_photos_dir, ) chunk_size_in_bytes = 200 * 2024 while not archiving.stdout.at_eof(): logging.info('Sending archive chunk...') await response.write(await archiving.stdout.read(n=chunk_size_in_bytes)) logging.info(f'"{archive_hash}" folder successfully archived and sent') return response
async def document(request): document_id = int(request.match_info['document_id']) pool = request.app['db_pool'] async with pool.acquire() as connection: async with connection.transaction(): sql = """ SELECT s.id, s.text, s.indexed FROM document d JOIN sentence s ON s.document_id = d.id WHERE d.id = $1 ORDER BY ordering ASC """ res = await connection.fetch(sql, document_id) if not res: raise HTTPNotFound(reason="Document not found") return web.json_response({ "success": True, "document_id": document_id, "sentences": [dict(item) for item in res] })
async def parse_content(self): """Download and parse packages' content from the remote repository.""" if self.data.updateinfo_url: updateinfo_downloader = self.remote.get_downloader( url=self.data.updateinfo_url) else: updateinfo_downloader = None # to preserve order, downloaders are created after all repodata urls are identified package_repodata_downloaders = [] for repodata_type in PACKAGE_REPODATA: downloader = self.remote.get_downloader( url=self.data.package_repodata_urls[repodata_type]) package_repodata_downloaders.append(downloader.run()) try: if updateinfo_downloader: await self.parse_advisories(await updateinfo_downloader.run()) await self.parse_packages( await asyncio.gather(*package_repodata_downloaders)) except ClientResponseError as exc: raise HTTPNotFound( reason=_("File not found: {}".format(exc.request_info.url)))
async def prepare(self): # we want have the field name = self.request.matchdict['field_name'] fti = query_utility(IFactory, name=self.context.type_name) schema = fti.schema field = None self.behavior = None if name in schema: field = schema[name] else: # TODO : We need to optimize and move to content.py iterSchema for behavior_schema in fti.behaviors or (): if name in behavior_schema: field = behavior_schema[name] self.behavior = behavior_schema(self.context) break for behavior_name in self.context.__behaviors__ or (): behavior_schema = BEHAVIOR_CACHE[behavior_name] if name in behavior_schema: field = behavior_schema[name] self.behavior = behavior_schema(self.context) break # Check that its a File Field if field is None: raise HTTPNotFound(text='No valid name') if self.behavior is not None: self.field = field.bind(self.behavior) else: self.field = field.bind(self.context) if (self.behavior is not None and IAsyncBehavior.implementedBy(self.behavior.__class__)): # providedBy not working here? await self.behavior.load() return self
async def spa_static_handler(request): """ Handler suitable for use with single page apps. Use with web.get(r'/{path:.*}', spa_static_handler, name='static') modified from aiohttp_web_urldispatcher.StaticResource_handle """ request_path = request.match_info['path'].lstrip('/') directory = request.app['static_dir'] csp_headers = request.app.get('static_headers') or {} if request_path == '': return FileResponse(directory / 'index.html', headers=csp_headers) # probably other paths to return 404 for? if request_path.startswith('.well-known/'): raise HTTPNotFound() try: filename = Path(request_path) if filename.anchor: # pragma: no cover # shouldn't happen on linux, but keep it just in case # request_path is an absolute name like # /static/\\machine_name\c$ or /static/D:\path # where the static dir is totally different raise RuntimeError('request path has anchor') filepath = directory.joinpath(filename).resolve() filepath.relative_to(directory) except Exception: # pragma: no cover logger.warning('error resolving path %r', request_path, exc_info=True) filepath = directory if filepath.is_file(): return FileResponse(filepath, headers=csp_headers) else: return FileResponse(directory / 'index.html', headers=csp_headers)
async def delete_object(self, key, bucket=None): """ Deletes the object at the given key """ if not bucket: log.error("delete_object - bucket not set") raise HTTPInternalServerError() start_time = time.time() log.debug(f"azureBlobClient.delete_object({bucket}/{key} start: {start_time}") try: async with self._client.get_container_client(container=bucket) as container_client: await container_client.delete_blob(blob=key) finish_time = time.time() log.info(f"azureBlobClient.delete_object({key} bucket={bucket}) start={start_time:.4f} finish={finish_time:.4f} elapsed={finish_time-start_time:.4f}") except CancelledError as cle: self._azure_stats_increment("error_count") msg = f"azureBlobClient.CancelledError for delete_object {key}: {cle}" log.error(msg) raise HTTPInternalServerError() except Exception as e: if isinstance(e, AzureError): if e.status_code == 404: msg = f"azureBlobClient.key: {key} not found " log.warn(msg) raise HTTPNotFound() elif e.status_code in (401, 403): msg = f"azureBlobClient.access denied for delete key: {key}" log.info(msg) raise HTTPForbidden() else: self._azure_stats_increment("error_count") log.error(f"azureBlobClient.got unexpected AzureError for delete_object {key}: {e.message}") raise HTTPInternalServerError() else: log.error(f"azureBlobClient.Unexpected exception for put_object {key}: {e}") raise HTTPInternalServerError()
async def parse_content(self): """Download and parse packages' content from the remote repository.""" # to preserve order, downloaders are created after all repodata urls are identified package_repodata_downloaders = [] for repodata_type in PACKAGE_REPODATA: downloader = self.remote.get_downloader( url=self.data.package_repodata_urls[repodata_type]) package_repodata_downloaders.append(downloader.run()) self.data.downloaders.append(package_repodata_downloaders) # asyncio.gather is used to preserve the order of results for package repodata pending = [ asyncio.gather(*downloaders_group) for downloaders_group in self.data.downloaders ] data_type_handlers = defaultdict( lambda: partial(asyncio.sleep, 0), { self.data.package_repodata_urls["primary"]: self.parse_packages, self.data.updateinfo_url: self.parse_advisories, }, ) while pending: done, pending = await asyncio.wait( pending, return_when=asyncio.FIRST_COMPLETED) for downloader in done: try: results = downloader.result() except ClientResponseError as exc: raise HTTPNotFound( reason=_(f"File not found: {exc.request_info.url}")) else: data_url = results[0].url await data_type_handlers[data_url](results)
async def DELETE_Group(request): """HTTP DELETE method for /groups/ """ log.request(request) app = request.app params = request.rel_url.query group_id = get_obj_id(request) if not isValidUuid(group_id, obj_class="group"): log.error(f"Unexpected group_id: {group_id}") raise HTTPInternalServerError() if "bucket" in params: bucket = params["bucket"] else: bucket = None log.info(f"DELETE group: {group_id} bucket: {bucket}") # verify the id exist obj_found = await check_metadata_obj(app, group_id, bucket=bucket) if not obj_found: log.debug(f"delete called on non-exsistet obj: {group_id}") raise HTTPNotFound() log.debug("deleting group: {}".format(group_id)) notify = True if "Notify" in params and not params["Notify"]: notify = False await delete_metadata_obj(app, group_id, bucket=bucket, notify=notify) resp_json = {} resp = json_response(resp_json) log.response(request, resp=resp) return resp
async def delete_object(self, key, bucket=None): """ Deletes the object at the given key """ if not bucket: log.error("delete_object - bucket not set") raise HTTPInternalServerError() start_time = time.time() log.debug(f"s3Client.delete_object({bucket}/{key} start: {start_time}") try: await self._client.delete_object(Bucket=bucket, Key=key) finish_time = time.time() log.info( f"s3Client.delete_object({key} bucket={bucket}) start={start_time:.4f} finish={finish_time:.4f} elapsed={finish_time-start_time:.4f}" ) except ClientError as ce: # key does not exist? key_found = await self.isS3Obj(key) if not key_found: log.warn(f"delete on s3key {key} but not found") raise HTTPNotFound() # else some other error self._s3_stats_increment("error_count") msg = f"Error deleting s3 obj: {ce}" log.error(msg) raise HTTPInternalServerError() except CancelledError as cle: self._s3_stats_increment("error_count") msg = f"CancelledError deleting s3 obj {key}: {cle}" log.error(msg) raise HTTPInternalServerError() except Exception as e: self._s3_stats_increment("error_count") msg = f"Unexpected Exception {type(e)} deleting s3 obj {key}: {e}" log.error(msg) raise HTTPInternalServerError()
async def GET_Attribute(request): """HTTP GET method to return JSON for /(obj)/<id>/attributes/<name> """ log.request(request) app = request.app params = request.rel_url.query obj_id = get_obj_id(request) attr_name = request.match_info.get('name') validateAttributeName(attr_name) if "bucket" in params: bucket = params["bucket"] else: bucket = None obj_json = await get_metadata_obj(app, obj_id, bucket=bucket) log.info( f"GET attribute obj_id: {obj_id} name: {attr_name} bucket: {bucket}") log.debug(f"got obj_json: {obj_json}") if "attributes" not in obj_json: log.error(f"unexpected obj data for id: {obj_id}") raise HTTPInternalServerError() attributes = obj_json["attributes"] if attr_name not in attributes: msg = f"Attribute '{attr_name}' not found for id: {obj_id}" log.warn(msg) raise HTTPNotFound() attr_json = attributes[attr_name] resp = json_response(attr_json) log.response(request, resp=resp) return resp
async def http_get(app, url, params=None, format="json"): log.info("http_get('{}')".format(url)) client = get_http_client(app) data = None status_code = None timeout = config.get("timeout") try: async with client.get(url, params=params, timeout=timeout) as rsp: log.info("http_get status: {}".format(rsp.status)) status_code = rsp.status if rsp.status != 200: log.warn(f"request to {url} failed with code: {status_code}") else: # 200, so read the response if format == "json": data = await rsp.json() else: data = await rsp.read() # read response as bytes except ClientError as ce: log.debug(f"ClientError: {ce}") status_code = 404 except CancelledError as cle: log.error("CancelledError for http_get({}): {}".format(url, str(cle))) raise HTTPInternalServerError() if status_code == 404: log.warn(f"Object: {url} not found") raise HTTPNotFound() elif status_code == 410: log.warn(f"Object: {url} removed") raise HTTPGone() elif status_code != 200: log.error(f"Error for http_get_json({url}): {status_code}") raise HTTPInternalServerError() return data
async def getObjectJson(app, obj_id, refresh=False): """ Return top-level json (i.e. excluding attributes or links) for a given obj_id. If refresh is False, any data present in the meta_cache will be returned. If not the DN will be queries, and any resultant data added to the meta_cache. Note: meta_cache values may be stale, but use of immutable data (e.g. type of a dataset) is always valid """ meta_cache = app['meta_cache'] obj_json = None log.info("getObjectJson {}".format(obj_id)) if obj_id in meta_cache and not refresh: log.debug("found {} in meta_cache".format(obj_id)) obj_json = meta_cache[obj_id] else: req = getDataNodeUrl(app, obj_id) collection = getCollectionForId(obj_id) req += '/' + collection + '/' + obj_id obj_json = await http_get(app, req) # throws 404 if doesn't exist meta_cache[obj_id] = obj_json if obj_json is None: msg = "Object: {} not found".format(obj_id) log.warn(msg) raise HTTPNotFound() return obj_json
async def archivate(request): archive_hash = request.match_info['archive_hash'] dir_path = f"test_photos/{archive_hash}" if not os.path.isdir(dir_path): raise HTTPNotFound(reason='Архив не существует или был удален') os.path.isdir(dir_path) os.chdir(dir_path) response = web.StreamResponse() response.headers[ 'Content-Disposition'] = 'attachment; filename="photos.zip"' await response.prepare(request) args = b'zip -R - * -0' process_coro = await asyncio.create_subprocess_shell( args, stdin=None, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) data_limit = 2**16 # 64 KiB successful = True logging.info('Start sending...') try: while not process_coro.stdout.at_eof(): logging.info('Sending archive chunk...') await response.write(await process_coro.stdout.read(data_limit)) except asyncio.CancelledError: logging.error('Download was interrupted') successful = False process_coro.kill() raise finally: os.chdir('../..') if successful: logging.info('Sending complete') return response
async def get_channel(request: web.Request) -> dict: context: Context = request.app['context'] channel: str = request.match_info['channel'] try: raw_event: Dict[Any, Any] = context.db[ACTUAL_KEY][channel] except KeyError: raise HTTPNotFound(text='Channel is not found.') event = Event(**raw_event) try: period = timedelta(seconds=abs(int(request.query.get('period')))) except (TypeError, ValueError): period = DEFAULT_PERIOD events = await run_in_executor(get_log, context.db, channel, period) chart = None if events: if event.unit.is_float: chart = await run_in_executor(make_float_chart, events) return { 'chart': chart, 'event': event, 'has_events': bool(events), 'raw_event': raw_event, 'request': request, }
async def get_object(self, key, bucket=None, offset=0, length=None): """ Return data for object at given key. If Range is set, return the given byte range. """ range = "" if length: range = f"bytes={offset}-{offset+length-1}" log.info(f"storage range request: {range}") if not bucket: log.error("get_object - bucket not set") raise HTTPInternalServerError() start_time = time.time() log.debug(f"s3CLient.get_object({bucket}/{key} start: {start_time}") try: resp = await self._client.get_object(Bucket=bucket, Key=key, Range=range) data = await resp['Body'].read() finish_time = time.time() log.info( f"s3Client.getS3Bytes({key} bucket={bucket}) start={start_time:.4f} finish={finish_time:.4f} elapsed={finish_time-start_time:.4f} bytes={len(data)}" ) resp['Body'].close() except ClientError as ce: # key does not exist? # check for not found status response_code = ce.response["Error"]["Code"] if response_code in ("NoSuchKey", "404") or response_code == 404: msg = f"s3_key: {key} not found " log.warn(msg) raise HTTPNotFound() elif response_code == "NoSuchBucket": msg = f"s3_bucket: {bucket} not found" log.info(msg) raise HTTPNotFound() elif response_code in ("AccessDenied", "401", "403") or response_code in (401, 403): msg = f"access denied for s3_bucket: {bucket}" log.info(msg) raise HTTPForbidden() else: self._s3_stats_increment("error_count") log.error( f"got unexpected ClientError on s3 get {key}: {response_code}" ) raise HTTPInternalServerError() except CancelledError as cle: self._s3_stats_increment("error_count") msg = f"CancelledError for get s3 obj {key}: {cle}" log.error(msg) raise HTTPInternalServerError() except Exception as e: self._s3_stats_increment("error_count") msg = f"Unexpected Exception {type(e)} get s3 obj {key}: {e}" log.error(msg) raise HTTPInternalServerError() return data