def _serialize_rule( folder: watolib.CREFolder, index: int, rule: watolib.Rule, ) -> DomainObject: return constructors.domain_object( domain_type="rule", editable=False, identifier=rule.id, title=rule.description(), extensions={ "ruleset": rule.ruleset.name, "folder": "/" + folder.path(), "folder_index": index, "properties": rule.rule_options.to_config(), "value_raw": rule.value, "conditions": denilled( { "host_name": rule.conditions.host_name, "host_tag": rule.conditions.host_tags, "host_label": rule.conditions.host_labels, "service_description": rule.conditions.service_description, "service_label": rule.conditions.service_labels, } ), }, )
def _translate_to_openapi_keys( name: str = None, location: LocationType = None, description: Optional[str] = None, required: bool = None, example: str = None, allow_emtpy: bool = None, schema_type: OpenAPISchemaType = None, schema_string_pattern: Optional[str] = None, schema_string_format: str = None, schema_num_minimum: Optional[int] = None, schema_num_maximum: Optional[int] = None, ): schema: Dict[str, Any] = {'type': schema_type} if schema_type == 'string': schema.update( format=schema_string_format, pattern=schema_string_pattern, ) if schema_type in ('number', 'integer'): schema.update( minimum=schema_num_minimum, maximum=schema_num_maximum, ) raw_values = { 'name': name, 'in': location, 'required': required, 'description': description, 'allowEmptyValue': allow_emtpy, 'example': example, 'schema': denilled(schema) or None, } return raw_values
def _serialize_rule(rule_entry: RuleEntry) -> DomainObject: rule = rule_entry.rule return constructors.domain_object( domain_type="rule", editable=False, identifier=rule.id, title=rule.description(), extensions={ "ruleset": rule.ruleset.name, "folder": "/" + rule_entry.folder.path(), "folder_index": rule_entry.index_nr, "properties": rule.rule_options.to_config(), "value_raw": rule.value, "conditions": denilled({ "host_name": rule.conditions.host_name, "host_tags": rule.conditions.host_tags, "host_labels": rule.conditions.host_labels, "service_description": rule.conditions.service_description, "service_labels": rule.conditions.service_labels, }), }, )
def __call__( self, name: Optional[str] = None, description: Optional[str] = None, location: Optional[LocationType] = None, required: Optional[bool] = None, allow_empty: Optional[bool] = None, example: Optional[str] = None, schema_type: Optional[OpenAPISchemaType] = None, schema_string_pattern: Optional[str] = None, schema_string_format: Optional[str] = None, schema_num_minimum: Optional[int] = None, schema_num_maximum: Optional[int] = None, ): """ Examples: >>> p = ParamDict.create('foo', 'query', required=False) >>> p = p( ... name='bar', ... allow_empty=True, ... ) >>> expected = { ... 'name': 'bar', ... 'in': 'query', ... 'required': False, ... 'allowEmptyValue': True, ... 'schema': {'type': 'string'}, ... } >>> assert p == expected, p """ # NOTE: The defaults are all None here, so that only the updated keys will overwrite the # previous values. new_dict = self.__class__(**self) raw_values = _translate_to_openapi_keys( name=name, location=location, description=description, example=example, required=required, allow_emtpy=allow_empty, schema_type=schema_type, schema_num_maximum=schema_num_maximum, schema_num_minimum=schema_num_minimum, schema_string_format=schema_string_format, schema_string_pattern=schema_string_pattern, ) new_dict.update(denilled(raw_values)) return new_dict
def create( cls, param_name: str, location: LocationType, description: Optional[str] = None, required: bool = True, allow_emtpy: bool = False, example: Optional[str] = None, schema_enum: Optional[List[str]] = None, schema_type: Optional[OpenAPISchemaType] = 'string', schema_string_pattern: Optional[str] = None, schema_string_format: Optional[str] = None, schema_num_minimum: Optional[int] = None, schema_num_maximum: Optional[int] = None, ) -> 'ParamDict': """Specify an OpenAPI parameter to be used on a particular endpoint. Args: param_name: The name of the parameter. description: Optionally the description of the parameter. Markdown may be used. location: One of 'query', 'path', 'cookie', 'header'. required: If `location` is `path` this needs to be set and True. Otherwise it can even be absent. allow_emtpy: If None as a value is allowed. example: Example value for the documentation. schema_type: May be 'string', 'bool', etc. schema_enum: A list of distinct values that this parameter can hold. These will be rendered in the documentation as well. schema_string_pattern: A regex which is used to filter invalid values. Only valid for `schema_type` being set to 'string'. schema_string_format: The format of the string. schema_num_minimum: Valid for `integer`, `number`. The minimum number. schema_num_maximum: Valid for `integer`, `number`. The maximum number. Examples: >>> p = ParamDict.create('foo', 'query', required=False) >>> expected = { ... 'name': 'foo', ... 'in': 'query', ... 'required': False, ... 'allowEmptyValue': False, ... 'schema': {'type': 'string'}, ... } >>> assert p == expected, p Returns: The parameter dict. """ if location == 'path' and not required: raise ValueError("path parameters' `required` field always needs to be True!") raw_values = _translate_to_openapi_keys( param_name, location, description=description, required=required, allow_emtpy=allow_emtpy, schema_type=schema_type, example=example, schema_enum=schema_enum, schema_num_maximum=schema_num_maximum, schema_num_minimum=schema_num_minimum, schema_string_format=schema_string_format, schema_string_pattern=schema_string_pattern, ) # We throw away None valued keys so they won't show up in the specification. return cls(denilled(raw_values))
def to_openapi( params: Optional[Union[RawParameter, Sequence[RawParameter]]], location: LocationType, ) -> Sequence[OpenAPIParameter]: """Put the 'in' key into a all parameters in a list. Examples: >>> class Params(Schema): ... field1 = fields.String() ... field2 = fields.String() >>> to_openapi([Params], 'query') [{'name': 'field1', 'in': 'query', 'required': True, 'allowEmptyValue': False, \ 'schema': {'type': 'string'}}, \ {'name': 'field2', 'in': 'query', 'required': True, 'allowEmptyValue': False, \ 'schema': {'type': 'string'}}] >>> to_openapi([{'field1': fields.String()}], 'query') [{'name': 'field1', 'in': 'query', 'required': True, 'allowEmptyValue': False, \ 'schema': {'type': 'string'}}] >>> to_openapi([{'field2': fields.String()}], 'query') [{'name': 'field2', 'in': 'query', 'required': True, 'allowEmptyValue': False, \ 'schema': {'type': 'string'}}] >>> to_openapi([{'field1': fields.String(), 'field2': fields.String()}], 'query') [{'name': 'field1', 'in': 'query', 'required': True, 'allowEmptyValue': False, \ 'schema': {'type': 'string'}}, \ {'name': 'field2', 'in': 'query', 'required': True, 'allowEmptyValue': False, 'schema': \ {'type': 'string'}}] >>> to_openapi([Schema], 'query') [] >>> to_openapi([{}], 'query') [] >>> to_openapi(None, 'query') [] Args: params: The list of parameters. location: The location of the parameters. May be either 'query', 'path', 'header' or 'cookie'. Returns: The list in a normalized form and the location added. """ if params is None: return [] if not isinstance(params, list): raise ValueError("Needs to be a sequence of parameters.") def _is_field_param(dict_): return all( [isinstance(value, fields.Field) for value in dict_.values()]) def _is_schema_class(klass): try: return issubclass(klass, Schema) except TypeError: return False result: List[OpenAPIParameter] = [] _fields: ItemsView[str, fields.Field] for raw_param in params: if _is_schema_class(raw_param): raw_param = raw_param() if isinstance(raw_param, Schema): _fields = raw_param.declared_fields.items() elif _is_field_param(raw_param): _fields = raw_param.items() else: raise ValueError( f"Don't recognize parameter of form: {raw_param!r}") for name, field in _fields: metadata = denilled({ 'description': field.metadata.get('description'), 'example': field.metadata.get('example'), 'required': field.metadata.get('required'), 'allow_empty': field.metadata.get('allow_empty'), 'schema_enum': field.metadata.get('enum'), 'schema_string_format': field.metadata.get('format'), 'schema_string_pattern': field.metadata.get('pattern'), 'schema_num_minimum': field.metadata.get('minimum'), 'schema_num_maximum': field.metadata.get('maximum'), }) param = translate_to_openapi_keys( name=name, location=location, **metadata, ) result.append(param) return result