def _use_environment_variable_for_variable( config: Union[Config, dict], keys: List[str], model: BaseModel, content_env_var: Union[str, int, float, None], ) -> Union[BaseModel, dict]: """Overwrite config with value of environment-variable. To be able to overwrite the config it must be converted to a dict (if it is not already). After updating the value it has to be converted back to an instance of the passed ``model``. Parameters: config: the config to update. keys: sublevels for the config to set the value. model: the model to convert back the config after update. content_env_var: the content of the environment-variable. env_var_name: the name of the variable. Returns: the updated config. """ if isinstance(config, dict): temp_config = copy.deepcopy(config) else: temp_config = copy.deepcopy(config.dict()) temp_config = _update_value_in_nested_dict_by_keylist( dictionary=temp_config, key_list=keys, new_value=content_env_var) if model: return model.parse_obj(temp_config) return temp_config
async def _convert_response_to_model(model: BaseModel, response, info_msg: str) -> BaseModel: """Extract request-result and convert it into an instance of ``model``. Parameters: model: the model to convert the service-result into. response: the result of the made service-call. info_msg: the message to return if something goes wrong during conversion to the model. Raises: if something goes wrong during conversion to the model a :class:`HTTPException` is raised. Returns: the service-call response converted to an instance of model. """ try: result = response.json() return model.parse_obj(result) except ValidationError as error: raise HTTPException( status_code=500, detail=( f'{info_msg} => Invalid result: {response}. Error was {error}.' ))
def pydantic_validate(data: Any, model: BaseModel) -> BaseModel: """Attempt to validate incoming data using Pydantic model class. Args: data (Any): Incoming data model (BaseModel): Pydantic model Raises: ValidationError: A django validation error parsing fails Returns: BaseModel: Instance of class, if successful. """ try: return model.parse_obj(data) except PydanticValidationError as err: raise ValidationError(err.json())
def create_list_endpoint(entity: str, crud: CRUDBase, request_schema: BaseModel, response_schema: BaseModel, is_headers: bool): def list_endpoint(request: Request): result = {} if is_headers: headers = [] json_schema = response_schema.schema() for field_name, values in json_schema["properties"].items(): if field_name == entity and values['type'] == 'array': if ref := values['items']['$ref'].split('#/definitions/')[1]: schema_fields = json_schema['definitions'][ref]['properties'] for name, prop in schema_fields.items(): headers.append({"value": name, 'text': prop["title"], 'type': prop['type']}) result.update({"headers": headers}) items = response_schema.parse_obj({entity: crud.get_all()}) result.update(items) return result
def model_templating(self, start_period: dt.datetime, end_period: dt.datetime, model: BaseModel): new_model_dict = {} for key, value in model.dict(exclude_unset=True).items(): new_model_dict[key] = value if isinstance(value, str): new_model_dict[key] = jinja2.Template(value).render( **{ "start_period": start_period, "end_period": end_period, "datetime": dt.datetime.now(), "name": self.config.name, "provider": self.config.provider, "storage": self.config.storage, }) self.validate_params(**new_model_dict) return model.parse_obj(new_model_dict)
def csv_to_pydantic(csv_reader: csv.DictReader, pydantic_schema: BaseModel, transformer: dict = None) -> CSVToPydanticResult: """ Converts the lines of a csv_reader to objects of a pydantic schema. If you need to rename columns, supply a `transformer` dictionary with the original column names as keys and their new name as values. Returns a tuple containing two lists - """ result = [] errors = [] def rename_dict(dict_, transformer): dict_ = dict(dict_) new_dict = {} for k, v in dict_.items(): if k in transformer: new_dict[transformer[k]] = dict_[k] else: new_dict[k] = v return new_dict for idx, record in enumerate(csv_reader): if transformer: record = rename_dict(record, transformer) record = {k: v for k, v in record.items() if v not in ("", None)} try: object_ = pydantic_schema.parse_obj(record) except pydantic.ValidationError as e: error_message = f"Erro de conversão do resultado #{idx}!" log(f"{error_message}\n{e}", level=logging.WARNING) errors.append( CSVToPydanticError(csv_record=record, validation_error=e.json())) else: result.append(object_) return CSVToPydanticResult(result, errors)
def model_templating(self, start_period: dt.datetime, end_period: dt.datetime, model: BaseModel): # TODO: replace method with function new_model_dict = {} for key, value in model.dict(exclude_unset=True).items(): new_model_dict[key] = value if isinstance(value, str): new_model_dict[key] = jinja2.Template(value).render( **{ "start_period": start_period, "end_period": end_period, "datetime": pendulum.now("UTC"), "name": self.notebook.name, "provider": self.notebook.provider, "storage": self.notebook.storage, }) self.validate_params(**new_model_dict) return model.parse_obj(new_model_dict)