def test_same_base_type(self): self.assertIsInstance(ServiceError(''), HTTPError) self.assertEqual(500, ServiceError('').status_code) self.assertEqual(503, ServiceError('', status_code=503).status_code) self.assertIsInstance(ServiceConfigError(''), ServiceError) self.assertEqual(500, ServiceConfigError('').status_code) self.assertIsInstance(ServiceBadRequestError(''), ServiceError) self.assertEqual(400, ServiceBadRequestError('').status_code) self.assertIsInstance(ServiceResourceNotFoundError(''), ServiceError) self.assertEqual(404, ServiceResourceNotFoundError('').status_code)
def get_query_argument(self, name: str, default: Optional[str] = UNDEFINED) -> Optional[str]: """ Get query argument. :param name: Query argument name :param default: Default value. :return: the value or none :raise: ServiceBadRequestError """ value = self.handler.get_query_argument(name, default=default) if value is UNDEFINED: raise ServiceBadRequestError(f'Missing query parameter "{name}"') return value
def get_time_series_for_geometry_collection( ctx: ServiceContext, ds_name: str, var_name: str, geometry_collection: Dict, start_date: np.datetime64 = None, end_date: np.datetime64 = None) -> Dict: dataset, variable = ctx.get_dataset_and_variable(ds_name, var_name) geometries = GeoJSON.get_geometry_collection_geometries( geometry_collection) if geometries is None: raise ServiceBadRequestError("Invalid GeoJSON geometry collection") shapes = [] for geometry in geometries: try: geometry = shapely.geometry.shape(geometry) except (TypeError, ValueError) as e: raise ServiceBadRequestError( "Invalid GeoJSON geometry collection") from e shapes.append(geometry) return _get_time_series_for_geometries(dataset, variable, shapes, start_date, end_date)
def get_time_series_for_geometry(ctx: ServiceContext, ds_name: str, var_name: str, geometry: Dict, start_date: np.datetime64 = None, end_date: np.datetime64 = None) -> Dict: dataset, variable = ctx.get_dataset_and_variable(ds_name, var_name) if not GeoJSON.is_geometry(geometry): raise ServiceBadRequestError("Invalid GeoJSON geometry") if isinstance(geometry, dict): geometry = shapely.geometry.shape(geometry) return _get_time_series_for_geometry(dataset, variable, geometry, start_date=start_date, end_date=end_date)
def get_body_as_json_object(self, name="JSON object"): """ Get the body argument as JSON object. """ try: return tornado.escape.json_decode(self.request.body) except (JSONDecodeError, TypeError, ValueError) as e: raise ServiceBadRequestError(f"Invalid or missing {name} in request body") from e
def get_query_argument(self, name: str, default: Optional[str] = UNDEFINED) -> Optional[str]: value = self.kvp.get(name, default) if value is UNDEFINED: raise ServiceBadRequestError(f'Missing query parameter "{name}"') return value