|
1 | 1 | import requests |
2 | | -import datetime |
3 | | -import json |
4 | 2 | from flask_restful import Resource, abort |
5 | | -from flask import request, make_response, jsonify |
6 | | -from pbench.server.database.models.metadata import MetadataModel |
7 | | -from pbench.server.database.database import Database |
8 | | -from pbench.server.api.auth import Auth |
9 | | - |
10 | | - |
11 | | -class UserMetadata(Resource): |
12 | | - """ |
13 | | - Abstracted pbench API for handling user metadata by using graphql schema |
14 | | - """ |
15 | | - |
16 | | - def __init__(self, config, logger, auth): |
17 | | - self.server_config = config |
18 | | - self.logger = logger |
19 | | - self.auth = auth |
20 | | - |
21 | | - @Auth.token_auth.login_required(f=Auth().verify_auth()) |
22 | | - def post(self): |
23 | | - """ |
24 | | - Post request for creating metadata instance for a user. |
25 | | - This requires a JWT auth token in the header field |
26 | | -
|
27 | | - This requires a JSON data with required user metadata fields |
28 | | - { |
29 | | - "config": "config", |
30 | | - "description": "description", |
31 | | - } |
32 | | -
|
33 | | - Required headers include |
34 | | - Authorization: JWT token (user received upon login) |
35 | | -
|
36 | | - :return: JSON Payload |
37 | | - response_object = { |
38 | | - "status": "success" |
39 | | - "data" { |
40 | | - "id": "metadata_id", |
41 | | - "config": "Config string" |
42 | | - "description": "Description string" |
43 | | - } |
44 | | - } |
45 | | - """ |
46 | | - post_data = request.get_json() |
47 | | - if not post_data: |
48 | | - self.logger.warning("Invalid json object: %s", request.url) |
49 | | - abort(400, message="Invalid json object in request") |
50 | | - |
51 | | - config = post_data.get("config") |
52 | | - if not config: |
53 | | - self.logger.warning("Config not provided during metadata creation") |
54 | | - abort(400, message="Please provide a config string") |
55 | | - |
56 | | - description = post_data.get("description") |
57 | | - if not description: |
58 | | - self.logger.warning("Description not provided during metadata creation") |
59 | | - abort(400, message="Please provide a description string") |
60 | | - current_user_id = self.auth.token_auth.current_user().id |
61 | | - |
62 | | - try: |
63 | | - # Create a new metadata session |
64 | | - metadata_session = MetadataModel( |
65 | | - created=str(datetime.datetime.now()), |
66 | | - config=config, |
67 | | - description=description, |
68 | | - user_id=current_user_id |
69 | | - ) |
70 | | - # insert the metadata session for a user |
71 | | - Database.db_session.add(metadata_session) |
72 | | - Database.db_session.commit() |
73 | | - self.logger.info("New user metadata session created") |
74 | | - except Exception: |
75 | | - self.logger.exception("Exception occurred during Metadata creation") |
76 | | - abort(500, message="INTERNAL ERROR") |
77 | | - else: |
78 | | - response_object = { |
79 | | - "status": "success", |
80 | | - "data": { |
81 | | - "id": metadata_session.id, |
82 | | - "config": metadata_session.config, |
83 | | - "description": metadata_session.description, |
84 | | - }, |
85 | | - } |
86 | | - return make_response(jsonify(response_object), 201) |
87 | | - |
88 | | - @Auth.token_auth.login_required(f=Auth().verify_auth()) |
89 | | - def get(self): |
90 | | - """ |
91 | | - Get request for querying all the metadata sessions for a user. |
92 | | - This requires a JWT auth token in the header field |
93 | | -
|
94 | | - Required headers include |
95 | | - Authorization: JWT token (user received upon login) |
96 | | -
|
97 | | - :return: JSON Payload |
98 | | - response_object = { |
99 | | - "status": "success" |
100 | | - "data": { |
101 | | - "sessions": [ |
102 | | - {"id": "metadata_id", |
103 | | - "config": "Config string" |
104 | | - "description": "Description string"}, |
105 | | - {}] |
106 | | - } |
107 | | - } |
108 | | - """ |
109 | | - current_user_id = self.auth.token_auth.current_user().id |
110 | | - try: |
111 | | - # Fetch the metadata session |
112 | | - sessions = ( |
113 | | - Database.db_session.query(MetadataModel) |
114 | | - .filter_by(user_id=current_user_id) |
115 | | - .all() |
116 | | - ) |
117 | | - |
118 | | - req_keys = ["id", "config", "description", "created"] |
119 | | - data = json.dumps([{key: session.as_dict()[key] for key in req_keys} for session in sessions]) |
120 | | - except Exception: |
121 | | - self.logger.exception("Exception occurred during querying Metadata model") |
122 | | - abort(500, message="INTERNAL ERROR") |
123 | | - |
124 | | - response_object = { |
125 | | - "status": "success", |
126 | | - "data": { |
127 | | - "sessions": data |
128 | | - }, |
129 | | - } |
130 | | - return make_response(jsonify(response_object), 200) |
131 | | - |
132 | | - |
133 | | -class QueryMetadata(Resource): |
134 | | - """ |
135 | | - Abstracted pbench API for querying a single user metadata session |
136 | | - """ |
137 | | - |
138 | | - def __init__(self, config, logger): |
139 | | - self.server_config = config |
140 | | - self.logger = logger |
141 | | - |
142 | | - @Auth.token_auth.login_required(f=Auth().verify_auth()) |
143 | | - def get(self, id=None): |
144 | | - """ |
145 | | - Get request for querying a metadata session for a user given a metadata id. |
146 | | - This requires a JWT auth token in the header field |
147 | | -
|
148 | | - This requires a JSON data with required user metadata fields to update |
149 | | - { |
150 | | - "description": "description", |
151 | | - } |
152 | | -
|
153 | | - The url requires a metadata session id such as /user/metadata/<string:id> |
154 | | -
|
155 | | - Required headers include |
156 | | - Authorization: JWT token (user received upon login) |
157 | | -
|
158 | | - :return: JSON Payload |
159 | | - response_object = { |
160 | | - "status": "success" |
161 | | - "data" { |
162 | | - "id": "metadata_id", |
163 | | - "config": "Config string" |
164 | | - "description": "Description string" |
165 | | - } |
166 | | - } |
167 | | - """ |
168 | | - if not id: |
169 | | - self.logger.warning("Meatadata id not provided during metadata query") |
170 | | - abort(400, message="Please provide a metadata id to query") |
171 | | - |
172 | | - try: |
173 | | - # Fetch the metadata session |
174 | | - session = ( |
175 | | - Database.db_session.query(MetadataModel) |
176 | | - .filter_by(id=id) |
177 | | - .first() |
178 | | - ) |
179 | | - except Exception: |
180 | | - self.logger.exception("Exception occurred during querying Metadata model") |
181 | | - abort(500, message="INTERNAL ERROR") |
182 | | - else: |
183 | | - response_object = { |
184 | | - "status": "success", |
185 | | - "data": { |
186 | | - "id": session.id, |
187 | | - "config": session.config, |
188 | | - "description": session.description, |
189 | | - }, |
190 | | - } |
191 | | - return make_response(jsonify(response_object), 200) |
192 | | - |
193 | | - @Auth.token_auth.login_required(f=Auth().verify_auth()) |
194 | | - def put(self, id=None): |
195 | | - """ |
196 | | - Put request for updating a metadata session for a user given a metadata id. |
197 | | - This requires a JWT auth token in the header field |
198 | | -
|
199 | | - The url requires a metadata session id such as /user/metadata/<string:id> |
200 | | -
|
201 | | - Required headers include |
202 | | - Authorization: JWT token (user received upon login) |
203 | | -
|
204 | | - :return: JSON Payload |
205 | | - response_object = { |
206 | | - "status": "success" |
207 | | - "data" { |
208 | | - "id": "metadata_id", |
209 | | - "config": "Config string" |
210 | | - "description": "Description string" |
211 | | - } |
212 | | - } |
213 | | - """ |
214 | | - if not id: |
215 | | - self.logger.warning("Meatadata id not provided during metadata query") |
216 | | - abort(400, message="Please provide a metadata id to query") |
217 | | - |
218 | | - post_data = request.get_json() |
219 | | - if not post_data: |
220 | | - self.logger.warning("Invalid json object: %s", request.url) |
221 | | - abort(400, message="Invalid json object in request") |
222 | | - |
223 | | - description = post_data.get("description") |
224 | | - if not description: |
225 | | - self.logger.warning("Description not provided during metadata update") |
226 | | - abort(400, message="Please provide a description string") |
227 | | - |
228 | | - try: |
229 | | - # Fetch the metadata session |
230 | | - session = ( |
231 | | - Database.db_session.query(MetadataModel) |
232 | | - .filter_by(id=id) |
233 | | - .first() |
234 | | - ) |
235 | | - session.description = description |
236 | | - # Update the metadata session for a user |
237 | | - Database.db_session.add(session) |
238 | | - Database.db_session.commit() |
239 | | - self.logger.info("User metadata session updated") |
240 | | - except Exception: |
241 | | - self.logger.exception("Exception occurred during querying Metadata model") |
242 | | - abort(500, message="INTERNAL ERROR") |
243 | | - else: |
244 | | - response_object = { |
245 | | - "status": "success", |
246 | | - "data": { |
247 | | - "id": session.id, |
248 | | - "config": session.config, |
249 | | - "description": session.description, |
250 | | - }, |
251 | | - } |
252 | | - return make_response(jsonify(response_object), 200) |
| 3 | +from flask import request, make_response |
253 | 4 |
|
254 | 5 |
|
255 | 6 | class GraphQL(Resource): |
|
0 commit comments