Skip to content

Commit

Permalink
added the datastore validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohann Gabory committed Oct 14, 2013
1 parent 25d8cd4 commit 9b8c044
Show file tree
Hide file tree
Showing 28 changed files with 991 additions and 96 deletions.
Binary file modified docs/build/.doctrees/datastore.doctree
Binary file not shown.
Binary file modified docs/build/.doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/.doctrees/index.doctree
Binary file not shown.
Binary file modified docs/build/.doctrees/references.doctree
Binary file not shown.
Binary file added docs/build/.doctrees/tutorial.doctree
Binary file not shown.
Binary file added docs/build/.doctrees/using_user_endpoint.doctree
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/build/_modules/rest_api_framework/controllers.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ <h1>Source code for rest_api_framework.controllers</h1><div class="highlight"><p
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">wsgi_app</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">wsgi_app</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<div class="viewcode-block" id="WSGIWrapper.wsgi_app"><a class="viewcode-back" href="../../references.html#rest_api_framework.controllers.WSGIWrapper.wsgi_app">[docs]</a> <span class="k">def</span> <span class="nf">wsgi_app</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">dispatch_request</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>

</div>
</div></div>
<div class="viewcode-block" id="WSGIDispatcher"><a class="viewcode-back" href="../../references.html#rest_api_framework.controllers.WSGIDispatcher">[docs]</a><span class="k">class</span> <span class="nc">WSGIDispatcher</span><span class="p">(</span><span class="n">DispatcherMiddleware</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Embed multiple endpoint in one</span>
Expand Down
1 change: 1 addition & 0 deletions docs/build/_sources/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Contents:
:maxdepth: 2

introduction
tutorial
datastore
controller
pagination
Expand Down
128 changes: 128 additions & 0 deletions docs/build/_sources/tutorial.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
Tutorial building an adressebook API
====================================

First Step Building a user endpoint
-----------------------------------

For this project we need users. Users will be helpfull for our adress
book and for our authentication process.

Users will be define with at least a first name and a last name. We
also need an unique identifier to retreive the user.

Define a model
~~~~~~~~~~~~~~

.. code-block:: python

from rest_api_framework import models

class UserModel(models.Model):

fields = [models.StringField(name="first_name", required=True),
models.StringField(name="last_name", required=True),
models.PkField(name="id", required=True)
]

The use of required_true will ensure that a user without this field
cannot be created

Chose a DataStore
~~~~~~~~~~~~~~~~~

We also need a datastore to get a place where we can save our
users. For instance we will use a sqlite3 database. The
SQLiteDataStore is what we need

.. code-block:: python

from rest_api_framework.datastore import SQLiteDataStore

Chose a view
~~~~~~~~~~~~

We want results to be rendered as Json. We use the JsonResponse view
for that:

.. code-block:: python

from rest_api_framework.views import JsonResponse

Create The user endpoint
~~~~~~~~~~~~~~~~~~~~~~~~

To create an endpoint, we need a controller. This will manage our
endpoint in a RESTFUL fashion.

.. code-block:: python

from rest_api_framework.controllers import Controller

class UserEndPoint(Controller):
ressource = {
"ressource_name": "users",
"ressource": {"name": "adress_book.db", "table": "users"},
"model": UserModel,
"datastore": SQLiteDataStore
}

controller = {
"list_verbs": ["GET", "POST"],
"unique_verbs": ["GET", "PUT", "DElETE"]
}

view = {"response_class": JsonResponse}

then we must run our application:

.. code-block:: python

if __name__ == '__main__':
from werkzeug.serving import run_simple
from rest_api_framework.controllers import WSGIDispatcher
app = WSGIDispatcher([UserEndPoint])
run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)

Summary
~~~~~~~

So far, all of the code should look like this:

.. code-block:: python

from rest_api_framework import models
from rest_api_framework.datastore import SQLiteDataStore
from rest_api_framework.views import JsonResponse
from rest_api_framework.controllers import Controller


class UserModel(models.Model):

fields = [models.StringField(name="first_name", required=True),
models.StringField(name="last_name", required=True),
models.PkField(name="id", required=True)
]


class UserEndPoint(Controller):
ressource = {
"ressource_name": "users",
"ressource": {"name": "adress_book.db", "table": "users"},
"model": UserModel,
"datastore": SQLiteDataStore
}

controller = {
"list_verbs": ["GET", "POST"],
"unique_verbs": ["GET", "PUT", "DElETE"]
}

view = {"response_class": JsonResponse}

if __name__ == '__main__':
from werkzeug.serving import run_simple
from rest_api_framework.controllers import WSGIDispatcher
app = WSGIDispatcher([UserEndPoint])
run_simple('127.0.0.1', 5000, app, use_debugger=True, use_reloader=True)

Next: :doc:`using_user_endpoint`
62 changes: 62 additions & 0 deletions docs/build/_sources/using_user_endpoint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Playing with the newly created endpoint
=======================================

First you can check that your endpoint is up

.. code-block:: bash

curl -i "http://localhost:5000/users/"

HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 2
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Mon, 14 Oct 2013 12:52:22 GMT

Your endpoint is responding but does not have any data. Let's add
some:

Create a user
-------------

.. code-block:: bash

curl -i -H "Content-type: application/json" -X POST -d '{"first_name":"John", "last_name": "Doe"}' http://localhost:5000/users/

HTTP/1.0 201 CREATED
Location: http://localhost:5000/users/1
Content-Type: application/json
Content-Length: 0
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Mon, 14 Oct 2013 13:00:13 GMT

Error handling
~~~~~~~~~~~~~~

If you don't provide a last_name, the API will raise a BAD REQUEST
explaining your error:

.. code-block:: bash

curl -i -H "Content-type: application/json" -X POST -d '{"first_name":"John"}' http://localhost:5000/users/

HTTP/1.0 400 BAD REQUEST
Content-Type: application/json
Content-Length: 62
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Mon, 14 Oct 2013 13:21:10 GMT

{"error": "last_name is missing. Cannot create the ressource"}

The same apply if you dont give coherent data:

.. code-block:: bash

curl -i -H "Content-type: application/json" -X POST -d '{"first_name":45, "last_name": "Doe"}' http://localhost:5000/users/

HTTP/1.0 400 BAD REQUEST
Content-Type: application/json
Content-Length: 41
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Mon, 14 Oct 2013 13:24:53 GMT
{"error": "first_name does not validate"}
22 changes: 11 additions & 11 deletions docs/build/datastore.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Python Rest Api Framework 0.1 documentation" href="index.html" />
<link rel="next" title="Authentication and Authorization" href="authentication.html" />
<link rel="prev" title="What is Python REST API Framework" href="introduction.html" />
<link rel="next" title="Controllers" href="controller.html" />
<link rel="prev" title="Tutorial building an adressebook API" href="tutorial.html" />
</head>
<body>
<div class="related">
Expand All @@ -38,10 +38,10 @@ <h3>Navigation</h3>
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="authentication.html" title="Authentication and Authorization"
<a href="controller.html" title="Controllers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="introduction.html" title="What is Python REST API Framework"
<a href="tutorial.html" title="Tutorial building an adressebook API"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Python Rest Api Framework 0.1 documentation</a> &raquo;</li>
</ul>
Expand Down Expand Up @@ -285,7 +285,7 @@ <h2>Create a DataStore<a class="headerlink" href="#create-a-datastore" title="Pe

<dl class="method">
<dt id="rest_api_framework.datastore.base.DataStore.paginate">
<tt class="descname">paginate</tt><big>(</big><em>data</em>, <em>offset</em>, <em>count</em>, <em>**kwargs</em><big>)</big><a class="reference internal" href="_modules/rest_api_framework/datastore/base.html#DataStore.paginate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#rest_api_framework.datastore.base.DataStore.paginate" title="Permalink to this definition"></a></dt>
<tt class="descname">paginate</tt><big>(</big><em>data</em>, <em>offset</em>, <em>count</em><big>)</big><a class="reference internal" href="_modules/rest_api_framework/datastore/base.html#DataStore.paginate"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#rest_api_framework.datastore.base.DataStore.paginate" title="Permalink to this definition"></a></dt>
<dd><p>Paginate sould return all the object if no pagination options
have been set or only a subset of the ressources if pagination
options exists.</p>
Expand Down Expand Up @@ -344,11 +344,11 @@ <h3><a href="index.html">Table Of Contents</a></h3>
</ul>

