Skip to content

Commit

Permalink
Fix curl command for PUT and DELETE.
Browse files Browse the repository at this point in the history
Bug #1076968

We print request curl command before HTTP request,
and response information after HTTP request.

Change-Id: I19840e8bd606a30389cd029c0add066c945fcdf6
  • Loading branch information
gongysh committed Nov 9, 2012
1 parent 6857c2a commit 57f0e91
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
5 changes: 2 additions & 3 deletions quantumclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ def _cs_request(self, *args, **kwargs):

if 'body' in kwargs:
kargs['body'] = kwargs['body']

utils.http_log_req(_logger, args, kargs)
resp, body = self.request(*args, **kargs)

utils.http_log(_logger, args, kargs, resp, body)
utils.http_log_resp(_logger, resp, body)
status_code = self.get_status_code(resp)
if status_code == 401:
raise exceptions.Unauthorized(message=body)
Expand Down
40 changes: 22 additions & 18 deletions quantumclient/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,27 @@ def str2dict(strdict):
return _info


def http_log(_logger, args, kwargs, resp, body):
if not _logger.isEnabledFor(logging.DEBUG):
return

string_parts = ['curl -i']
for element in args:
if element in ('GET', 'POST'):
string_parts.append(' -X %s' % element)
else:
string_parts.append(' %s' % element)
def http_log_req(_logger, args, kwargs):
if not _logger.isEnabledFor(logging.DEBUG):
return

string_parts = ['curl -i']
for element in args:
if element in ('GET', 'POST', 'DELETE', 'PUT'):
string_parts.append(' -X %s' % element)
else:
string_parts.append(' %s' % element)

for element in kwargs['headers']:
header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
string_parts.append(header)

if 'body' in kwargs and kwargs['body']:
string_parts.append(" -d '%s'" % (kwargs['body']))
_logger.debug("\nREQ: %s\n" % "".join(string_parts))

for element in kwargs['headers']:
header = ' -H "%s: %s"' % (element, kwargs['headers'][element])
string_parts.append(header)

_logger.debug("REQ: %s\n" % "".join(string_parts))
if 'body' in kwargs and kwargs['body']:
_logger.debug("REQ BODY: %s\n" % (kwargs['body']))
_logger.debug("RESP:%s\n", resp)
_logger.debug("RESP BODY:%s\n", body)
def http_log_resp(_logger, resp, body):
if not _logger.isEnabledFor(logging.DEBUG):
return
_logger.debug("RESP:%s %s\n", resp, body)

0 comments on commit 57f0e91

Please sign in to comment.