Package: auto-api
AutoApiThe goal of AutoApi is avoid developing anAPI RESTat the start of a project, making a prototype easier than usual. AutoApi also has an authentication system and multiple APIs are supported.QuickstartAssuming you have MongoDB server running inlocalhoston the default port27017without authentication, AutoApi can be started as following:Docker container$dockerrun-it-eMONGO_HOST=localhost-eMONGO_PORT=27017felipevalverde/autoapi:latest...Listeningat:http://0.0.0.0:8686Python module$workonapi(api)$pipinstallauto_api(api)$MONGO_HOST=localhostMONGO_PORT=27017python-mauto_apirun*Runningonhttp://0.0.0.0:8686/(PressCTRL+Ctoquit)...A personal agenda is a good example to show how AutoApi works. We will use theexampleAPI to insert and retrieve items fromagendacollection.InsertTo add an item in the agenda, the following HTTP request shows how to do it:POSThttp://localhost:8686/example/agendaContent-Type: application/json

{
  "name": "user",
  "email": "user@email.com",
  "phone": "+123 456-789",
  "address": "123 Street"
}It's important to add/example(API name) before the REST path/agenda, because that is the way AutoApi identifies them.
The response will contain the id of the created item as:{"id": "591a79400000000000000000"}Where the value ofidwill always be aMongoDB ObjectId.RetrieveTo get the previous inserted item in the agenda, is required to know theidof the item. The previous response shows theidis591a79400000000000000000, so the item can be retrieve making the following HTTP request:GEThttp://localhost:8686/example/agenda/591a79400000000000000000In the same way as the insert operation, the API name and the REST path are required, in this case the path is/agenda/591a79400000000000000000. The response will contain the initial inserted data and the AutoApi assignedid:{
  "id": "591a79400000000000000000",
  "name": "user",
  "email": "user@email.com",
  "phone": "+123 456-789",
  "address": "123 Street"
}But, if you want to retrieve all the items of the agenda, the following HTTP request shows how:GEThttp://localhost:8686/example/agendaAnd the response will be:[
  {
    "id": "591a79400000000000000000",
    "name": "user",
    "email": "user@email.com",
    "phone": "+123 456-789",
    "address": "123 Street"
  },
  ...
]How does AutoApi work?AutoApi was develop onPythonusingFlaskandMongoDB, it was thought to support multiples API because AutoApi uses each database to represent each API. This means that to differentiate between two APIs it is necessary to add the API name as a prefix in the URL. For instance, to retrieve all the movies fromimdb-copyAPI it is necessary to do aGETto/imdb-copy/movies, and to retrieve the movies fromrottentomatoes-copyAPI the URL is/rottentomatoes-copy/movies.Another important AutoApi's feature is the authentication, but authentication in for this tool is at API level, this means that users can not be shared between APIs. The reason why users can not be shared is because AutoApi uses MongoDB users instead of a collection to store them, so they are strictly related to a database and as AutoApi is considering a database as an API they are isolated per database.ConfigurationAutoApi uses MongoDB to store all the neccesary data, then it is necessary to know the location of the database, this means that next environment variablesmust be provided:MONGO_HOSTandMONGO_PORT.In the same way, if authentication is needed, the next environment variables must be provided too:MONGO_ADMINandMONGO_ADMIN_PASS.AutoApi featuresAuthentication & AuthorizationAutoApi authentication is optional, by default it is not activated. To activate it is necessary:MongoDB authactivatedAutoApi admin configurationsRun AutoApi serverwith --auth flagAuthenticationEach API has their own users, so users have to logged specifying the API in the request:POST/loginContent-Type: application/json

{
  "api": "example",
  "email": "user@email.com",
  "password": "pass"
}The response will contain a session token in the headers and body:X-Email: user@email.comX-Token: 123456

{
  "email": "user@email.com",
  "token": "123456"
}To logout, users have to specify the API too:POST/logoutContent-Type: application/jsonX-Email: user@email.comX-Token: 123456

{"api": "example"}Users and AuthorizationOnly admin userscan create more users specifying the API and CRUD roles:POST/userContent-Type: application/jsonX-Email: ADMIN_USERX-Token: ADMIN_USER_TOKEN

{
  "email": "other_user@email.com",
  "password": "pass",
  "api": "example",
  "roles": ["read", "update"]
}The last request creates the userother_user@email.comand authorizes him toreadandupdatetheexampleAPI without any API creation request.Each user can update his own password and only an admin user can change other users password . The change can be done using the following request:POST/passwordContent-Type: application/jsonX-Email: USERX-Token: USER_TOKEN

{
  "email": "other_user@email.com",
  "password": "new-pass",
  "api": "example"
}It is important to note that the request needs theemailparameter to select to user that will change the password.Finally, only an admin user can change the authorization roles for a particular user using the following request:POST/rolesContent-Type: application/jsonX-Email: ADMIN_USERX-Token: ADMIN_USER_TOKEN

{
  "email": "other_user@email.com",
  "api": "example",
  "roles": {
    "update": false,
    "delete": true
  }
}Collections and ResourcesAPITo use an API in AutoApi it is not necessary to create it, it is created on demand and there is no operations related for path/api.API collectionTo use and API collection in AutoApi it is not necessary to create it, it is also created on demand.CRUD collection's resourcesIt is important to remember that if AutoApi's authentication is enabled then only logged users, with the respective authorization, can CRUD API's resources.A good API REST example is to show how to mark as a classic all the movies whereactor_1appears.PATCH/example/actors/actor_1/moviesContent-Type: application/jsonX-Email: user@email.comX-Token: USER_TOKEN

{"classic": true}More info about REST:http://www.restapitutorial.com/lessons/httpmethods.htmlhttp://restful-api-design.readthedocs.org/en/latest/Dependencies and configurationInstallationDocker containerYou can pull the latest docker image as following:dockerpullfelipevalverde/autoapi:latestPython moduleYou can install the latest python module. I strongly recommend you to usevirtualenvandvirtualenvwrapperas following:$workonapi(api)$pipinstallauto_apiMongoDBAutoApi doesn't required modifications on MongoDB configuration to handle APIs, collectios or resources. But, if you want to activateAuthenticationandAuthorization, as AutoApi uses MongoDB users, it is necessary to setauth=truein yourmongodb.cfgor runmongodwith the flag--authand provide the neccesaryAutoApi environment variables.Related info:http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/http://docs.mongodb.org/manual/core/authenticationhttp://docs.mongodb.org/manual/core/authorizationhttp://docs.mongodb.org/manual/tutorial/add-user-administratorRunning AutoApiAfterinstalling AutoApiit will be created an executable calledautoapiand the python moduleauto_api. Also, remember that if you want to run AutoApi with authentication, you must firstturn on the authentication in MongoDBand then provide the flag--auth.(api)$autoapirun[--auth]or(api)$python-mauto_apirun[--auth]Testing AutoApiTo run the AutoApi test there is a script calledrun_tests.py, that automatically start and stop two MongoDB servers for testing purpose only (one with authentication enabled).(api)$./run_tests.pyor(api)$pythonsetup.pyrun_tests[-a'[pytest-parameters]']