def wrapper(*args, **kwargs): validation_schema = { 'dataset_names': { 'type': 'string', 'required': True, 'default': None }, 'geojson': { 'type': 'string', 'excludes': 'geostore', 'required': False } } try: logging.debug(f"[VALIDATOR - prediction params]: {kwargs}") validator = Validator(validation_schema, allow_unknown=True, purge_unknown=True) logging.info( f"[VALIDATOR - prediction params]: {validator.validate(kwargs['params'])}" ) if not validator.validate(kwargs['params']): return error(status=400, detail=validator.errors) kwargs['sanitized_params'] = validator.normalized(kwargs['params']) return func(*args, **kwargs) except Exception as err: return error(status=502, detail=f'{err}')
def wrapper(*args, **kwargs): try: if 'geostore' in kwargs["sanitized_params"].keys(): geostore = kwargs["sanitized_params"]["geostore"] logging.info(f'[middleware]: {geostore}') geojson = GeostoreService.get(geostore) kwargs["sanitized_params"]["geojson"] = geojson except GeostoreNotFound: return error(status=404, detail='Geostore not found') except Exception as err: return error(status=502, detail=f'{err}') return func(*args, **kwargs)
def get_normalized_bands(**kwargs): try: pp = Preprocessing() result = pp.normalize_images(**kwargs) return jsonify({'data': result}), 200 except Exception as err: return error(status=502, detail=f'{err}')
def get_composites(**kwargs): try: pp = Preprocessing() result = pp.composite(**kwargs) return jsonify({'data': result}), 200 except Exception as err: return error(status=502, detail=f'{err}')
def remove_keys(keys, dictionary): for key in keys: try: del dictionary[key] except KeyError: pass except Exception as err: return error(status=502, detail=f'{err}') return dictionary
def execute(config): try: response = request_to_microservice(config) if not response or response.get('errors'): raise GeostoreNotFound geostore = response.get('data', None).get('attributes', None) geojson = geostore.get('geojson', None).get('features', None)[0] except Exception as err: return error(status=502, detail=f'{err}') return geojson
def wrapper(*args, **kwargs): try: logging.debug(f'[middleware] [sanitizer] args: {args}') logging.debug(f'[middleware] [sanitizer] kargs: {kwargs}') myargs = { **kwargs, **request.args, **request.args.get('payload', {}) } logging.debug(f'[middleware] [sanitizer] User_args: {myargs}') # Exclude params like loggedUser here sanitized_args = remove_keys(['loggedUser'], myargs) kwargs['params'] = sanitized_args except GeostoreNotFound: return error(status=404, detail='body params not found') except Exception as err: return error(status=502, detail=f'{err}') return func(*args, **kwargs)
def setup_ee(): """Sets up Earth Engine authentication.""" try: private_key = os.getenv('EE_PRIVATE_KEY') service_email = os.getenv('GEO_PREDICTOR_SERVICE_EMAIL') credentials = ee.ServiceAccountCredentials(email=service_email, key_data=private_key) ee.Initialize(credentials=credentials, use_cloud_api=False) ee.data.setDeadline(60000) app.logger.info("EE Initialized") except Exception as err: return error(status=502, detail=f'{err}')
def get_datasets(): # Receive a payload and post it to DB to get all models. No pagination or filtering capabilities applied yet try: db = Database() query = """ SELECT dataset.slug, dataset.name, dataset.bands, dataset.rgb_bands, dataset.provider FROM dataset """ result = db.Query(query) app.logger.debug(result) # Add temporal range, bbox, and bounds result = add_range_bbox(result) # function to post schema return jsonify({'data': result}), 200 except Exception as err: return error(status=502, detail=f'{err}')
def create_jobs(**kwargs): try: db = Database() params = { 'dataset_names': list( map(lambda x: x.strip(), kwargs['sanitized_params']['dataset_names'].split(','))), 'init_date': kwargs['sanitized_params']['init_date'], 'end_date': kwargs['sanitized_params']['end_date'], 'geostore': kwargs['sanitized_params']['geojson'], 'norm_type': kwargs['sanitized_params']['norm_type'], 'input_bands': list( map(lambda x: x.strip(), kwargs['sanitized_params']['input_bands'].split(','))), 'output_bands': list( map(lambda x: x.strip(), kwargs['sanitized_params']['output_bands'].split(','))), 'input_type': kwargs['sanitized_params']['input_type'], 'model_type': kwargs['sanitized_params']['model_type'], 'model_output': kwargs['sanitized_params']['model_output'], 'batch_size': kwargs['sanitized_params']['batch_size'], 'epochs': kwargs['sanitized_params']['epochs'] } result = db.insert('jobs', [{'status': 'start', 'params': params}]) logging.info(f'[JOB creation]{result}') return jsonify({'data': params}), 200 except Exception as err: return error(status=502, detail=f'{err}')
def internal_server_error(e): return error(status=500, detail='Internal Server Error')
def gone(e): return error(status=410, detail='Gone')
def method_not_allowed(e): return error(status=405, detail='Method Not Allowed')
def page_not_found(e): return error(status=404, detail='Not Found')
def forbidden(e): return error(status=403, detail='Forbidden')