<h4>Previous topic</h4>
<p class="topless"><a href="introduction.html"
title="previous chapter">What is Python REST API Framework</a></p>
<p class="topless"><a href="tutorial.html"
title="previous chapter">Tutorial building an adressebook API</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="authentication.html"
title="next chapter">Authentication and Authorization</a></p>
<p class="topless"><a href="controller.html"
title="next chapter">Controllers</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/datastore.txt"
Expand Down Expand Up @@ -381,10 +381,10 @@ <h3>Navigation</h3>
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="authentication.html" title="Authentication and Authorization"
<a href="controller.html" title="Controllers"
>next</a> |</li>
<li class="right" >
<a href="introduction.html" title="What is Python REST API Framework"
<a href="tutorial.html" title="Tutorial building an adressebook API"
>previous</a> |</li>
<li><a href="index.html">Python Rest Api Framework 0.1 documentation</a> &raquo;</li>
</ul>
Expand Down
31 changes: 21 additions & 10 deletions docs/build/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ <h1 id="index">Index</h1>
| <a href="#G"><strong>G</strong></a>
| <a href="#I"><strong>I</strong></a>
| <a href="#L"><strong>L</strong></a>
| <a href="#M"><strong>M</strong></a>
| <a href="#P"><strong>P</strong></a>
| <a href="#R"><strong>R</strong></a>
| <a href="#S"><strong>S</strong></a>
Expand Down Expand Up @@ -84,10 +85,6 @@ <h2 id="C">C</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%" valign="top"><dl>

<dt><a href="references.html#rest_api_framework.authentication.Authentication.check_auth">check_auth() (rest_api_framework.authentication.Authentication method)</a>
</dt>


<dt><a href="references.html#rest_api_framework.controllers.Controller">Controller (class in rest_api_framework.controllers)</a>
</dt>

Expand Down Expand Up @@ -134,11 +131,7 @@ <h2 id="D">D</h2>
</dl></td>
<td style="width: 33%" valign="top"><dl>

<dt><a href="references.html#rest_api_framework.controllers.Dispatcher.dispatch_request">dispatch_request() (rest_api_framework.controllers.Dispatcher method)</a>
</dt>


<dt><a href="references.html#rest_api_framework.controllers.Dispatcher">Dispatcher (class in rest_api_framework.controllers)</a>
<dt><a href="references.html#rest_api_framework.controllers.WSGIWrapper.dispatch_request">dispatch_request() (rest_api_framework.controllers.WSGIWrapper method)</a>
</dt>

</dl></td>
Expand Down Expand Up @@ -205,6 +198,10 @@ <h2 id="G">G</h2>
</dt>

</dl></dd>

<dt><a href="references.html#rest_api_framework.authentication.Authentication.get_user">get_user() (rest_api_framework.authentication.Authentication method)</a>
</dt>

</dl></td>
</tr></table>

Expand All @@ -222,7 +219,17 @@ <h2 id="L">L</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%" valign="top"><dl>

<dt><a href="references.html#rest_api_framework.controllers.Dispatcher.load_urls">load_urls() (rest_api_framework.controllers.Dispatcher method)</a>
<dt><a href="references.html#rest_api_framework.controllers.Controller.load_urls">load_urls() (rest_api_framework.controllers.Controller method)</a>
</dt>

</dl></td>
</tr></table>

<h2 id="M">M</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%" valign="top"><dl>

<dt><a href="references.html#rest_api_framework.controllers.Controller.make_options">make_options() (rest_api_framework.controllers.Controller method)</a>
</dt>

</dl></td>
Expand Down Expand Up @@ -332,6 +339,10 @@ <h2 id="W">W</h2>
<table style="width: 100%" class="indextable genindextable"><tr>
<td style="width: 33%" valign="top"><dl>

<dt><a href="references.html#rest_api_framework.controllers.WSGIWrapper.wsgi_app">wsgi_app() (rest_api_framework.controllers.WSGIWrapper method)</a>
</dt>


<dt><a href="references.html#rest_api_framework.controllers.WSGIDispatcher">WSGIDispatcher (class in rest_api_framework.controllers)</a>
</dt>

Expand Down
4 changes: 4 additions & 0 deletions docs/build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ <h1>Welcome to Python Rest Api Framework&#8217;s documentation!<a class="headerl
<li class="toctree-l2"><a class="reference internal" href="introduction.html#where-to-go-from-here">Where to go from here</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="tutorial.html">Tutorial building an adressebook API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="tutorial.html#first-step-building-a-user-endpoint">First Step Building a user endpoint</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="datastore.html">Datastore</a><ul>
<li class="toctree-l2"><a class="reference internal" href="datastore.html#using-a-datastore">Using a Datastore</a></li>
<li class="toctree-l2"><a class="reference internal" href="datastore.html#available-datastore">Available DataStore</a></li>
Expand Down
Binary file modified docs/build/objects.inv
Binary file not shown.
Loading

0 comments on commit 9b8c044

Please sign in to comment.