Skip to content

Commit 9e598c4

Browse files
authored
Merge pull request #27 from ioet/feature/add-examples-in-swagger#26
Close #26
2 parents ba0ad4d + 09adcb8 commit 9e598c4

File tree

8 files changed

+67
-31
lines changed

8 files changed

+67
-31
lines changed

requirements/dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pytest==5.2.0
99

1010
# Mocking
1111
pytest-mock==2.0.0
12+
Faker==4.0.2
1213

1314
# Coverage
1415
coverage==4.5.1
File renamed without changes.

time_tracker_api/activities/activities_namespace.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from flask_restplus import fields, Resource, Namespace
22
from time_tracker_api.api import audit_fields
3+
from faker import Faker
4+
5+
faker = Faker()
36

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

@@ -10,10 +13,12 @@
1013
title='Name',
1114
max_length=50,
1215
description='Canonical name of the activity',
16+
example=faker.word(['Development', 'Training']),
1317
),
1418
'description': fields.String(
1519
title='Description',
1620
description='Comments about the activity',
21+
example=faker.paragraph(),
1722
),
1823
})
1924

@@ -23,6 +28,7 @@
2328
required=True,
2429
title='Identifier',
2530
description='The unique identifier',
31+
example=faker.random_int(1, 9999),
2632
)
2733
}
2834
activity_response_fields.update(audit_fields)

time_tracker_api/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
from flask_restplus import Api, fields
2+
from faker import Faker
3+
4+
faker = Faker()
25

36
api = Api(version='1.0.1', title="TimeTracker API",
47
description="API for the TimeTracker project")
@@ -8,19 +11,22 @@
811
'created_at': fields.Date(
912
readOnly=True,
1013
title='Created',
11-
description='Date of creation'
14+
description='Date of creation',
15+
example=faker.iso8601(end_datetime=None),
1216
),
1317
'tenant_id': fields.String(
1418
readOnly=True,
1519
title='Tenant',
1620
max_length=64,
1721
description='The tenant this belongs to',
22+
example=faker.random_int(1, 9999),
1823
),
1924
'created_by': fields.String(
2025
readOnly=True,
2126
title='Creator',
2227
max_length=64,
2328
description='User that created it',
29+
example=faker.random_int(1, 9999),
2430
),
2531
}
2632

time_tracker_api/projects/projects_model.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@ def __init__(self):
1010
def get_all(self):
1111
return self.projects
1212

13-
def get(self, uid):
13+
def get(self, id):
1414
for project in self.projects:
15-
if project.get('uid') == uid:
15+
if project.get('id') == id:
1616
return project
17-
raise MissingResource("Project '%s' not found" % uid)
17+
raise MissingResource("Project '%s' not found" % id)
1818

1919
def create(self, project):
2020
self.counter += 1
21-
project['uid'] = str(self.counter)
21+
project['id'] = str(self.counter)
2222
self.projects.append(project)
2323
return project
2424

25-
def update(self, uid, data):
26-
project = self.get(uid)
25+
def update(self, id, data):
26+
project = self.get(id)
2727
if project:
2828
project.update(data)
2929
return project
3030
else:
31-
raise MissingResource("Project '%s' not found" % uid)
31+
raise MissingResource("Project '%s' not found" % id)
3232

33-
def delete(self, uid):
34-
if uid:
35-
project = self.get(uid)
33+
def delete(self, id):
34+
if id:
35+
project = self.get(id)
3636
self.projects.remove(project)
3737

3838
def flush(self):

time_tracker_api/projects/projects_namespace.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from .projects_model import project_dao
33
from time_tracker_api.errors import MissingResource
44
from time_tracker_api.api import audit_fields
5+
from faker import Faker
6+
7+
faker = Faker()
58

69
ns = Namespace('projects', description='API for projects (clients)')
710

@@ -11,17 +14,20 @@
1114
required=True,
1215
title='Name',
1316
max_length=50,
14-
description='Name of the project'
17+
description='Name of the project',
18+
example=faker.company(),
1519
),
1620
'description': fields.String(
1721
title='Description',
18-
description='Description about the project'
22+
description='Description about the project',
23+
example=faker.paragraph(),
1924
),
2025
'type': fields.String(
2126
required=True,
2227
title='Type',
2328
max_length=30,
2429
description='If it is `Costumer`, `Training` or other type',
30+
example=faker.word(['Customer', 'Training']),
2531
),
2632
})
2733

@@ -31,6 +37,7 @@
3137
required=True,
3238
title='Identifier',
3339
description='The unique identifier',
40+
example=faker.random_int(1, 9999),
3441
)
3542
}
3643
project_response_fields.update(audit_fields)
@@ -66,24 +73,24 @@ def post(self):
6673
help='Is the project active?')
6774

6875

69-
@ns.route('/<string:uid>')
76+
@ns.route('/<string:id>')
7077
@ns.response(404, 'Project not found')
71-
@ns.param('uid', 'The project identifier')
78+
@ns.param('id', 'The project identifier')
7279
class Project(Resource):
7380
@ns.doc('get_project')
7481
@ns.marshal_with(project)
75-
def get(self, uid):
82+
def get(self, id):
7683
"""Retrieve a project"""
77-
return project_dao.get(uid)
84+
return project_dao.get(id)
7885

