Skip to content

znanl/flask-user-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flask User API

A simple User RESTFUL API built using Flask Framework (http://flask.pocoo.org/) and Sqlalchemy (http://www.sqlalchemy.org/)

Requirements

Python 2.7.x

Installation

  1. Create a virtual with:

     mkvirtualenv flaskapi
     workon flaskapi
    
  2. Install requirements file:

     pip install -r requirements.txt
    
  3. Run unit test suite:

     python manage.py test
    
  4. Create database:

     python manage.py syncdb
    
  5. Run app:

     python manage.py runserver
    

User API

The main goal is desing and built a RestFull API that handles user registration and login for a new service (let's say a music service but could be anything). The API format will be JSON. The first thing that we must do is define a root url: ::

http://[hostname]/mymusic/api/v1.0/

After that we will define the User Resource urls that will allows to create, query, update, delete an user.

Create an User

POST request to create an user: ::

curl -X POST localhost:5000/mymusic/api/v1.0/users/ -H "Content-Type: application/json" \
     -d '{"username": "maigfrga", "email": "maigfrga@gmail.com", \
          "last_name": "franco", "first_name": "manuel"}'

This request will return a json with the user information, access_token included: ::

    {
      "user": {
        "access_token": "dda568fe6781259a1f9b910c6704b4da", 
        "email": "maigfrga@gmail.com", 
        "first_name": "manuel", 
        "id": 1, 
        "last_name": "franco", 
        "username": "maigfrga"
      }
    }

Authentication

GET, PUT, DELETE request requires authentication, the API expects two headers api_access_token and api_username the next example will return user information, authentication is required: ::

curl  localhost:5000/mymusic/api/v1.0/users/ -H "api_access_token: dda568fe6781259a1f9b910c6704b4da" \
     -H "api_username: maigfrga"

This call will return the user data: ::

{
    "access_token": "dda568fe6781259a1f9b910c6704b4da", 
    "email": "maigfrga@gmail.com", 
      "first_name": "manuel", 
      "id": 1, 
      "last_name": "franco", 
      "username": "maigfrga"
}

Update an user

Perform a PUT request will update the user resource: ::

    curl -X PUT  localhost:5000/mymusic/api/v1.0/users/ -H "Content-Type: application/json" \
         -H "api_access_token: dda568fe6781259a1f9b910c6704b4da" \
         -H "api_username: maigfrga" -d '{"last_name": "last name modified"}'

Delete an user

Perform a DELETE request will delete the user resource: ::

    curl -X DELETE  localhost:5000/mymusic/api/v1.0/users/  \
     -H "api_access_token: dda568fe6781259a1f9b910c6704b4da" \
     -H "api_username: maigfrga"

See also

Flask Framework Website

Flask python3 support

Sqlalchemy Website

Designing a RESTful API with Python and Flask

Unit testing framework

argparse — Parser for command-line options, arguments and sub-commands

Python @property versus getters and setters

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%