14.1.1.2. reschema Module¶
The ServiceDef class represents a single service definition as a collection of resources and types. An instance of this class is normally created by parsing a service definition file.
14.1.1.2.1. Manual ServiceDef creation¶
An instance may be created directly froma file:
>>> bookstore_def = ServiceDef.create_from_file('bookstore.yaml')
This is useful for simple stand alone services. Once created, the services’ resources and types may be browsed the object used for validation:
# Get the 'author' resource
>>> author_res = bookstore_def.resources['author']
# Define an author and validate that the format is correct
>>> author = dict(id=5, name='John Doe')
>>> author_res.validate(author)
# If the format is incorrect, a ValidationError is thrown
>>> author = dict(id='5', name='John Doe')
>>> author_res.validate(author)
ValidationError: ("author.id should be a number, got '<type 'str'>'", <servicedef.Integer 'http://support.riverbed.com/apis/bookstore/1.0#/resources/author/properties/id'>)
14.1.1.2.2. Using Managers¶
For larger projects that span multiple services, a ServiceDefManager can be used to load service defintions on demand. This is particularly important when one service defintion references types or resources from another service definition.
# Create a ServiceDefManager to manage service definitions The
# CustomServiceDefLoader implements the ServiceDefLoadHook andmust
# be defined in order to load service definitions on this system.
>>> svcdef_mgr = ServiceDefManager()
>>> svcdef_mgr.add_load_hook(CustomServiceDefLoader)
# Load a service by looking for it by name/version
>>> bookstore_def = svcdef_mgr.find_by_name('bookstore', '1.0')
14.1.1.2.3. Classes¶
14.1.1.2.3.1. class ServiceDef
¶
-
class
reschema.servicedef.
ServiceDef
(manager=None)¶ Loads and represents the complete service definition
A ServiceDef parses the top-level service definition fields and calls reschema.schema.Schema.parse() on sub-sections that have JSON Schema-based values.
Parameters: manager – The ServiceManager, if any, that is keeping track of this particular service definition. -
__init__
(manager=None)¶
-
check_references
()¶ Iterate through all schemas and check references.
Check all resources and types associated with this service defintion and verify that all references can be properly resolved. This returns an array of jsonschema.Ref instances that cannot be resolved.
-
find
(reference)¶ Resolve a reference using this servicedef as a relative base
Returns a jsonschema.Schema instance
Parameters: reference – string reference to resolve The reference may be one of three supported forms:
- <server><path>#<fragment> - fully qualified reference
- <path>#<fragment> - reference is resolved against the same <server> as servicedef. <path> starts with ‘/’
- #<fragment> - reference is resolved against the same <server> and <path> as servicedef
Raises: InvalidReference – reference does not appear to be to the correct syntax
-
find_resource
(name)¶ Look up and return a resource by name
-
find_type
(name)¶ Look up and return a type by name
-
load
(filename)¶ Loads and parses a JSON or YAML schema.
Support both JSON(.json) and YAML(.yml/.yaml) file formats as detected by filename extensions.
Parameters: filename – The path to the JSON or YAML file. Raises: ValueError – if the file has an unsupported extension.
-
load_from_stream
(f, format='yaml')¶ Loads and parses a JSON or YAML schema.
Support both JSON(.json) and YAML(.yml/.yaml) file formats as detected by filename extensions.
Parameters: f – An open file object. Raises: ValueError – if the file has an unsupported extension.
-
parse
(obj)¶ Parses a Python data object representing a schema.
Parameters: obj (dict) – The Python object containig the schema data.
-
parse_text
(text, format='yaml')¶ Loads and parses a schema from a string.
Parameters: - text – The string containing the schema.
- format – Either ‘json’ (the default), ‘yaml’ or ‘yml’. This much match the format of the data in the string.
-
resource_iter
()¶ Generator for iterating over all resources
-
type_iter
()¶ Generator for iterating over all types
-
14.1.1.2.3.2. class ServiceDefManager
¶
-
class
reschema.servicedef.
ServiceDefManager
¶ Manager for ServiceDef instances.
A ServiceDefManager manages loading and finding ServiceDef instances by id as indicated in the ‘id’ property at the top level of the schema.
-
__init__
()¶
-
add
(servicedef)¶ Add a new ServiceDef instance known at the given id.
-
add_load_hook
(load_hook)¶ Add a callable hook to load a schema by id.
Parameters: hook – an object that implements the ServiceDefLoadHook interface Hooks are processed in order until the first hook returns a ServiceDef instance.
-
clear
()¶ Clear all known schemas.
-
find_by_id
(id_)¶ Resolve an id_ to a servicedef instance.
If a service definition by this id is not yet in the cache, load hooks are invoked in order until one of them returns a instance.
Raises: InvalidServiceId – No schema found for id and could not be loaded
-
find_by_name
(name, version, provider='riverbed')¶ Resolve <provider/name/version> triplet to a servicedef instance.
If a service definition by this full name is not yet in the cache, load hooks are invoked in order until one of them returns a instance.
Parameters: - name – the service name
- version – the service version
- provider – the provider of the service
Returns: a ServiceDef instance
Raises: InvalidServiceId – No schema found for id and could not be loaded
-
14.1.1.2.3.3. class ServiceDefLoadHook
¶
-
class
reschema.servicedef.
ServiceDefLoadHook
¶ Interface for load hooks.
See ServiceDefManager.add_hook()
-
__init__
()¶ x.__init__(...) initializes x; see help(type(x)) for signature
-
find_by_id
(id_)¶ Find a ServiceDef by id.
Parameters: id – the globally unique URI identifying a service definition. Returns: a ServiceDef instance or None
-
find_by_name
(name, version, provider)¶ Find a ServiceDef by <name,version,provider> triplet.
Parameters: - name – the service name
- version – the service version
- provider – the provider of the service
Returns: a ServiceDef instance or None
-