From 54c16903091a4f07c85e8d0384e35cb72fb253a6 Mon Sep 17 00:00:00 2001 From: "Dr. Peter Poeml" Date: Sun, 16 Mar 2008 20:46:16 +0000 Subject: [PATCH] osc req: add option -a / --add-header to add arbitrary request headers --- osc/commandline.py | 9 ++++++++- osc/core.py | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/osc/commandline.py b/osc/commandline.py index 8a4e4919..3aee5706 100755 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -1944,6 +1944,9 @@ class Osc(cmdln.Cmdln): help='specify string data for e.g. POST') @cmdln.option('-f', '--file', default=None, metavar='FILE', help='specify filename for e.g. PUT or DELETE') + @cmdln.option('-a', '--add-header', default=None, metavar='NAME STRING', + nargs=2, action='append', dest='headers', + help='add the specified header to the request') def do_req(self, subcmd, opts, url): """${cmd_name}: Issue an arbitrary request to the API @@ -1970,11 +1973,15 @@ class Osc(cmdln.Cmdln): url = '/' + url url = conf.config['apiurl'] + url + if opts.headers: + opts.headers = dict(opts.headers) + try: r = http_request(opts.method, url, data=opts.data, - file=opts.file) + file=opts.file, + headers=opts.headers) except urllib2.HTTPError, e: if e.code in [400, 404]: diff --git a/osc/core.py b/osc/core.py index b037b951..3b5f6c36 100755 --- a/osc/core.py +++ b/osc/core.py @@ -1243,7 +1243,7 @@ def makeurl(baseurl, l, query=[]): return urlunsplit((scheme, netloc, '/'.join(l), '&'.join(query), '')) -def http_request(method, url, data=None, file=None): +def http_request(method, url, headers={}, data=None, file=None): """wrapper around urllib2.urlopen for error handling, and to support additional (PUT, DELETE) methods""" @@ -1266,6 +1266,11 @@ def http_request(method, url, data=None, file=None): if method == 'PUT': req.add_header('Content-Type', 'application/octet-stream') + if type(headers) == type({}): + for i in headers.keys(): + print headers[i] + req.add_header(i, headers[i]) + if file and not data: size = os.path.getsize(file) if size < 1024*512: