class InitiateAuthRequest(typesystem.Schema): AuthFlow = typesystem.Choice(choices=list(AuthFlowType)) AuthParameters = typesystem.Object() ClientMetadata = typesystem.Object(allow_null=True) ClientId = typesystem.String() AnalyticsMetadata = typesystem.Reference(AnalyticsMetadataType, allow_null=True) UserContextData = typesystem.Reference(UserContextDataType, allow_null=True)
async def update(self, **kwargs): # Validate the keyword arguments. fields = { key: field for key, field in self.fields.items() if key in kwargs } validator = typesystem.Object(properties=fields) kwargs = validator.validate(kwargs) # fields that know how to auto update auto_update_fields = { key: field.onupdate() for key, field in self.fields.items() if field.has_onupdate() } kwargs.update(auto_update_fields) # Build the update expression. pk_column = getattr(self.__table__.c, self.__pkname__) expr = self.__table__.update() expr = expr.values(**kwargs).where(pk_column == self.pk) # Perform the update. await self.__database__.execute(expr) # Update the model instance. for key, value in kwargs.items(): setattr(self, key, value)
async def create(self, **kwargs): # Validate the keyword arguments. fields = self.model_cls.fields required = [ key for key, value in fields.items() if not value.has_default() ] validator = typesystem.Object(properties=fields, required=required, additional_properties=False) kwargs = validator.validate(kwargs) # Remove primary key when None to prevent not null constraint in postgresql. pkname = self.model_cls.__pkname__ pk = self.model_cls.fields[pkname] if kwargs[pkname] is None and pk.allow_null: del kwargs[pkname] # Build the insert expression. expr = self.table.insert() expr = expr.values(**kwargs) # Execute the insert, and return a new model instance. instance = self.model_cls(kwargs) instance.pk = await self.database.execute(expr) return instance
class InitiateAuthResponse(typesystem.Schema): ChallengeName = typesystem.Choice(choices=list(ChallengeNameType), allow_null=True) Session = typesystem.String(max_length=2048, allow_null=True) ChallengeParameters = typesystem.Object(allow_null=True) AuthenticationResult = typesystem.Reference(AuthenticationResultType, allow_null=True)
class JSONRPCResponse(typesystem.Schema): jsonrpc = typesystem.String(pattern="2.0", trim_whitespace=False) id = typesystem.Union(any_of=[ typesystem.String(allow_null=True, min_length=1, trim_whitespace=False), typesystem.Integer(allow_null=True), ]) result = typesystem.Object()
class GetIdRequest(typesystem.Schema): AccountId = typesystem.String(min_length=1, max_length=15, pattern=r"^\d+$", allow_null=True) IdentityPoolId = typesystem.String(min_length=1, max_length=55, pattern=r"^[\w-]+:[0-9a-f-]+$") Logins = typesystem.Object(allow_null=False)
def validate_model(self, model): fields = self.model_cls.fields required = [ key for key, value in fields.items() if not value.has_default() ] validator = typesystem.Object(properties=fields, required=required, additional_properties=False) kwargs = validator.validate(model) if not kwargs.get("id"): kwargs.pop("id", None) return kwargs
class JSONRPCRequest(typesystem.Schema): jsonrpc = typesystem.String(pattern="2.0", trim_whitespace=False) id = typesystem.Union( any_of=[ typesystem.String(allow_null=True, min_length=1, trim_whitespace=False), typesystem.Integer(allow_null=True), ] ) params = typesystem.Union( any_of=[typesystem.Object(additional_properties=True), typesystem.Array()] ) method = typesystem.String()
class Settings(BaseSchema): enforce_https = typesystem.Boolean(default=False) mode = typesystem.Choice( choices=( ("proxy", "Proxy"), ("redirect", "Redirect"), ), default="proxy", ) headers = typesystem.Object(default={}, properties=typesystem.String(max_length=100)) ttl = typesystem.Integer(allow_null=True) certificate_path = typesystem.String(allow_null=True) key_path = typesystem.String(allow_null=True)
async def create(self, **kwargs): # Validate the keyword arguments. fields = self.model_cls.fields required = [key for key, value in fields.items() if not value.has_default()] validator = typesystem.Object( properties=fields, required=required, additional_properties=False ) kwargs = validator.validate(kwargs) # Build the insert expression. expr = self.table.insert() expr = expr.values(**kwargs) # Execute the insert, and return a new model instance. instance = self.model_cls(kwargs) instance.pk = await self.database.execute(expr) return instance
def request(self, operation_id: str, **params): link = self.lookup_operation(operation_id) validator = typesystem.Object( properties={field.name: typesystem.Any() for field in link.fields}, required=[field.name for field in link.fields if field.required], additional_properties=False, ) try: validator.validate(params) except typesystem.ValidationError as exc: raise exceptions.ClientError(messages=exc.messages()) from None method = link.method url = self.get_url(link, params) query_params = self.get_query_params(link, params) (content, encoding) = self.get_content_and_encoding(link, params) return self.transport.send(method, url, query_params=query_params, content=content, encoding=encoding)
class ExampleG(typesystem.Schema, definitions=definitions): field_on_g = typesystem.Integer() example_h = typesystem.Object( properties={"h": typesystem.Reference("ExampleH")} )
class ErrorSchema(typesystem.Schema): code = typesystem.Integer() message = typesystem.String() data = typesystem.Object(additional_properties=True)
import typesystem APISTAR_CONFIG = typesystem.Object( properties={ "schema": typesystem.Object( properties={ "path": typesystem.String(), "format": typesystem.Choice(choices=["openapi", "swagger"]), "base_format": typesystem.Choice(choices=["json", "yaml"]), }, additional_properties=False, required=["path", "format"], ), "docs": typesystem.Object( properties={ "output_dir": typesystem.String(), "theme": typesystem.Choice(choices=["apistar", "redoc", "swaggerui"]), }, additional_properties=False, ), }, additional_properties=False, required=["schema"], )
import re from urllib.parse import urljoin import typesystem from apistar.document import Document, Field, Link, Section from apistar.schemas.jsonschema import JSON_SCHEMA SCHEMA_REF = typesystem.Object( properties={"$ref": typesystem.String(pattern="^#/components/schemas/")} ) REQUESTBODY_REF = typesystem.Object( properties={"$ref": typesystem.String(pattern="^#/components/requestBodies/")} ) RESPONSE_REF = typesystem.Object( properties={"$ref": typesystem.String(pattern="^#/components/responses/")} ) definitions = typesystem.SchemaDefinitions() OPEN_API = typesystem.Object( title="OpenAPI", properties={ "openapi": typesystem.String(), "info": typesystem.Reference("Info", definitions=definitions), "servers": typesystem.Array( items=typesystem.Reference("Server", definitions=definitions) ), "paths": typesystem.Reference("Paths", definitions=definitions), "components": typesystem.Reference("Components", definitions=definitions), "security": typesystem.Array( items=typesystem.Reference("SecurityRequirement", definitions=definitions)
typesystem.Object( properties={ "$ref": typesystem.String(), "type": typesystem.String() | typesystem.Array(items=typesystem.String()), "enum": typesystem.Array(unique_items=True, min_items=1), "definitions": typesystem.Object(additional_properties=typesystem.Reference( "JSONSchema", definitions=definitions)), # String "minLength": typesystem.Integer(minimum=0), "maxLength": typesystem.Integer(minimum=0), "pattern": typesystem.String(format="regex"), "format": typesystem.String(), # Numeric "minimum": typesystem.Number(), "maximum": typesystem.Number(), "exclusiveMinimum": typesystem.Number(), "exclusiveMaximum": typesystem.Number(), "multipleOf": typesystem.Number(exclusive_minimum=0), # Object "properties": typesystem.Object(additional_properties=typesystem.Reference( "JSONSchema", definitions=definitions)), "minProperties": typesystem.Integer(minimum=0), "maxProperties": typesystem.Integer(minimum=0), "patternProperties": typesystem.Object(additional_properties=typesystem.Reference( "JSONSchema", definitions=definitions)), "additionalProperties": ( typesystem.Reference("JSONSchema", definitions=definitions) | typesystem.Boolean()), "required": typesystem.Array(items=typesystem.String(), unique_items=True), # Array "items": ( typesystem.Reference("JSONSchema", definitions=definitions) | typesystem.Array( items=typesystem.Reference("JSONSchema", definitions=definitions), min_items=1, )), "additionalItems": ( typesystem.Reference("JSONSchema", definitions=definitions) | typesystem.Boolean()), "minItems": typesystem.Integer(minimum=0), "maxItems": typesystem.Integer(minimum=0), "uniqueItems": typesystem.Boolean(), })
import re from urllib.parse import urljoin import typesystem from apistar.document import Document, Field, Link, Section from apistar.schemas.jsonschema import JSON_SCHEMA SCHEMA_REF = typesystem.Object( properties={"$ref": typesystem.String(pattern="^#/definitiions/")}) RESPONSE_REF = typesystem.Object( properties={"$ref": typesystem.String(pattern="^#/responses/")}) definitions = typesystem.SchemaDefinitions() SWAGGER = typesystem.Object( title="Swagger", properties={ "swagger": typesystem.String(), "info": typesystem.Reference("Info", definitions=definitions), "paths": typesystem.Reference("Paths", definitions=definitions), "host": typesystem.String(), "basePath": typesystem.String(pattern="^/"), "schemes": typesystem.Array(items=typesystem.Choice( choices=["http", "https", "ws", "wss"])), "consumes":
def get_link(self, base_url, path, path_info, operation, operation_info, schema_definitions): """ Return a single link in the document. """ name = operation_info.get("operationId") title = operation_info.get("summary") description = operation_info.get("description") if name is None: name = _simple_slugify(title) if not name: return None # Parameters are taken both from the path info, and from the operation. parameters = path_info.get("parameters", []) parameters += operation_info.get("parameters", []) fields = [ self.get_field(parameter, schema_definitions) for parameter in parameters ] default_encoding = None if any([field.location == "body" for field in fields]): default_encoding = "application/json" elif any([field.location == "formData" for field in fields]): default_encoding = "application/x-www-form-urlencoded" form_fields = [ field for field in fields if field.location == "formData" ] body_field = Field( name="body", location="body", schema=typesystem.Object( properties={ field.name: typesystem.Any() if field.schema is None else field.schema for field in form_fields }, required=[ field.name for field in form_fields if field.required ], ), ) fields = [ field for field in fields if field.location != "formData" ] fields.append(body_field) encoding = lookup(operation_info, ["consumes", 0], default_encoding) return Link( name=name, url=urljoin(base_url, path), method=operation, title=title, description=description, fields=fields, encoding=encoding, )