Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# time-tracker-api

## Getting started
Follow the following instructions to get the project ready to use ASAP:

### Requirements
Be sure you have installed in your system

- [Python version 3](https://www.python.org/download/releases/3.0/) in your path. It will install
automatically [pip](https://pip.pypa.io/en/stable/) as well.
- A virtual environment, namely [venv](https://docs.python.org/3/library/venv.html).

## Setup

- Create and activate the environment,
Expand Down Expand Up @@ -36,4 +46,5 @@
flask run
```

- Open `http://127.0.0.1:5000/` in a browser
- Open `http://127.0.0.1:5000/` in a browser. You will find in the presented UI
a link to the swagger.json with the definition of the api.
10 changes: 6 additions & 4 deletions time_tracker_api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from flask import Flask
from flask_restplus import Api


def create_app():
flask_app = Flask(__name__)
app = Api(app=flask_app)

from time_tracker_api.projects.projects_endpoints import ns as projects_ns
app.add_namespace(projects_ns)
init_app(flask_app)

return flask_app


def init_app(app: Flask):
from .api import api
api.init_app(app)
57 changes: 57 additions & 0 deletions time_tracker_api/activities/activities_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from flask_restplus import fields, Resource, Namespace

ns = Namespace('activities', description='API for activities')

# Activity Model
activity = ns.model('Activity', {
'id': fields.String(readOnly=True, required=True, title='Identifier',
description='The unique id of the activity'),
'name': fields.String(required=True, title='Name', max_length=50,
description='Canonical name of the activity'),
'description': fields.String(title='Description',
description='Comments about the activity'),
'tenant_id': fields.String(required=True, title='Tenant', max_length=64,
description='The tenant this belongs to')
})


@ns.route('/')
class Activities(Resource):
@ns.doc('list_activities')
@ns.marshal_list_with(activity, code=200)
def get(self):
"""List all available activities"""
return []

@ns.doc('create_activity')
@ns.expect(activity)
@ns.marshal_with(activity, code=201)
@ns.response(400, 'Invalid format of the attributes of the activity.')
def post(self):
"""Create a single activity"""
return ns.payload, 201


@ns.route('/<string:id>')
@ns.response(404, 'Activity not found')
@ns.param('id', 'The unique identifier of the activity')
class Activity(Resource):
@ns.doc('get_activity')
@ns.marshal_with(activity)
def get(self, id):
"""Retrieve all the data of a single activity"""
return {}

@ns.doc('delete_activity')
@ns.response(204, 'The activity was deleted successfully (No content is returned)')
def delete(self, id):
"""Deletes a activity"""
return None, 204

@ns.doc('put_activity')
@ns.response(400, 'Invalid format of the attributes of the activity.')
@ns.expect(activity)
@ns.marshal_with(activity)
def put(self, id):
"""Updates an activity"""
return ns.payload
12 changes: 12 additions & 0 deletions time_tracker_api/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

from flask_restplus import Api

api = Api(version='1.0.1', title="TimeTracker API",
description="API for the TimeTracker project")

# APIs
from time_tracker_api.projects import projects_api
Copy link
Contributor

@Angeluz-07 Angeluz-07 Mar 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we put all the imports at the top?

On the other hand, the import from time_tracker_api.projects import projects_api from what I understand, you import the file projects_api and use it to reference functions inside of it. But It took me some minutes to figure it out. I think it would more easy to grasp something like from time_tracker_api.projects.projects_api import ns as projects_ns. But feel free to argument your approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that when I see the variable projects_ns it looks like an alias but it is not clear where it comes from, also it makes that import line bigger. When I see projects_api.ns, I understand that it comes from projects_api and that the variable it references is ns, there is no possible abstract interpretation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did the import in the same part where I added that namespace because it seemed a good idea to have them together in case I wanted to remove or comment the inclusion of a particular namespace.

api.add_namespace(projects_api.ns)

from time_tracker_api.activities import activities_api
api.add_namespace(activities_api.ns)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .projects_model import project_dao
from time_tracker_api.errors import MissingResource

ns = Namespace('projects', description='Api for resource `Projects`')
ns = Namespace('projects', description='API for projects (clients)')

@ns.route('/')
class Projects(Resource):
Expand Down