7986
@ns.doc('update_project_status')
8087
@ns.expect(project)
8188
@ns.response(204, 'State of the project successfully updated')
82-
def post(self, uid):
89+
def post(self, id):
8390
"""Updates a project using form data"""
8491
try:
8592
update_data = project_update_parser.parse_args()
86-
return project_dao.update(uid, update_data), 200
93+
return project_dao.update(id, update_data), 200
8794
except ValueError:
8895
abort(code=400)
8996
except MissingResource as e:
@@ -92,13 +99,13 @@ def post(self, uid):
9299
@ns.doc('put_project')
93100
@ns.expect(project_input)
94101
@ns.marshal_with(project)
95-
def put(self, uid):
102+
def put(self, id):
96103
"""Create or replace a project"""
97-
return project_dao.update(uid, ns.payload)
104+
return project_dao.update(id, ns.payload)
98105

99106
@ns.doc('delete_project')
100107
@ns.response(204, 'Project deleted successfully')
101-
def delete(self, uid):
108+
def delete(self, id):
102109
"""Deletes a project"""
103-
project_dao.delete(uid)
110+
project_dao.delete(id)
104111
return None, 204

time_tracker_api/technologies/technologies_namespace.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from flask_restplus import Namespace, Resource, fields
22
from time_tracker_api.api import audit_fields
3+
from faker import Faker
4+
5+
faker = Faker()
36

47
ns = Namespace('technologies', description='API for technologies used')
58

@@ -9,7 +12,8 @@
912
required=True,
1013
title='Name',
1114
max_length=50,
12-
description='Name of the technology'
15+
description='Name of the technology',
16+
example=faker.word(['Java', 'Python', 'Elixir'])
1317
),
1418
})
1519

@@ -19,6 +23,7 @@
1923
required=True,
2024
title='Identifier',
2125
description='The unique identifier',
26+
example=faker.random_int(1, 9999),
2227
),
2328
}
2429
technology_response_fields.update(audit_fields)
@@ -46,25 +51,25 @@ def post(self):
4651
return ns.payload, 201
4752

4853

49-
@ns.route('/<string:uid>')
54+
@ns.route('/<string:id>')
5055
@ns.response(404, 'Technology not found')
51-
@ns.param('uid', 'The technology identifier')
56+
@ns.param('id', 'The technology identifier')
5257
class Technology(Resource):
5358
@ns.doc('get_technology')
5459
@ns.marshal_with(technology)
55-
def get(self, uid):
60+
def get(self, id):
5661
"""Retrieve a technology"""
5762
return {}
5863

5964
@ns.doc('put_technology')
6065
@ns.expect(technology_input)
6166
@ns.marshal_with(technology)
62-
def put(self, uid):
67+
def put(self, id):
6368
"""Updates a technology"""
6469
return ns.payload()
6570

6671
@ns.doc('delete_technology')
6772
@ns.response(204, 'Technology deleted successfully')
68-
def delete(self, uid):
73+
def delete(self, id):
6974
"""Deletes a technology"""
7075
return None, 204

time_tracker_api/time_entries/time_entries_namespace.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from flask_restplus import fields, Resource, Namespace
22
from time_tracker_api.api import audit_fields
33
from time_tracker_api import database
4+
from faker import Faker
5+
6+
faker = Faker()
47

58
ns = Namespace('time-entries', description='API for time entries')
69

@@ -11,12 +14,14 @@
1114
title='Project',
1215
max_length=64,
1316
description='The id of the selected project',
17+
example=faker.random_int(1, 9999),
1418
),
1519
'activity_id': fields.String(
1620
required=False,
1721
title='Activity',
1822
max_length=64,
1923
description='The id of the selected activity',
24+
example=faker.random_int(1, 9999),
2025
),
2126
'technologies': fields.String(
2227
required=True,
@@ -26,17 +31,20 @@
2631
),
2732
'description': fields.String(
2833
title='Comments',
29-
description='Comments about the time entry'
34+
description='Comments about the time entry',
35+
example=faker.paragraph(),
3036
),
3137
'start_date': fields.DateTime(
3238
required=True,
3339
title='Start date',
3440
description='When the user started doing this activity',
41+
example=faker.iso8601(end_datetime=None),
3542
),
3643
'end_date': fields.DateTime(
3744
required=True,
3845
title='End date',
3946
description='When the user ended doing this activity',
47+
example=faker.iso8601(end_datetime=None),
4048
),
4149
})
4250

@@ -46,11 +54,13 @@
4654
required=True,
4755
title='Identifier',
4856
description='The unique identifier',
57+
example=faker.random_int(1, 9999),
4958
),
5059
'running': fields.Boolean(
5160
readOnly=True,
5261
title='Is it running?',
53-
description='Whether this time entry is currently running or not'
62+
description='Whether this time entry is currently running or not',
63+
example=faker.boolean(),
5464
),
5565
}
5666
time_entry_response_fields.update(audit_fields)
@@ -64,6 +74,7 @@
6474

6575
model = database.create('time-entries')
6676

77+
6778
@ns.route('')
6879
class TimeEntries(Resource):
6980
@ns.doc('list_time_entries')

0 commit comments

Comments
 (0)