Skip to content

Commit 159e16c

Browse files
#57 Refactoring APIGmS to improve and minfy the code.
1 parent 7f61e3e commit 159e16c

File tree

2 files changed

+86
-68
lines changed

2 files changed

+86
-68
lines changed

SMS-Back-End/apigms/apigms_api.py

+83-66
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,56 @@
1111
# Activating verbose mode
1212
v = 1
1313

14+
#DEFAULT PORT TO THIS microService in local: 8001
15+
1416
microservice_name = '\n ## dbms Data Base mService ##'
1517

1618
module = modules.get_current_module_name()
1719
instance = modules.get_current_instance_id()
1820

19-
# Test method.
21+
22+
def ping(url, payload=None, method=None):
23+
"""
24+
Method that isolates the way to do the request front all microservices and how processes the response.
25+
26+
:param url:
27+
:param payload:
28+
:param method:
29+
:return:
30+
"""
31+
32+
if payload is None:
33+
request = urllib2.Request(url)
34+
if method == 'DELETE':
35+
request.get_method = lambda: "DELETE"
36+
37+
else:
38+
# We need send data with json format and by default is POST
39+
request = urllib2.Request(url, data=json.dumps(payload.get_json()), headers={'Content-Type': 'application/json'})
40+
# We can force to PUT if is the method that we need
41+
if method is 'PUT':
42+
request.get_method = lambda: "PUT"
43+
44+
response = make_response(Response(urllib2.urlopen(request), mimetype='application/json'))
45+
response.headers['Access-Control-Allow-Origin'] = "*"
46+
47+
return response
48+
49+
2050
@app.route('/test',methods=['GET'])
2151
def test():
2252
"""
23-
Test resource.
53+
Test resource.
2454
25-
Example of use:
26-
curl -i -X GET localhost:8002/test
55+
:return:
56+
57+
Example of use:
58+
curl -i -X GET localhost:8002/test
2759
"""
2860

2961
url = "http://%s/" % modules.get_hostname(module='dbms')
3062
url += 'test'
63+
3164
req = urllib2.Request(url)
3265
f = urllib2.urlopen(req)
3366
response = f.read()
@@ -38,72 +71,63 @@ def test():
3871

3972
@app.route('/entities/<string:kind>', methods=['POST'])
4073
def put_entity(kind):
74+
"""
75+
Data Base micro Service Resource connector, put all kind of entities in this mService.
4176
42-
url = "http://%s/" % modules.get_hostname(module='dbms')
43-
url += 'entities/' + str(kind)
77+
:param kind:
78+
:payload json:
79+
:return:
4480
45-
req = urllib2.Request(url, request.get_data(), {'Content-Type': 'application/json'})
46-
f = urllib2.urlopen(req)
47-
response = f.read()
48-
f.close()
81+
Example of use:
82+
curl -H "Content-Type: application/json" -X POST -d '{ "data": {"name": "María"} }' localhost:8001/entities/student
4983
50-
return response
84+
"""
85+
86+
url = 'http://' + str(modules.get_hostname(module='dbms')) + '/' + 'entities/' + str(kind)
87+
88+
return ping(url=url, payload=request, method='POST')
5189

5290

5391
@app.route('/entities/<string:kind>', methods=['GET'])
5492
@app.route('/entities/<string:kind>/<int:entity_id>', methods=['GET'])
5593
def get_entities(kind, entity_id=None):
56-
# curl -i -X GET localhost:8080/entities/teacher
57-
58-
url = "http://%s/" % modules.get_hostname(module='dbms')
59-
url += 'entities/' + str(kind)
60-
61-
if entity_id is not None:
62-
url += '/' + str(entity_id)
63-
64-
print url
94+
"""
95+
Data Base micro Service Resource connector, to get info about all kind of entities in this mService.
6596
66-
req = urllib2.Request(url)
67-
f = urllib2.urlopen(req)
68-
response = f.read()
97+
:param kind: Type of entity to get info.
98+
:param entity_id:
99+
:return: Exactly the response which is received from service.
69100
70-
print type(response)
71-
response = json.dumps(response)
72-
print type(response)
101+
Example of use:
73102
74-
f.close()
103+
curl -i -X GET localhost:8001/entities/teacher
75104
105+
See more info in dbms_apy.py
76106
"""
77-
res = Response(json.dumps(response), mimetype='application/json')
78107

79-
rews = make_response(res)
108+
url = 'http://' + str(modules.get_hostname(module='dbms')) + '/' + 'entities/' + str(kind)
80109

81-
rews.headers['Access-Control-Allow-Origin'] = "*"
82-
"""
83-
84-
return response
110+
if entity_id is not None:
111+
url += '/' + str(entity_id)
85112

86-
#return Response(json.dumps(list), mimetype='application/json')
87-
#return jsonify(r)
88-
#return response
113+
return ping(url)
89114

90115

91116
@app.route('/entities/<string:kind>/<int:entity_id>', methods=['PUT'])
92117
def update_entities(kind, entity_id):
93-
# curl -H "Content-Type: application/json" -X PUT -d '{ "data": {"name": "NombreModificado"} }' localhost:8001/entities/teacher/1
94-
95-
url = "http://%s/" % modules.get_hostname(module='dbms')
96-
url += 'entities/' + str(kind) + '/' + str(entity_id)
118+
"""
119+
Data Base micro Service Resource connector, to update all kind of entities in this mService.
97120
98-
print url
121+
:param kind:
122+
:param entity_id:
123+
:return:
99124
100-
req = urllib2.Request(url, request.get_data(), {'Content-Type': 'application/json'})
101-
req.get_method = lambda: "PUT"
102-
f = urllib2.urlopen(req)
103-
response = f.read()
104-
f.close()
125+
Exampe of use:
126+
# curl -H "Content-Type: application/json" -X PUT -d '{ "data": {"name": "NombreModificado"} }' localhost:8001/entities/teacher/1
127+
"""
105128

106-
return response
129+
url = 'http://' + modules.get_hostname(module='dbms') + '/entities/' + str(kind) + '/' + str(entity_id)
130+
return ping(url=url, payload=request, method='PUT')
107131

108132

109133
@app.route('/entities/<string:kind>/<int:entity_id>', methods=['DELETE'])
@@ -112,33 +136,26 @@ def delete_entity(kind, entity_id):
112136
curl -i -X DELETE localhost:8002/entities/subject/1
113137
"""
114138

115-
url = "http://%s/" % modules.get_hostname(module='dbms')
116-
url += 'entities/' + str(kind) + '/' + str(entity_id)
117-
118-
req = urllib2.Request(url)
119-
req.get_method = lambda: "DELETE"
120-
f = urllib2.urlopen(req)
121-
response = f.read()
122-
f.close()
139+
url = 'http://' + modules.get_hostname(module='dbms') + '/entities/' + str(kind) + '/' + str(entity_id)
123140

124-
return response
141+
return ping(url=url, method='DELETE')
125142

126143

127144
@app.route('/entities/<string:kind>/<int:entity_id>/<string:related_kind>', methods=['GET'])
128145
def get_related_entities(kind, entity_id, related_kind):
129-
# """
130-
# curl -i -X GET localhost:8001/entities/student/1/teacher
131-
# ""
146+
"""
132147
133-
url = "http://%s/" % modules.get_hostname(module='dbms')
134-
url += 'entities/' + str(kind) + '/' + str(entity_id) + '/' + str(related_kind)
148+
:param kind:
149+
:param entity_id:
150+
:param related_kind:
151+
:return:
135152
136-
req = urllib2.Request(url)
137-
f = urllib2.urlopen(req)
138-
response = f.read()
139-
f.close()
153+
Example of use:
154+
curl -i -X GET localhost:8001/entities/student/1/teacher
155+
"""
140156

141-
return response
157+
url = 'http://' + modules.get_hostname(module='dbms') + '/entities/' + str(kind) + '/' + str(entity_id) + '/' + str(related_kind)
158+
return ping(url=url)
142159

143160

144161
if __name__ == '__main__':

SMS-Back-End/dbms/dbms_api.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def test():
122122
# Resources about entities #
123123
#################################
124124

125+
125126
@app.route('/entities/<string:kind>', methods=['POST'])
126127
def put_entity(kind):
127128
"""
@@ -141,8 +142,8 @@ def put_entity(kind):
141142
A json with the entire entity which is saved in database (with all extra control values) or error status code.
142143
143144
Example of use:
144-
curl -H "Content-Type: application/json" -X POST -d '{ "data": {"name": "María"} }' localhost:8080/entities/student
145-
curl -H "Content-Type: application/json" -X POST -d '{"data": {"course": 1, "word": "B", "level": "ESO"} }' localhost:8080/entities/class
145+
curl -H "Content-Type: application/json" -X POST -d '{ "data": {"name": "María"} }' localhost:8002/entities/student
146+
curl -H "Content-Type: application/json" -X POST -d '{"data": {"course": 1, "word": "B", "level": "ESO"} }' localhost:8002/entities/class
146147
147148
Example of return:
148149
{"createdBy": 1, "course": 5, "createdAt": "Thu Sep 22 16:09:36 2016", "word": "B", "level": "Primary", "classId": 19}

0 commit comments

Comments
 (0)