Mailinglist Archive: opensuse-buildservice (181 mails)
| < Previous | Next > |
[opensuse-buildservice] How to extend osc locally (or: issuing arbitrary requests to the api)
- From: "Dr. Peter Poeml" <poeml@xxxxxxx>
- Date: Thu, 19 Jul 2007 12:32:47 +0200
- Message-id: <20070719103247.GP11306@xxxxxxx>
Hi,
I recently announced this little change:
> The following change gives you the opportunity to implement new
> subcommands (or change existing commands) without touching the osc
> package (if there is interest in this, I can write a howto):
>
> - load subcommands from /var/lib/osc-plugins/ or ~/.osc-plugins/
Since I just implemented a new command, which I figured could be handy,
I am posting this as an example.
You can use it after copying the attached file to ~/.osc-plugins.
It allows to issue "raw" HTTP requests to the api by specifying the URL
and possible data or file to send, like:
osc req /source/home:poeml
or
osc req -m PUT -f /etc/fstab source/home:poeml/test5/myfstab
Thus, it can be used in a curl-like fashion, with authentification
handled by osc.
Peter
--
"WARNING: This bug is visible to non-employees. Please be respectful!"
SUSE LINUX Products GmbH
Research & Development
@cmdln.option('-m', '--method', default='GET', metavar='HTTP_METHOD',
help='specify HTTP method to use (GET|PUT|DELETE|POST)')
@cmdln.option('-d', '--data', default=None, metavar='STRING',
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')
def do_req(self, subcmd, opts, url):
"""${cmd_name}: Issue an arbitrary request to the API
Useful for testing.
URL can be specified either partially (only the path component), or fully
with URL scheme and hostname ('http://...').
Examples:
osc req /source/home:poeml
osc req -m PUT -f /etc/fstab source/home:poeml/test5/myfstab
${cmd_usage}
${cmd_option_list}
"""
if not opts.method in ['GET', 'PUT', 'POST', 'DELETE']:
sys.exit('unknown method %s' % opts.method)
if not url.startswith('http'):
if not url.startswith('/'):
url = '/' + url
url = conf.config['apiurl'] + url
try:
r = http_request(opts.method,
url,
data=opts.data,
file=opts.file)
except urllib2.HTTPError, e:
if e.code == 400:
print >>sys.stderr, e
print >>sys.stderr, e.read()
sys.exit(1)
if e.code == 500:
print >>sys.stderr, e
# this may be unhelpful... because it may just print a big blob of uninteresting
# ichain html and javascript... however it could potentially be useful if the orign
# server returns an information body
if conf.config['http_debug']:
print >>sys.stderr, e.read()
sys.exit(1)
else:
sys.exit('unexpected error')
try:
out = r.read()
except:
sys.exit('failed to read from file object')
sys.stdout.write(out)
I recently announced this little change:
> The following change gives you the opportunity to implement new
> subcommands (or change existing commands) without touching the osc
> package (if there is interest in this, I can write a howto):
>
> - load subcommands from /var/lib/osc-plugins/ or ~/.osc-plugins/
Since I just implemented a new command, which I figured could be handy,
I am posting this as an example.
You can use it after copying the attached file to ~/.osc-plugins.
It allows to issue "raw" HTTP requests to the api by specifying the URL
and possible data or file to send, like:
osc req /source/home:poeml
or
osc req -m PUT -f /etc/fstab source/home:poeml/test5/myfstab
Thus, it can be used in a curl-like fashion, with authentification
handled by osc.
Peter
--
"WARNING: This bug is visible to non-employees. Please be respectful!"
SUSE LINUX Products GmbH
Research & Development
@cmdln.option('-m', '--method', default='GET', metavar='HTTP_METHOD',
help='specify HTTP method to use (GET|PUT|DELETE|POST)')
@cmdln.option('-d', '--data', default=None, metavar='STRING',
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')
def do_req(self, subcmd, opts, url):
"""${cmd_name}: Issue an arbitrary request to the API
Useful for testing.
URL can be specified either partially (only the path component), or fully
with URL scheme and hostname ('http://...').
Examples:
osc req /source/home:poeml
osc req -m PUT -f /etc/fstab source/home:poeml/test5/myfstab
${cmd_usage}
${cmd_option_list}
"""
if not opts.method in ['GET', 'PUT', 'POST', 'DELETE']:
sys.exit('unknown method %s' % opts.method)
if not url.startswith('http'):
if not url.startswith('/'):
url = '/' + url
url = conf.config['apiurl'] + url
try:
r = http_request(opts.method,
url,
data=opts.data,
file=opts.file)
except urllib2.HTTPError, e:
if e.code == 400:
print >>sys.stderr, e
print >>sys.stderr, e.read()
sys.exit(1)
if e.code == 500:
print >>sys.stderr, e
# this may be unhelpful... because it may just print a big blob of uninteresting
# ichain html and javascript... however it could potentially be useful if the orign
# server returns an information body
if conf.config['http_debug']:
print >>sys.stderr, e.read()
sys.exit(1)
else:
sys.exit('unexpected error')
try:
out = r.read()
except:
sys.exit('failed to read from file object')
sys.stdout.write(out)
| < Previous | Next > |