1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-12-26 09:56:13 +01:00

osc req: add option -a / --add-header to add arbitrary request headers

This commit is contained in:
Dr. Peter Poeml 2008-03-16 20:46:16 +00:00
parent 7b91075229
commit 54c1690309
2 changed files with 14 additions and 2 deletions

View File

@ -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]:

View File

@ -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: