Pages

Sunday, 26 January 2014

一个基于django的项目-Moksaya


Creating a restful interface to moksaya and the website/front end would be a client which consumes the RESTful Api itself

Setup:

$virtualenv ../ENV
$source ../ENV/bin/activate
(ENV)$pip install -r requirements.txt
(ENV)$fab setup start

TODO:

1: Implement Proper authorization Class to Apis        

Authentication:

I am using the ApiKeyAuthentication class offered by the django-tastypie, so in order to set this up once you have setup the project login to 127.0.0.1:/admin and login with the super user you created while setting up the project.You will find Tastypie app with Api keys model.Select it and add api key for desired user.
   # Pass it as GET params       
   curl http://127.0.0.1:8000/api/v1/profile/?username=<yourusername>\&api_key=<yourapikey>
Each user is supposed to get their own api key , which could be autogenerated and send in the request ,currently I have manually set the api_key for super user and I will use it to perform various operations.
Reffer Tastypie documentation to know more about it: http://django-tastypie.readthedocs.org/en/latest/authentication.html#apikeyauthentication

Authorization:

Tastypie seperates the authentication and authorization pretty neatly ,I am using DjangoAuthorization class from tastypie.authorization.This class provides the authenticated user to access the areas of the application for which they are authorized in django user permission.

Test:

After setting up the project for the first time , you would need to register an user and get the api_key to access the resources. You can also setup the superuser while setting up the project with ./manage.py syncdb or fab setup
To Register a new user make the following curl request:
 $curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"username":"spock","password":"notebook"}' http://127.0.0.1:8000/api/v1/register/ 
To get your api_key :
 $ curl -k --user "username:password" http://127.0.0.1:8000/api/v1/token/auth/
This should return a json resoponse like this :
 {
"key": "600ca8c91d70376c7427854687d979ad97bb92ef"
 }       
Next thing you need to do is to edit thhe fabfile
 user = "spock"
 #Provide the username you used while setting up the project.
 key =  "600ca8c91d70376c7427854687d979ad97bb92ef"
 #Add the api_key that you createdd in the admin 
Now , this fabric script would test basic use cases for the project using curl.Fabric is included in the dependencies so just open a new terminal to the root of project:
 $source ../ENV/bin/activate
 (ENV)$fab test
Apart from fab test , you may try fab fabfile.py to get list of commands. This test cases covers primariliy these Basic Operations:
 * POST request to populate the data base < fab PostTest >
 * GET  request to retrive all the accessible json data feed < fab GetTest >
 * PATCH requst  ,we can use POST and PATCH interchangeably they 
and I find PATCH more comfortable thant PUT < fab PatchTest >
 * DELETE request to simliy remove the entries from the database < fab clean > , 
this function performs the DELETE operation on all the Resources ie : Users,Profiles,
Projects,Comments,Likes,Connections but leaving the admin or super user intact.
Uppon successful completation , you woul find a project forked to super users profile.

Interacting with the APIs :

Okay , lets say first thing we want to do is to create a new user say 'Spock':
   $curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"username":"spock","password":"notebook"}' http://127.0.0.1:8000/api/v1/register/ 
 
from  https://github.com/aregee/moksaya