Overview
========

Before we dig in, here is a high-level overview of Cosmic.

REST and RPC
------------

The definition of `REST
<http://en.wikipedia.org/wiki/Representational_state_transfer>`_ is a
controversial subject. For our purposes, these are the key elements:

* Objects are assigned unique URLs.
* These URLs are used to link objects together.
* Objects are manipulated by a set of standard methods (`CRUD
  <http://en.wikipedia.org/wiki/Create,_read,_update_and_delete>`_).

It is fairly straightforward to create a REST API from a database table, but
other kinds of information can be expressed with REST as well. In general it
is a good idea to try, and fall back on `RPC
<http://en.wikipedia.org/wiki/Remote_procedure_call>`_ only if REST is clearly
a bad fit.

RPC stands for "remote procedure call". It is a simple interface where a
client sends a request to call a remote function with certain parameters and
gets the return value of the function in the response. You might find many
discussions online that are framed as "REST vs RPC", but in reality they are
tools that complement each other.

Architecture
------------

Cosmic is a layer that sits between business code and HTTP. This is true for
both the client and server components. In fact, if you were to draw the client
and the server component, you'll see that they are perfectly symmetrical.

Because of the above, and because API clients are to be built from a JSON spec
generated by the server, we decided to implement the API class as a
serializable type. When you serialize a server-side Cosmic API, you get the
JSON spec, when you deserialize it on a different machine, you get an API
client. The serialization is done by `Teleport
<http://www.cosmic-api.com/docs/teleport/python/latest/>`_.

Cosmic abstracts HTTP into endpoints. An endpoint is a rule for serving a
function via HTTP. It contains both the server and the client component. For
the server, it defines :meth:`parse_request` and :meth:`build_response`
methods. For the client, it defined :meth:`build_request` and
:meth:`parse_response`. The reason the server and the client components are
grouped in one object is that the client's :meth:`build_request` method is
intimately related to the server's :meth:`parse_request` method. Similarly,
:meth:`build_response` and :meth:`parse_response` are perfectly symmetrical.

Cosmic treats HTTP as an elaborate serialization scheme and takes full
responsibility for it, leaving you with natively-behaving functions.

..  TODO [endpoint diagram]

Built on Teleport
-----------------

.. seealso::

    The `Teleport documentation </docs/teleport/python/latest/>`_ is worth a
    look if you are getting started with Cosmic.

Teleport is our very own library that is used for JSON serialization,
validation, and generating documentation. At first this might seem like an odd
set of features for a library, but they come quite naturally from the fact
that Teleport is essentially a very simple static type system. All information
that gets carried between Cosmic clients and servers is statically typed with
the help of Teleport.

Teleport is implemented as a collection of composable type objects. The
composition of these objects mirrors the data it is meant to serialize and
validate. One important feature of Teleport is that this composition, the
schema, is also serializable. This makes it possible to use Teleport to
serialize model properties and function definition, which are necessary to
serialize the API.

Teleport makes it easy to define custom types, a feature used by Cosmic.

The Teleport docs will teach you to import from the :mod:`teleport` module:

.. code:: python

    from teleport import *

In Cosmic, you should import from :mod:`cosmic.types`:

.. code:: python

    from cosmic.types import *

.. _hal:

Hypermedia with JSON HAL
------------------------

`JSON HAL <http://stateless.co/hal_specification.html>`_ is a compact
specification for linking REST-ful resources as well as returning multiple
embedded resources in one call (this is used by the :ref:`get_list` endpoint).
Note that HAL recommends ``application/hal+json`` for the *Content-Type*
header, but currently Cosmic responds only to ``application/json``.
