Extracted HTTP response codes to constants

There are several places in the source code where
HTTP response codes are used as numeric values.
These values are used from six.moves and the
numeric values are replaced by constants.

All of the used status codes were replaced with symbolic constants
from six.moves.http_client. More about six.moves.http_client can be
found at [2], under the table "Supported renames:".

Also, this change  improves code readibility.

This patchset does not extract numeric values
from the tests, but it can be found at [1].

[1]: Idfc7b043552f428f01ac3e47b270ee0639a8f5bc
[2]: https://pythonhosted.org/six/#module-six.moves

Change-Id: Ib9e26dcea927e96e65c626c18421621d3a29a64d
Partial-Bug: #1520159
This commit is contained in:
Gábor Antal 2016-01-27 19:48:57 +01:00
parent 9bd264cd03
commit 8a63622348
13 changed files with 62 additions and 50 deletions

View File

@ -26,6 +26,7 @@ import re
import six
from oslo_log import log as logging
from six.moves import http_client as http
import webob
from glance.api.common import size_checked_iter
@ -260,7 +261,7 @@ class CacheFilter(wsgi.Middleware):
except TypeError:
return resp
if method == 'GET' and status_code == 204:
if method == 'GET' and status_code == http.NO_CONTENT:
# Bugfix:1251055 - Don't cache non-existent image files.
# NOTE: Both GET for an image without locations and DELETE return
# 204 but DELETE should be processed.

View File

