11
11
# Activating verbose mode
12
12
v = 1
13
13
14
+ #DEFAULT PORT TO THIS microService in local: 8001
15
+
14
16
microservice_name = '\n ## dbms Data Base mService ##'
15
17
16
18
module = modules .get_current_module_name ()
17
19
instance = modules .get_current_instance_id ()
18
20
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
+
20
50
@app .route ('/test' ,methods = ['GET' ])
21
51
def test ():
22
52
"""
23
- Test resource.
53
+ Test resource.
24
54
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
27
59
"""
28
60
29
61
url = "http://%s/" % modules .get_hostname (module = 'dbms' )
30
62
url += 'test'
63
+
31
64
req = urllib2 .Request (url )
32
65
f = urllib2 .urlopen (req )
33
66
response = f .read ()
@@ -38,72 +71,63 @@ def test():
38
71
39
72
@app .route ('/entities/<string:kind>' , methods = ['POST' ])
40
73
def put_entity (kind ):
74
+ """
75
+ Data Base micro Service Resource connector, put all kind of entities in this mService.
41
76
42
- url = "http://%s/" % modules .get_hostname (module = 'dbms' )
43
- url += 'entities/' + str (kind )
77
+ :param kind:
78
+ :payload json:
79
+ :return:
44
80
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
49
83
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' )
51
89
52
90
53
91
@app .route ('/entities/<string:kind>' , methods = ['GET' ])
54
92
@app .route ('/entities/<string:kind>/<int:entity_id>' , methods = ['GET' ])
55
93
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.
65
96
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.
69
100
70
- print type (response )
71
- response = json .dumps (response )
72
- print type (response )
101
+ Example of use:
73
102
74
- f . close ()
103
+ curl -i -X GET localhost:8001/entities/teacher
75
104
105
+ See more info in dbms_apy.py
76
106
"""
77
- res = Response(json.dumps(response), mimetype='application/json')
78
107
79
- rews = make_response(res )
108
+ url = 'http://' + str ( modules . get_hostname ( module = 'dbms' )) + '/' + 'entities/' + str ( kind )
80
109
81
- rews.headers['Access-Control-Allow-Origin'] = "*"
82
- """
83
-
84
- return response
110
+ if entity_id is not None :
111
+ url += '/' + str (entity_id )
85
112
86
- #return Response(json.dumps(list), mimetype='application/json')
87
- #return jsonify(r)
88
- #return response
113
+ return ping (url )
89
114
90
115
91
116
@app .route ('/entities/<string:kind>/<int:entity_id>' , methods = ['PUT' ])
92
117
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.
97
120
98
- print url
121
+ :param kind:
122
+ :param entity_id:
123
+ :return:
99
124
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
+ """
105
128
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' )
107
131
108
132
109
133
@app .route ('/entities/<string:kind>/<int:entity_id>' , methods = ['DELETE' ])
@@ -112,33 +136,26 @@ def delete_entity(kind, entity_id):
112
136
curl -i -X DELETE localhost:8002/entities/subject/1
113
137
"""
114
138
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 )
123
140
124
- return response
141
+ return ping ( url = url , method = 'DELETE' )
125
142
126
143
127
144
@app .route ('/entities/<string:kind>/<int:entity_id>/<string:related_kind>' , methods = ['GET' ])
128
145
def get_related_entities (kind , entity_id , related_kind ):
129
- # """
130
- # curl -i -X GET localhost:8001/entities/student/1/teacher
131
- # ""
146
+ """
132
147
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:
135
152
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
+ """
140
156
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 )
142
159
143
160
144
161
if __name__ == '__main__' :
0 commit comments