Services¶
Services are the building block of nautilus clouds. Very simply, a nautilus service is a standalone process that responds to actions sent over the global event queue, maintains and mutates some internal state according to the action, and provides a summary of that internal state via a GraphQL API.
Nautilus provides extendible implementations of common services as well as a base Service class which all act as good starting points for your own services:
-
class
nautilus.ModelService(model=None, **kwargs)[source]¶ This service acts as the primary data store in your cloud. It manages the records of a single model by listening for actions that indicate a record mutation as well as emitting actions when the mutations have finished (whether successfully or not). The external API is automatically generated to match the given model.
Parameters: model (nautilus.BaseModel) – The nautilus model to manage. Example
import nautilus import nautilus.models as models class Model(models.BaseModel): name = models.fields.CharField() class ServiceConfig: database_url = 'sqlite:////tmp/models.db' class MyModelService(nautilus.ModelService): model = Model config = ServiceConfig
-
class
nautilus.ConnectionService(**kwargs)[source]¶ This service manages a “one-way” connection between two services. The underlying schema and database are automatically generated to match the primary keys of the linked services.
This service will listen for actions indicating the deletion of a related model and remove any related fields to maintain consistency. And provides a way for the api gateway to deduce the relationship between services when summarizing the cloud.
Parameters: services (list of nautilus.Service) – The list of services to connect. Example
# external imports import nautilus class ServiceConfig: database_url = 'sqlite:////tmp/connections.db' class MyConnection(nautilus.ConnectionService): config = ServiceConfig from_service = ('service_one',) to_service = ('service_one',)
-
class
nautilus.APIGateway(*args, **kwds)[source]¶ This provides a single endpoint that other services and clients can use to query the cloud without worrying about the distributed nature of the system.
Example
# external imports import nautilus # local imports from .schema import schema class MyAPIGateway(nautilus.APIGateway): schema = schema
-
action_handler¶ alias of
APIActionHandler
-
api_request_handler_class¶ alias of
APIQueryHandler
-
auth_criteria¶ This attribute provides the mapping of services to their auth requirement
Returns: the mapping from services to their auth requirements. Return type: (dict)
-
get_models()[source]¶ Returns the models managed by this service.
Returns: the models managed by the service Return type: (list)
-
init_db()[source]¶ This function configures the database used for models to make the configuration parameters.
-
login_user(password, **kwds)[source]¶ This function handles the registration of the given user credentials in the database
-
mutation_resolver(mutation_name, args, fields)[source]¶ the default behavior for mutations is to look up the event, publish the correct event type with the args as the body, and return the fields contained in the result
-
object_resolver(object_name, fields, obey_auth=False, current_user=None, **filters)[source]¶ This function resolves a given object in the remote backend services
-
-
class
nautilus.Service(name=None, schema=None, action_handler=None, config=None, auth=True)[source]¶ This is the base class for all services that are part of a nautilus cloud. This class provides basic functionalities such as registration, responding to actions, and predictable api endpoints.
Parameters: - action_handler (optional, function) – The callback function fired when an action is recieved. If None, the service does not connect to the action queue.
- config (optional, class) – A python class to use for configuring the service.
- name (string) – The name of the service. This will be used to register the service with the registry as act as the designator for a ServiceObjectType.
- schema (optional, graphql.core.type.GraphQLSchema) – The GraphQL schema which acts as a basis for the external API. If None, no endpoints are added to the service.
Example
import nautilus from nautilus.api.util import create_model_schema from nautilus.network import crud_handler import nautilus.models as models class Model(models.BaseModel): name = models.fields.CharField() class MyService(nautilus.Service): name = 'My Awesome Service' schema = create_model_schema(Model) action_handler = crud_handler(Model)
-
add_http_endpoint(url, request_handler)[source]¶ This method provides a programatic way of added invidual routes to the http server.
Parameters: - url (str) – the url to be handled by the request_handler
- request_handler (nautilus.network.RequestHandler) – The request handler
-
cleanup()[source]¶ This function is called when the service has finished running regardless of intentionally or not.
-
classmethod
route(route, config=None)[source]¶ This method provides a decorator for adding endpoints to the http server.
Parameters: Example
import nautilus from nauilus.network.http import RequestHandler class MyService(nautilus.Service): # ... @MyService.route('/') class HelloWorld(RequestHandler): def get(self): return self.finish('hello world')