https://github.com/dropbox/babelapi
Define an API once in Babel. Implement or use an existing code generator to map the API definition into usable objects and functions in any programming language.
https://github.com/dropbox/babelapi
Last synced: 12 months ago
JSON representation
Define an API once in Babel. Implement or use an existing code generator to map the API definition into usable objects and functions in any programming language.
- Host: GitHub
- URL: https://github.com/dropbox/babelapi
- Owner: dropbox
- Created: 2014-07-22T00:55:00.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2016-11-05T18:04:55.000Z (over 9 years ago)
- Last Synced: 2025-06-10T03:11:21.629Z (about 1 year ago)
- Homepage:
- Size: 1010 KB
- Stars: 4
- Watchers: 27
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
Awesome Lists containing this project
README
*****
Babel
*****
**This repository is a stub. No code is available yet. Check back soon.**
Define an API once in Babel. Use code generators to translate your
specification into objects and functions in the programming languages of your
choice.
Babel is made up of several components:
1. An interface-description language (IDL) for specifying APIs.
2. A command-line tool (``babelapi``) that takes an API specification and
generator module, and generates output.
3. A Python-interface for defining new generators.
4. A JSON-compatible serialization format.
Babel is in active use for the `Dropbox v2 API
`_. Right now, the only available generator
is for Python, but we're working on releasing the other ones we have
internally: Swift, C#, Java, Go, JavaScript, and HTML documentation.
* Introduction
* Motivation_
* Installation_
* `Taste of Babel <#a-taste-of-babel>`_
* `Language Reference (.babel) `_
* `Choosing a Filename `_
* `Comments `_
* `Namespace `_
* `Primitive Types `_
* `Alias `_
* `Struct `_
* `Union `_
* `Nullable Type `_
* `Route `_
* `Include `_
* `Documentation `_
* `Formal Grammar `_
* `Using Generated Code `_
* `Compile with the CLI `_
* `Python Guide `_
* `Managing Specs `_
* `Using Namespaces `_
* `Splitting a Namespace Across Files `_
* `Using Header Files `_
* `Separating Public and Private Routes `_
* `Evolving a Spec `_
* `Background `_
* `Sender-Recipient `_
* `Backwards Incompatible Changes `_
* `Backwards Compatible Changes `_
* `Planning for Backwards Compatibility `_
* `Leader-Clients `_
* `Route Versioning `_
* `Writing a Generator (.babelg.py) `_
* `Using the API Object `_
* `Creating an Output File `_
* `Emit Methods `_
* `Indentation `_
* `Examples `_
* `JSON Serializer `_
* `Network Protocol `_
.. _motivation:
Motivation
==========
Babel was birthed at Dropbox at a time when it was becoming clear that API
development needed to be scaled beyond a single team. The company was
undergoing a large expansion in the number of product groups, and it wasn't
scalable for the API team, which traditionally dealt with core file operations,
to learn the intricacies of each product and build their APIs.
Babel's chief goal is to decentralize API development and ownership at Dropbox.
To be successful, it needed to do several things:
**Decouple APIs from SDKS**: Dropbox has first-party clients for our mobile
apps, desktop client, and website. Each of these is implemented in a different
language. And we want to make onboarding easier for third-parties by providing
them with SDKs. It's untenable to ask product groups that build APIs to also
implement these endpoints in a half-dozen different language-specific SDKs.
Without decoupling, as was the case in our v1 API, the SDKs will inevitably
fall behind. Our solution is to have our SDKs automatically generated.
**Improve Visibility into our APIs**: These days, APIs aren't just in the
domain of engineering. Product managers, product specialists, partnerships,
sales, and services groups all need to have clear and accurate specifications
of our APIs. After all, APIs define Dropbox's data models and functionlaity.
Before Babel, API design documents obseleted by changes during implementation
were the source of truth.
**Consistency and Predictability**: Consistency ranging from documentation
tense to API patterns are important for making an API predictable and therefore
easier to use. We needed an easy way to make and enforce patterns.
**JSON**: To make consumption easier for third parties, we wanted our data
types to map to JSON. For cases where serialization efficiency
(space and time) are important, you can try using msgpack (alpha support
available in the Python generator). It's possible also to define your own
serialization scheme, but at that point, you may consider using something like
`Protobuf `_.
Assumptions
-----------
Babel makes no assumptions about the transport layer being used to make API
requests and return responses; its first use case is the Dropbox v2 API which
operates over HTTP. Babel does not come with nor enforce any particular RPC
framework.
Babel makes some assumptions about the data types supported in target
programming languages. It's assumed that there is a capacity for representing
dictionaries (unordered string keys -> value), lists, numeric types, and
strings.
Babel assumes that a route (or API endpoint) can have its argument and
result types defined without relation to each other. In other words, the
type of response does not change based on the input to the endpoint. An
exception to this rule is afforded for error responses.
.. _installation:
Installation
============
Download or clone BabelAPI, and run the following in its root directory::
$ sudo python setup.py install
This will install a script ``babelapi`` to your PATH that can be run from the
command line::
$ babelapi -h
Alternative
-----------
If you choose not to install ``babelapi`` using the method above, you will need
to ensure that you have the Python packages ``ply`` and ``six``, which can be
installed through ``pip``::
$ pip install ply>=3.4 six>=1.3.0
If the ``babelapi`` package is in your PYTHONPATH, you can replace ``babelapi``
with ``python -m babelapi.cli`` as follows::
$ python -m babelapi.cli -h
If you have the ``babelapi`` package on your machine, but did not install it or
add its location to your PYTHONPATH, you can use the following::
$ PYTOHNPATH=path/to/babelapi python -m babelapi.cli -h
.. taste-of-babel:
A Taste of Babel
================
Here we define a hypothetical route that shows up in some form or another in
APIs for web services: querying the account information for a user of a
service. Our hypothetical spec lives in a file called ``users.babel``::
# We put this in the "users" namespace in anticipation that
# there would be many user-account-related routes.
namespace users
route get_account (GetAccountReq, Account, GetAccountErr)
"Get information about a specified user's account."
# This struct represents the input data to the route.
struct GetAccountReq
account_id AccountId
# We define an AccountId as being a 10-character string
# once here to avoid declaring it each time.
alias AccountId = String(min_length=10, max_length=10)
struct Account
"Information about a user's account."
account_id AccountId
"A unique identifier for the user's account."
email String(pattern="^[^@]+@[^@]+\.[^@]+$")
"The e-mail address of the user."
name String(min_length=1)?
"The user's full name. :val:`null` if no name was provided."
status Status
"The status of the account."
example default "A regular user"
account_id="id-48sa2f0"
email="alex@example.org"
name="Alexander the Great"
status=active
union Status
active
"The account is active."
inactive Timestamp("%a, %d %b %Y %H:%M:%S")
"The account is inactive. The value is when the account was
deactivated."
# This union represents the possible errors that might be returned.
union GetAccountErr
no_account
"No account with the requested id could be found."
perm_denied
"Insufficient privileges to query account information."
unknown*
Using the Python generator, we can generate a Python module that mirrors this
specification using the command-line interface. From the top-level of the
``babelapi`` folder, try::
$ babelapi generator/python/python.babelg.py . users.babel
Now we can interact with the specification in Python::
$ python -i users.py
>>> a = Account()
>>> a.account_id = 1234 # fails type validation
Traceback (most recent call last):
...
babel_data_types.ValidationError: '1234' expected to be a string, got integer
>>> a.account_id = '1234' # fails length validation
Traceback (most recent call last):
...
babel_data_types.ValidationError: '1234' must be at least 10 characters, got 4
>>> a.account_id = 'id-48sa2f0' # passes validation
>>> # Now we use the included JSON serializer
>>> from babel_serializers import json_encode
>>> a2 = Account(account_id='id-48sa2f0', name='Alexander the Great',
... email='alex@example.org', status=Status.active)
>>> json_encode(GetAccountRoute.result_data_type, a2)
'{"status": "active", "account_id": "id-48sa2f0", "name": "Alexander the Great", "email": "alex@example.org"}'