From 87393f4eba3b68f9598c02dcbe1f06f2afdd400c Mon Sep 17 00:00:00 2001 From: SerenaFeng Date: Thu, 30 Mar 2017 14:01:30 +0800 Subject: replace self-defined http codes with standard definitions Change-Id: I3045dc690e0bc1186f5c548cb533462dd03130d9 Signed-off-by: SerenaFeng --- testapi/opnfv_testapi/resources/handlers.py | 30 ++++++++++++---------- testapi/opnfv_testapi/resources/pod_handlers.py | 5 ++-- .../opnfv_testapi/resources/project_handlers.py | 5 ++-- testapi/opnfv_testapi/resources/result_handlers.py | 10 ++++---- .../opnfv_testapi/resources/scenario_handlers.py | 6 ++--- .../opnfv_testapi/resources/testcase_handlers.py | 7 ++--- 6 files changed, 34 insertions(+), 29 deletions(-) (limited to 'testapi/opnfv_testapi/resources') diff --git a/testapi/opnfv_testapi/resources/handlers.py b/testapi/opnfv_testapi/resources/handlers.py index 1509646..bf8a92b 100644 --- a/testapi/opnfv_testapi/resources/handlers.py +++ b/testapi/opnfv_testapi/resources/handlers.py @@ -22,15 +22,17 @@ from datetime import datetime import functools +import httplib import json from tornado import gen from tornado import web import models -from opnfv_testapi.common import constants from opnfv_testapi.tornado_swagger import swagger +DEFAULT_REPRESENTATION = "application/json" + class GenericApiHandler(web.RequestHandler): def __init__(self, application, request, **kwargs): @@ -50,18 +52,18 @@ class GenericApiHandler(web.RequestHandler): if self.request.method != "GET" and self.request.method != "DELETE": if self.request.headers.get("Content-Type") is not None: if self.request.headers["Content-Type"].startswith( - constants.DEFAULT_REPRESENTATION): + DEFAULT_REPRESENTATION): try: self.json_args = json.loads(self.request.body) except (ValueError, KeyError, TypeError) as error: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, + raise web.HTTPError(httplib.BAD_REQUEST, "Bad Json format [{}]". format(error)) def finish_request(self, json_object=None): if json_object: self.write(json.dumps(json_object)) - self.set_header("Content-Type", constants.DEFAULT_REPRESENTATION) + self.set_header("Content-Type", DEFAULT_REPRESENTATION) self.finish() def _create_response(self, resource): @@ -81,12 +83,12 @@ class GenericApiHandler(web.RequestHandler): try: token = self.request.headers['X-Auth-Token'] except KeyError: - raise web.HTTPError(constants.HTTP_UNAUTHORIZED, + raise web.HTTPError(httplib.UNAUTHORIZED, "No Authentication Header.") query = {'access_token': token} check = yield self._eval_db_find_one(query, 'tokens') if not check: - raise web.HTTPError(constants.HTTP_FORBIDDEN, + raise web.HTTPError(httplib.FORBIDDEN, "Invalid Token.") ret = yield gen.coroutine(method)(self, *args, **kwargs) raise gen.Return(ret) @@ -99,13 +101,13 @@ class GenericApiHandler(web.RequestHandler): :param db_checks: [(table, exist, query, error)] """ if self.json_args is None: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, "no body") + raise web.HTTPError(httplib.BAD_REQUEST, "no body") data = self.table_cls.from_dict(self.json_args) for miss in miss_checks: miss_data = data.__getattribute__(miss) if miss_data is None or miss_data == '': - raise web.HTTPError(constants.HTTP_BAD_REQUEST, + raise web.HTTPError(httplib.BAD_REQUEST, '{} missing'.format(miss)) for k, v in kwargs.iteritems(): @@ -151,7 +153,7 @@ class GenericApiHandler(web.RequestHandler): def _get_one(self, query): data = yield self._eval_db_find_one(query) if data is None: - raise web.HTTPError(constants.HTTP_NOT_FOUND, + raise web.HTTPError(httplib.NOT_FOUND, "[{}] not exist in table [{}]" .format(query, self.table)) self.finish_request(self.format_data(data)) @@ -160,7 +162,7 @@ class GenericApiHandler(web.RequestHandler): def _delete(self, query): data = yield self._eval_db_find_one(query) if data is None: - raise web.HTTPError(constants.HTTP_NOT_FOUND, + raise web.HTTPError(httplib.NOT_FOUND, "[{}] not exit in table [{}]" .format(query, self.table)) @@ -170,12 +172,12 @@ class GenericApiHandler(web.RequestHandler): @authenticate def _update(self, query, db_keys): if self.json_args is None: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, "No payload") + raise web.HTTPError(httplib.BAD_REQUEST, "No payload") # check old data exist from_data = yield self._eval_db_find_one(query) if from_data is None: - raise web.HTTPError(constants.HTTP_NOT_FOUND, + raise web.HTTPError(httplib.NOT_FOUND, "{} could not be found in table [{}]" .format(query, self.table)) @@ -185,7 +187,7 @@ class GenericApiHandler(web.RequestHandler): if not equal: to_data = yield self._eval_db_find_one(new_query) if to_data is not None: - raise web.HTTPError(constants.HTTP_FORBIDDEN, + raise web.HTTPError(httplib.FORBIDDEN, "{} already exists in table [{}]" .format(new_query, self.table)) @@ -204,7 +206,7 @@ class GenericApiHandler(web.RequestHandler): request = self._update_request(request, k, v, data.__getattribute__(k)) if not request: - raise web.HTTPError(constants.HTTP_FORBIDDEN, "Nothing to update") + raise web.HTTPError(httplib.FORBIDDEN, "Nothing to update") edit_request = data.format() edit_request.update(request) diff --git a/testapi/opnfv_testapi/resources/pod_handlers.py b/testapi/opnfv_testapi/resources/pod_handlers.py index 65c27f6..fd9ce3e 100644 --- a/testapi/opnfv_testapi/resources/pod_handlers.py +++ b/testapi/opnfv_testapi/resources/pod_handlers.py @@ -6,8 +6,9 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import httplib + import handlers -from opnfv_testapi.common import constants from opnfv_testapi.tornado_swagger import swagger import pod_models @@ -46,7 +47,7 @@ class PodCLHandler(GenericPodHandler): def error(data): message = '{} already exists as a pod'.format(data.name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.table, False, query, error)] diff --git a/testapi/opnfv_testapi/resources/project_handlers.py b/testapi/opnfv_testapi/resources/project_handlers.py index f352196..087bb8a 100644 --- a/testapi/opnfv_testapi/resources/project_handlers.py +++ b/testapi/opnfv_testapi/resources/project_handlers.py @@ -6,8 +6,9 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## +import httplib + import handlers -from opnfv_testapi.common import constants from opnfv_testapi.tornado_swagger import swagger import project_models @@ -48,7 +49,7 @@ class ProjectCLHandler(GenericProjectHandler): def error(data): message = '{} already exists as a project'.format(data.name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.table, False, query, error)] diff --git a/testapi/opnfv_testapi/resources/result_handlers.py b/testapi/opnfv_testapi/resources/result_handlers.py index d41ba48..44b9f8c 100644 --- a/testapi/opnfv_testapi/resources/result_handlers.py +++ b/testapi/opnfv_testapi/resources/result_handlers.py @@ -8,11 +8,11 @@ ############################################################################## from datetime import datetime from datetime import timedelta +import httplib from bson import objectid from tornado import web -from opnfv_testapi.common import constants from opnfv_testapi.resources import handlers from opnfv_testapi.resources import result_models from opnfv_testapi.tornado_swagger import swagger @@ -30,7 +30,7 @@ class GenericResultHandler(handlers.GenericApiHandler): try: value = int(value) except: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, + raise web.HTTPError(httplib.BAD_REQUEST, '{} must be int'.format(key)) return value @@ -146,14 +146,14 @@ class ResultsCLHandler(GenericResultHandler): def pod_error(data): message = 'Could not find pod [{}]'.format(data.pod_name) - return constants.HTTP_NOT_FOUND, message + return httplib.NOT_FOUND, message def project_query(data): return {'name': data.project_name} def project_error(data): message = 'Could not find project [{}]'.format(data.project_name) - return constants.HTTP_NOT_FOUND, message + return httplib.NOT_FOUND, message def testcase_query(data): return {'project_name': data.project_name, 'name': data.case_name} @@ -161,7 +161,7 @@ class ResultsCLHandler(GenericResultHandler): def testcase_error(data): message = 'Could not find testcase [{}] in project [{}]'\ .format(data.case_name, data.project_name) - return constants.HTTP_NOT_FOUND, message + return httplib.NOT_FOUND, message miss_checks = ['pod_name', 'project_name', 'case_name'] db_checks = [('pods', True, pod_query, pod_error), diff --git a/testapi/opnfv_testapi/resources/scenario_handlers.py b/testapi/opnfv_testapi/resources/scenario_handlers.py index 80eb1aa..a2856db 100644 --- a/testapi/opnfv_testapi/resources/scenario_handlers.py +++ b/testapi/opnfv_testapi/resources/scenario_handlers.py @@ -1,8 +1,8 @@ import functools +import httplib from tornado import web -from opnfv_testapi.common import constants from opnfv_testapi.resources import handlers import opnfv_testapi.resources.scenario_models as models from opnfv_testapi.tornado_swagger import swagger @@ -84,7 +84,7 @@ class ScenariosCLHandler(GenericScenarioHandler): def error(data): message = '{} already exists as a scenario'.format(data.name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.table, False, query, error)] @@ -185,7 +185,7 @@ class ScenarioGURHandler(GenericScenarioHandler): def _update_requests_rename(self, data): data.name = self._term.get('name') if not data.name: - raise web.HTTPError(constants.HTTP_BAD_REQUEST, + raise web.HTTPError(httplib.BAD_REQUEST, "new scenario name is not provided") def _update_requests_add_installer(self, data): diff --git a/testapi/opnfv_testapi/resources/testcase_handlers.py b/testapi/opnfv_testapi/resources/testcase_handlers.py index 3debd69..1211a05 100644 --- a/testapi/opnfv_testapi/resources/testcase_handlers.py +++ b/testapi/opnfv_testapi/resources/testcase_handlers.py @@ -6,7 +6,8 @@ # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -from opnfv_testapi.common import constants +import httplib + from opnfv_testapi.resources import handlers from opnfv_testapi.resources import testcase_models from opnfv_testapi.tornado_swagger import swagger @@ -58,12 +59,12 @@ class TestcaseCLHandler(GenericTestcaseHandler): def p_error(data): message = 'Could not find project [{}]'.format(data.project_name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message def tc_error(data): message = '{} already exists as a testcase in project {}'\ .format(data.name, data.project_name) - return constants.HTTP_FORBIDDEN, message + return httplib.FORBIDDEN, message miss_checks = ['name'] db_checks = [(self.db_projects, True, p_query, p_error), -- cgit 1.2.3-korg