Skip to content

Commit d9bb604

Browse files
author
EliuX
committed
Fixes #4 Create API for activities
1 parent c3c0963 commit d9bb604

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# time-tracker-api
22

3+
## Getting started
4+
Follow the following instructions to get the project ready to use ASAP:
5+
6+
### Requirements
7+
Be sure you have installed in your system
8+
9+
- [Python version 3](https://www.python.org/download/releases/3.0/) in your path. It will install
10+
automatically [pip](https://pip.pypa.io/en/stable/) as well.
11+
- A virtual environment, namely [venv](https://docs.python.org/3/library/venv.html).
12+
313
## Setup
414

515
- Create and activate the environment,
@@ -36,4 +46,5 @@
3646
flask run
3747
```
3848
39-
- Open `http://127.0.0.1:5000/` in a browser
49+
- Open `http://127.0.0.1:5000/` in a browser. You will find in the presented UI
50+
a link to the swagger.json with the definition of the api.

time_tracker_api/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ def create_app():
99
return flask_app
1010

1111

12-
def init_app(app):
12+
def init_app(app: Flask):
1313
from .api import api
14-
api.init_app(app)
14+
api.init_app(app)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from flask_restplus import fields, Resource, Namespace
2+
3+
ns = Namespace('activities', description='API for activities')
4+
5+
# Activity Model
6+
activity = ns.model('Activity', {
7+
'id': fields.String(readOnly=True, required=True, title='Identifier',
8+
description='The unique id of the activity'),
9+
'name': fields.String(required=True, title='Name', max_length=50,
10+
description='Canonical name of the activity'),
11+
'description': fields.String(title='Description',
12+
description='Comments about the activity'),
13+
'tenant_id': fields.String(required=True, title='Tenant', max_length=64,
14+
description='The tenant this belongs to')
15+
})
16+
17+
18+
@ns.route('/')
19+
class Activities(Resource):
20+
@ns.doc('list_activities')
21+
@ns.marshal_list_with(activity, code=200)
22+
def get(self):
23+
"""List all available activities"""
24+
return []
25+
26+
@ns.doc('create_activity')
27+
@ns.expect(activity)
28+
@ns.marshal_with(activity, code=201)
29+
@ns.response(400, 'Invalid format of the attributes of the activity.')
30+
def post(self):
31+
"""Create a single activity"""
32+
return ns.payload, 201
33+
34+
35+
@ns.route('/<string:id>')
36+
@ns.response(404, 'Activity not found')
37+
@ns.param('id', 'The unique identifier of the activity')
38+
class Activity(Resource):
39+
@ns.doc('get_activity')
40+
@ns.marshal_with(activity)
41+
def get(self, id):
42+
"""Retrieve all the data of a single activity"""
43+
return {}
44+
45+
@ns.doc('delete_activity')
46+
@ns.response(204, 'The activity was deleted successfully (No content is returned)')
47+
def delete(self, id):
48+
"""Deletes a activity"""
49+
return None, 204
50+
51+
@ns.doc('put_activity')
52+
@ns.response(400, 'Invalid format of the attributes of the activity.')
53+
@ns.expect(activity)
54+
@ns.marshal_with(activity)
55+
def put(self, id):
56+
"""Updates an activity"""
57+
return ns.payload

time_tracker_api/api.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
description="API for the TimeTracker project")
66

77
# APIs
8-
from time_tracker_api.projects import projects_endpoints
9-
api.add_namespace(projects_endpoints.ns)
8+
from time_tracker_api.projects import projects_api
9+
api.add_namespace(projects_api.ns)
10+
11+
from time_tracker_api.activities import activities_api
12+
api.add_namespace(activities_api.ns)

time_tracker_api/projects/projects_endpoints.py renamed to time_tracker_api/projects/projects_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from .projects_model import project_dao
33
from time_tracker_api.errors import MissingResource
44

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

77
@ns.route('/')
88
class Projects(Resource):

0 commit comments

Comments
 (0)