Given a spec URL, loads the JSON form of an API and deserializes it, returning the API object.
Return a flask.blueprints.Blueprint instance containing everything necessary to run your API. You may use this to augment an existing Flask website with an API:
from flask import Flask
from cosmic import API
hackernews = Flask(__name__)
hnapi = API("hackernews")
hackernews.register_blueprint(
hnapi.get_blueprint(),
url_prefix="/api")
The debug parameter will determine whether Cosmic will propagate exceptions, letting them reach the debugger or swallow them up, returning proper HTTP error responses.
Returns a Flask application with nothing but the API blueprint registered.
Runs the API as a Flask app. All keyword arguments except url_prefix channelled into Flask.run().
A decorator for creating actions out of functions and registering them with the API.
The accepts parameter is a schema that will deserialize the input of the action, returns is a schema that will serialize the output of the action. The name of the function becomes the name of the action. Internally from_func() is used.
from teleport import Integer
random = API("random")
@random.action(returns=Integer())
def generate():
return 9
A decorator for registering a model with an API. The name of the model class is used as the name of the resulting model.
from teleport import String
dictionary = API("dictionary")
@dictionary.model
class Word(object):
schema = String()
A data type definition attached to an API.
Returns the Teleport serializer that describes the structure of the data for this model. By default this method will return the schema attribute of the Model subclass. This is the attribute you should override to set the schema.
Returns the JSON form of the model
Given the JSON form of the model, deserializes it, validates it and instantiates the model.
Given the native data as deserialized by schema, validate it, raising a teleport.exceptions.ValidationError if the data is invalid.
Create an action from a function func that expects data as defined by the accepts schema and returns data that will be serialized by the returns schema. Both accepts and returns must be Teleport serializers.
If action was generated from a function, calls it with the passed in data, otherwise serializes the data, makes an HTTP request to the API and returns its normalized response.
Uses pack_action_arguments().
Essentially a sorted dictionary. Allows to reference actions or models as attributes and implements __all__ so that a Namespace instance can be treated as a module.
Calculate JSON schema spec for action. If function has no arguments, returns None.
Applies a piece of normalized data to the user-defined action function based on its argument spec. The data is assumed to be normalized by a schema compatible with func. (see schema_is_compatible()). Thus, no validation is performed.
If func takes a single argument, data is passed in as is. If it takes multiple arguments, data is assumed to be a dict and is unpacked into the function arguments.
If object is None, func is called with no arguments.
Takes arbitrary args and kwargs and packs them into a dict if there are more than one. Returns None if there are no arguments. Must be called with either a single argument or multiple keyword arguments.
Given two Teleport serializers, checks if the detailed one is compatible with the general one. The general schema is a subset as returned by tools.get_arg_spec().