@ -14,6 +14,7 @@
# under the License.
import glance_store
from oslo_log import log as logging
from six.moves import http_client as http
import webob.exc
from glance.api import policy
@ -81,10 +82,10 @@ class ImageActionsController(object):
class ResponseSerializer(wsgi.JSONResponseSerializer):
def deactivate(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def reactivate(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def create_resource():

View File

@ -20,6 +20,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import webob
from glance.api import policy
@ -234,7 +235,7 @@ class ImageMembersController(object):
member = self._lookup_member(req, image, member_id)
try:
member_repo.remove(member)
return webob.Response(body='', status=204)
return webob.Response(body='', status=http.NO_CONTENT)
except exception.Forbidden:
msg = _("Not allowed to delete members for image %s.") % image_id
LOG.warning(msg)

View File

@ -15,6 +15,7 @@
import glance_store
from oslo_log import log as logging
from oslo_utils import encodeutils
from six.moves import http_client as http
import webob.exc
from glance.api import policy
@ -89,10 +90,10 @@ class Controller(object):
class ResponseSerializer(wsgi.JSONResponseSerializer):
def update(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
class RequestDeserializer(wsgi.JSONRequestDeserializer):

View File

@ -21,6 +21,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils as json
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import six.moves.urllib.parse as urlparse
import webob.exc
@ -795,7 +796,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
raise webob.exc.HTTPForbidden(explanation=e.msg)
def create(self, response, image):
response.status_int = 201
response.status_int = http.CREATED
self.show(response, image)
response.location = self._get_image_href(image)
@ -831,7 +832,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.content_type = 'application/json'
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def get_base_properties():

View File

@ -18,6 +18,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import six.moves.urllib.parse as urlparse
import webob.exc
from wsme.rest import json
@ -510,7 +511,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
def create(self, response, namespace):
ns_json = json.tojson(Namespace, namespace)
response = self.__render(ns_json, response, 201)
response = self.__render(ns_json, response, http.CREATED)
response.location = get_namespace_href(namespace)
def show(self, response, namespace):
@ -535,16 +536,16 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
def update(self, response, namespace):
ns_json = json.tojson(Namespace, namespace)
response = self.__render(ns_json, response, 200)
response = self.__render(ns_json, response, http.OK)
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def delete_objects(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def delete_properties(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def __render(self, json_data, response, response_status=None):
body = jsonutils.dumps(json_data, ensure_ascii=False)

View File

@ -17,6 +17,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import webob.exc
from wsme.rest import json
@ -327,7 +328,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
self.schema = schema or get_schema()
def create(self, response, metadata_object):
response.status_int = 201
response.status_int = http.CREATED
self.show(response, metadata_object)
def show(self, response, metadata_object):
@ -337,7 +338,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.content_type = 'application/json'
def update(self, response, metadata_object):
response.status_int = 200
response.status_int = http.OK
self.show(response, metadata_object)
def index(self, response, result):
@ -348,7 +349,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.content_type = 'application/json'
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def get_object_href(namespace_name, metadef_object):

View File

@ -17,6 +17,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import webob.exc
from wsme.rest import json
@ -245,15 +246,15 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.content_type = 'application/json'
def create(self, response, result):
response.status_int = 201
response.status_int = http.CREATED
self.show(response, result)
def update(self, response, result):
response.status_int = 200
response.status_int = http.OK
self.show(response, result)
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def _get_base_definitions():

View File

@ -17,6 +17,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import webob.exc
from wsme.rest import json
@ -188,13 +189,13 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
def create(self, response, result):
resource_type_json = json.tojson(ResourceTypeAssociation, result)
response.status_int = 201
response.status_int = http.CREATED
body = jsonutils.dumps(resource_type_json, ensure_ascii=False)
response.unicode_body = six.text_type(body)
response.content_type = 'application/json'
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def _get_base_properties():

View File

@ -18,6 +18,7 @@ from oslo_log import log as logging
from oslo_serialization import jsonutils
from oslo_utils import encodeutils
import six
from six.moves import http_client as http
import webob.exc
from wsme.rest import json
@ -368,11 +369,11 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
self.schema = schema or get_schema()
def create(self, response, metadata_tag):
response.status_int = 201
response.status_int = http.CREATED
self.show(response, metadata_tag)
def create_tags(self, response, result):
response.status_int = 201
response.status_int = http.CREATED
metadata_tags_json = json.tojson(MetadefTags, result)
body = jsonutils.dumps(metadata_tags_json, ensure_ascii=False)
response.unicode_body = six.text_type(body)
@ -385,7 +386,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.content_type = 'application/json'
def update(self, response, metadata_tag):
response.status_int = 200
response.status_int = http.OK
self.show(response, metadata_tag)
def index(self, response, result):
@ -395,7 +396,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
response.content_type = 'application/json'
def delete(self, response, result):
response.status_int = 204
response.status_int = http.NO_CONTENT
def get_tag_href(namespace_name, metadef_tag):

View File

@ -24,6 +24,7 @@ import oslo_serialization.jsonutils as json
from oslo_utils import encodeutils
from oslo_utils import uuidutils
import six
from six.moves import http_client as http
import six.moves.urllib.parse as urlparse
import webob.exc
@ -289,7 +290,7 @@ class ResponseSerializer(wsgi.JSONResponseSerializer):
return task_view
def create(self, response, task):
response.status_int = 201
response.status_int = http.CREATED
self._inject_location_header(response, task)
self.get(response, task)

View File

@ -27,7 +27,7 @@ from oslo_serialization import jsonutils
from oslo_utils import encodeutils
from oslo_utils import uuidutils
import six
from six.moves import http_client
from six.moves import http_client as http
import six.moves.urllib.parse as urlparse
from webob import exc
@ -147,29 +147,29 @@ class ImageService(object):
response = self.conn.getresponse()
headers = self._header_list_to_dict(response.getheaders())
code = response.status
code_description = http_client.responses[code]
code_description = http.responses[code]
LOG.debug('Response: %(code)s %(status)s %(headers)s',
{'code': code,
'status': code_description,
'headers': repr(headers)})
if code == 400:
if code == http.BAD_REQUEST:
raise exc.HTTPBadRequest(
explanation=response.read())
if code == 500:
if code == http.INTERNAL_SERVER_ERROR:
raise exc.HTTPInternalServerError(
explanation=response.read())
if code == 401:
if code == http.UNAUTHORIZED:
raise exc.HTTPUnauthorized(
explanation=response.read())
if code == 403:
if code == http.FORBIDDEN:
raise exc.HTTPForbidden(
explanation=response.read())
if code == 409:
if code == http.CONFLICT:
raise exc.HTTPConflict(
explanation=response.read())
@ -339,7 +339,7 @@ def replication_size(options, args):
count = 0
imageservice = get_image_service()
client = imageservice(http_client.HTTPConnection(server, port),
client = imageservice(http.HTTPConnection(server, port),
options.slavetoken)
for image in client.get_images():
LOG.debug('Considering image: %(image)s', {'image': image})
@ -371,7 +371,7 @@ def replication_dump(options, args):
server, port = utils.parse_valid_host_port(args.pop())
imageservice = get_image_service()
client = imageservice(http_client.HTTPConnection(server, port),
client = imageservice(http.HTTPConnection(server, port),
options.mastertoken)
for image in client.get_images():
LOG.debug('Considering: %(image_id)s (%(image_name)s) '
@ -457,7 +457,7 @@ def replication_load(options, args):
server, port = utils.parse_valid_host_port(args.pop())
imageservice = get_image_service()
client = imageservice(http_client.HTTPConnection(server, port),
client = imageservice(http.HTTPConnection(server, port),
options.slavetoken)
updated = []
@ -531,11 +531,11 @@ def replication_livecopy(options, args):
imageservice = get_image_service()
slave_server, slave_port = utils.parse_valid_host_port(args.pop())
slave_conn = http_client.HTTPConnection(slave_server, slave_port)
slave_conn = http.HTTPConnection(slave_server, slave_port)
slave_client = imageservice(slave_conn, options.slavetoken)
master_server, master_port = utils.parse_valid_host_port(args.pop())
master_conn = http_client.HTTPConnection(master_server, master_port)
master_conn = http.HTTPConnection(master_server, master_port)
master_client = imageservice(master_conn, options.mastertoken)
updated = []
@ -609,11 +609,11 @@ def replication_compare(options, args):
imageservice = get_image_service()
slave_server, slave_port = utils.parse_valid_host_port(args.pop())
slave_conn = http_client.HTTPConnection(slave_server, slave_port)
slave_conn = http.HTTPConnection(slave_server, slave_port)
slave_client = imageservice(slave_conn, options.slavetoken)
master_server, master_port = utils.parse_valid_host_port(args.pop())
master_conn = http_client.HTTPConnection(master_server, master_port)
master_conn = http.HTTPConnection(master_server, master_port)
master_client = imageservice(master_conn, options.mastertoken)
differences = {}

View File

@ -31,6 +31,7 @@ Keystone (an identity management system).
import httplib2
from keystoneclient import service_catalog as ks_service_catalog
from oslo_serialization import jsonutils
from six.moves import http_client as http
# NOTE(jokke): simplified transition to py3, behaves like py2 xrange
from six.moves import range
import six.moves.urllib.parse as urlparse
@ -167,20 +168,20 @@ class KeystoneStrategy(BaseStrategy):
not_found = e
raise not_found
if resp.status in (200, 204):
if resp.status in (http.OK, http.NO_CONTENT):
try:
if self.configure_via_auth:
self.management_url = _management_url(self, resp)
self.auth_token = resp['x-auth-token']
except KeyError:
raise exception.AuthorizationFailure()
elif resp.status == 305:
elif resp.status == http.USE_PROXY:
raise exception.AuthorizationRedirect(uri=resp['location'])
elif resp.status == 400:
elif resp.status == http.BAD_REQUEST:
raise exception.AuthBadRequest(url=token_url)
elif resp.status == 401:
elif resp.status == http.UNAUTHORIZED:
raise exception.NotAuthenticated()
elif resp.status == 404:
elif resp.status == http.NOT_FOUND:
raise exception.AuthUrlNotFound(url=token_url)
else:
raise Exception(_('Unexpected response: %s') % resp.status)
@ -205,7 +206,7 @@ class KeystoneStrategy(BaseStrategy):
resp, resp_body = self._do_request(
token_url, 'POST', headers=headers, body=req_body)
if resp.status == 200:
if resp.status == http.OK:
resp_auth = jsonutils.loads(resp_body)['access']
creds_region = self.creds.get('region')
if self.configure_via_auth:
@ -213,13 +214,13 @@ class KeystoneStrategy(BaseStrategy):
endpoint_region=creds_region)
self.management_url = endpoint
self.auth_token = resp_auth['token']['id']
elif resp.status == 305:
elif resp.status == http.USE_PROXY:
raise exception.RedirectException(resp['location'])
elif resp.status == 400:
elif resp.status == http.BAD_REQUEST:
raise exception.AuthBadRequest(url=token_url)
elif resp.status == 401:
elif resp.status == http.UNAUTHORIZED:
raise exception.NotAuthenticated()
elif resp.status == 404:
elif resp.status == http.NOT_FOUND:
raise exception.AuthUrlNotFound(url=token_url)
else:
raise Exception(_('Unexpected response: %s') % resp.status)