#!/usr/bin/env python

from jsontester.shell import Script
from jsontester.request import JSONRequest,JSONRequestError,response_code_text

script = Script(description='Send a HTTP DELETE request to given URL as JSON')
script.add_argument('urls', nargs='*', help='URLs to delete')
args = script.parse_args()

request = JSONRequest(browser=args.browser, username=args.username, password=args.password, verify=args.insecure)

for url in args.urls:
    script.log.debug('Submitting DELETE request to {0}'.format(url))

    try:
        res = request.delete(url)
    except JSONRequestError as e:
        script.exit(1, e)

    res_text = response_code_text(res.status_code)
    if res.status_code != 204:
        script.error('RESPONSE CODE: {0}'.format(res_text))
        script.error('ERROR deleting JSON resource')
        continue

    if args.debug:
        script.error('RESPONSE CODE: {0}'.format(res_text))

    if res.content.strip():
        script.message(res.content)

