Coder Social home page Coder Social logo

apiexample's Introduction

APIExample

This is a simple Flask (http://flask.pocoo.org) application demonstrating exposing a MongoDB baced API. The application will store and retrieve data from MongoDB as well as perform simple IP-based throttling (configurable via a decorator).

Setup

  • Create virtualenv (mkvirtualenv apiexample or virtualenv --no-site-packages apiexample)
  • Activate virtualenv and install requirements: pip install -r requirements.txt
  • Start MongoDB and Redis (application uses default ports and db #12 for redis)
  • Start app: python application.py
  • Application should now be running on 0.0.0.0:5000

Usage

Insert data to MongoDB:

curl -v -d "test=123&moretesting=456" localhost:5000/

You will get a response (in JSON) like:

{
  "r_id": "1329142610"
}

Retrieve data from MongoDB:

curl localhost:5000/1329142610

You will get a response (in JSON) like:

{
  "_id": {"$oid": "4f391b52db487d114b000001"}, 
  "data": {"moretesting": ["456"], 
  "test": ["123"]}, 
  "r_id": "1329142610"
}

Throttling

To test throttling, simply perform a series of requests against the throttled url. By default, the / url is limited to 50 req/20 sec and the /visitors url is limited to 10 req/20 sec.

curl localhost:5000/visitors    (x15 in under 20 seconds)

You will get a response (in JSON) like:

{
  "status": "fail", 
  "response": "Over limit ; try again later"
}

Tests

There are some basic default tests in tests.py. One performs a simple request on the / url, checking for HTTP status code. The other performs a request on /visitors checking content.

To run the tests:

python tests.py -v

apiexample's People

Contributors

ehazlett avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.