From: Alfredo Deza Date: Wed, 3 Dec 2014 22:14:20 +0000 (-0500) Subject: refactor the request helpers out of client X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=dfab4b902dc0c505cb5981a8b9224768e4acd5ff;p=radosgw-agent.git refactor the request helpers out of client Signed-off-by: Alfredo Deza --- diff --git a/radosgw_agent/request.py b/radosgw_agent/request.py new file mode 100644 index 0000000..6c38a66 --- /dev/null +++ b/radosgw_agent/request.py @@ -0,0 +1,104 @@ +import boto +import logging +from boto.connection import AWSAuthConnection + +log = logging.getLogger(__name__) + + +class MetaData(object): + """ + A basic container class that other than the method, it just registers + all the keyword arguments passed in, so that it is easier/nicer to + re-use the values + """ + + def __init__(self, conn, method, **kw): + self.conn = conn + self.method = method + for k, v in kw.items(): + setattr(self, k, v) + + +def base_http_request(conn, method, basepath='', resource='', headers=None, + data=None, special_first_param=None, params=None): + """ + Returns a ``AWSAuthConnection.build_base_http_request`` call with the + preserving of the special params done by ``build``. + """ + + # request meta data + md = build( + conn, + method, + basepath=basepath, + resource=resource, + headers=headers, + data=data, + special_first_param=special_first_param, + params=params, + ) + + return AWSAuthConnection.build_base_http_request( + md.conn, md.method, md.path, + md.auth_path, md.params, md.headers, + md.data, md.host) + + +def make_request(conn, method, basepath='', resource='', headers=None, + data=None, special_first_param=None, params=None, _retries=3): + """ + Returns a ``AWSAuthConnection.make_request`` call with the preserving + of the special params done by ``build``. + """ + # request meta data + md = build( + conn, + method, + basepath=basepath, + resource=resource, + headers=headers, + data=data, + special_first_param=special_first_param, + params=params, + ) + + return AWSAuthConnection.make_request( + md.conn, md.method, md.path, + headers=md.headers, + data=md.data, + host=md.host, + auth_path=md.auth_path, + params=md.params, + override_num_retries=_retries + ) + + +def build(conn, method, basepath='', resource='', headers=None, + data=None, special_first_param=None, params=None): + """ + Adapted from the build_request() method of boto.connection + """ + + path = conn.calling_format.build_path_base(basepath, resource) + auth_path = conn.calling_format.build_auth_path(basepath, resource) + host = conn.calling_format.build_host(conn.server_name(), '') + + if special_first_param: + path += '?' + special_first_param + boto.log.debug('path=%s' % path) + auth_path += '?' + special_first_param + boto.log.debug('auth_path=%s' % auth_path) + + return MetaData( + conn, + method, + path=path, + auth_path=auth_path, + basepath=basepath, + resource=resource, + headers=headers, + data=data, + special_first_param=special_first_param, + params=params, + host=host, + )