Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions bin/agave-curl
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/usr/bin/env python
import re
import argparse
import requests
import sys
import os
from os.path import expanduser

cmd = ''
for a in sys.argv:
if re.search(r'[\s"]',a):
cmd += " '%s'" % a
else:
cmd += " " + a

parser = argparse.ArgumentParser(description="Python-based Curl Replacement")
parser.add_argument("-s","--silent",help="Silent mode",action="store_true")
parser.add_argument("-k","--insecure",help="Allow insecure connections",action="store_true")
parser.add_argument("-u","--user",help="Environment variable to read for USER:PASSWORD")
parser.add_argument("-X","--request",help="Request Type")
parser.add_argument("-H","--header",help="Environment variable to read for a header",action="append")
parser.add_argument("-d","--data",help="HTTP POST data",action="append")
parser.add_argument("--data-urlencode",help="HTTP POST data",action="append")
parser.add_argument("--data-binary",help="HTTP POST data")
parser.add_argument("--globoff",help="Disable URL sequences and ranges using {} and []",action="store_true")
parser.add_argument("-F","--form",help="Specify HTTP multipart POST data",action="append")
parser.add_argument("url",nargs="?")
args = parser.parse_args()

def loader(val,require_secure=False):
g = re.match(r'^\s*(@|\$)(.*)',val)
if g:
if g.group(1) == '@':
try:
if g.group(2) == '-':
return sys.stdin
else:
return open(expanduser(g.group(2)),"rb")
except:
sys.stderr.write('curl: couldn\'t open file "%s"' % g.group(2))
return ""
else: # g.group(1) == '$'
try:
return os.environ[g.group(2)]
except:
sys.stderr.write('curl: couldn\'t read environment variable "%s"' % g.group(2))
return ""
else:
#if require_secure:
# raise Exception("Credentials Exposed: "+cmd)
return val

verify = True
if args.insecure:
pass #verify = not args.insecure

request = "GET"
if args.request:
request = args.request

headers = {}
if args.header:
for arg in args.header:
s = arg.split(":")
if s[0] == "Authorization":
headers[s[0]]=loader(s[1],True)
else:
headers[s[0]]=loader(s[1],False)

data = []

if args.data:
for d1 in args.data:
for d in d1.split('&'):
g = re.match(r'(.*?)=(.*)',d)
if g:
data += [(g.group(1),loader(g.group(2)))]
else:
data += [(loader(d),'')]

if args.data_urlencode:
for d1 in args.data_urlencode:
for d in d1.split('&'):
g = re.match(r'(.*?)=(.*)',d)
if g:
data += [(g.group(1),loader(g.group(2)))]
else:
data += [(loader(d),'')]

if args.data_binary:
data = loader(args.data_binary)
if hasattr(data,"read"):
data = data.read()

if args.user:
s = args.user.split(":")
auth = (s[0],loader(s[1],True))
else:
auth = None

form_data = {}
if args.form:
for f in args.form:
g = re.match(r'^(.*?)=(.*)$',f)
if g:
form_data[g.group(1)] = loader(g.group(2))

res = ''

if request == "GET":
if args.user:
res = requests.get(args.url,headers=headers,data=data,auth=auth,verify=verify)
else:
res = requests.get(args.url,headers=headers,data=data,verify=verify)

elif request == "POST":
if args.user:
res = requests.post(args.url,headers=headers,data=data,auth=auth,files=form_data,verify=verify)
else:
res = requests.post(args.url,headers=headers,data=data,files=form_data,verify=verify)

elif request == "DELETE":
if args.user:
res = requests.delete(args.url,headers=headers,data=data,auth=auth,verify=verify)
else:
res = requests.delete(args.url,headers=headers,data=data,verify=verify)

elif request == "PUT":
if args.user:
res = requests.put(args.url,headers=headers,data=data,auth=auth,verify=verify)
else:
res = requests.put(args.url,headers=headers,data=data,verify=verify)

else:
raise Exception("Not supported: "+cmd)

# Python 3 issue
if type(res.text) == str:
sys.stdout.write(res.text)
else:
sys.stdout.write(res.text.encode('utf-8'))
8 changes: 4 additions & 4 deletions bin/apps-addupdate
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,25 @@ main() {
# reading from stdin
if [[ "$filetoupload" == "-" ]]; then

cmd="curl --globoff -sk -H \"${authheader}\" -H \"Content-Type: application/json\" -X POST --data-binary @- '${hosturl}${args}?pretty=true'"
cmd="agave-curl --globoff -sk -H \"${authheader}\" -H \"Content-Type: application/json\" -X POST --data-binary @- '${hosturl}${args}?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

# make sure we specify content type as application/json
response=`curl --globoff -sk -H "${authheader}" -H "Content-Type: application/json" -X POST --data-binary @- "${hosturl}${args}?pretty=true"`
response=`agave-curl --globoff -sk -H "${authheader}" -H "Content-Type: application/json" -X POST --data-binary @- "${hosturl}${args}?pretty=true"`

# standard file upload
else

cmd="curl -sk -H \"${authheader}\" -X POST -F \"fileToUpload=@$filetoupload\" '${hosturl}${args}?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X POST -F \"fileToUpload=@$filetoupload\" '${hosturl}${args}?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X POST -F "fileToUpload=@$filetoupload" "${hosturl}${args}?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X POST -F "fileToUpload=@$filetoupload" "${hosturl}${args}?pretty=true"`

fi

Expand Down
4 changes: 2 additions & 2 deletions bin/apps-clone
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ main() {
form_params="${form_params} --data-urlencode "${dat}
fi

cmd="curl -sk -H \"${authheader}\" -X PUT '$hosturl$args?pretty=true' --data-urlencode "action=clone" '${form_params}'"
cmd="agave-curl -sk -H \"${authheader}\" -X PUT '$hosturl$args?pretty=true' --data-urlencode "action=clone" '${form_params}'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X PUT -d "$form_params" "$hosturl$args?pretty=true" --data-urlencode "action=clone" ${form_params}`
response=`agave-curl -sk -H "${authheader}" -X PUT -d "$form_params" "$hosturl$args?pretty=true" --data-urlencode "action=clone" ${form_params}`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-delete
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ main() {
err $response
else

cmd="curl -sk -H \"${authheader}\" -X DELETE '$hosturl$args?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X DELETE '$hosturl$args?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X DELETE "$hosturl$args?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X DELETE "$hosturl$args?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-disable
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ main() {
err "Please specify a valid app id to enable"
else

cmd="curl -sk -H \"${authheader}\" -X PUT -d \"action=disable\" '$hosturl$args?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X PUT -d \"action=disable\" '$hosturl$args?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X PUT -d "action=disable" "$hosturl$args?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X PUT -d "action=disable" "$hosturl$args?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-enable
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ main() {
err "Please specify a valid app id to enable"
else

cmd="curl -sk -H \"${authheader}\" -X PUT -d \"action=enable\" '$hosturl$args?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X PUT -d \"action=enable\" '$hosturl$args?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X PUT -d "action=enable" "$hosturl$args?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X PUT -d "action=enable" "$hosturl$args?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-erase
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ main() {
err "Please specify a valid app id to erase"
else

cmd="curl -sk -H \"${authheader}\" -X PUT -d \"action=erase\" '$hosturl$args?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X PUT -d \"action=erase\" '$hosturl$args?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X PUT -d "action=erase" "$hosturl$args?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X PUT -d "action=erase" "$hosturl$args?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-history
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ main() {

[[ -n "${eventId}" ]] && hosturl="${hosturl}/${eventId}"

cmd="curl -sk -H \"${authheader}\" '$hosturl?pretty=true$(pagination)'"
cmd="agave-curl -sk -H \"${authheader}\" '$hosturl?pretty=true$(pagination)'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" "${hosturl}?pretty=true$(pagination)"`
response=`agave-curl -sk -H "${authheader}" "${hosturl}?pretty=true$(pagination)"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then

Expand Down
4 changes: 2 additions & 2 deletions bin/apps-list
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ main() {
fi
fi

cmd="curl -sk -H \"${authheader}\" '$hosturl?${querystring}'"
cmd="agave-curl -sk -H \"${authheader}\" '$hosturl?${querystring}'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" "$hosturl?${querystring}"`
response=`agave-curl -sk -H "${authheader}" "$hosturl?${querystring}"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "${response}")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-pems-delete
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ main() {
if [ -z "$args" ]; then
err "Please specify an app id for which to set the permissions"
else
cmd="curl -sk -H \"${authheader}\" -X DELETE '$hosturl$args/pems/$pemusername?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X DELETE '$hosturl$args/pems/$pemusername?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X DELETE "$hosturl$args/pems/$pemusername?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X DELETE "$hosturl$args/pems/$pemusername?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-pems-list
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ main() {
err "Please specify a valid app id for which to retrieve permissions"
else

cmd="curl -sk -H \"${authheader}\" '$hosturl$args/pems/$pemusername?pretty=true$(pagination)'"
cmd="agave-curl -sk -H \"${authheader}\" '$hosturl$args/pems/$pemusername?pretty=true$(pagination)'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" "$hosturl$args/pems/$pemusername?pretty=true$(pagination)"`
response=`agave-curl -sk -H "${authheader}" "$hosturl$args/pems/$pemusername?pretty=true$(pagination)"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-pems-update
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ main() {
if [ -z "$args" ]; then
err "Please specify an app id for which to set the permissions"
else
cmd="curl -sk -H \"${authheader}\" -X POST -d \"permission=$permission\" '$hosturl$args/pems/$pemusername?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X POST -d \"permission=$permission\" '$hosturl$args/pems/$pemusername?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X POST -d "permission=$permission" "$hosturl$args/pems/$pemusername?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X POST -d "permission=$permission" "$hosturl$args/pems/$pemusername?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-publish
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ main() {
fi


cmd="curl -sk -H \"${authheader}\" -X PUT --data-urlencode \"action=publish\" ${publicAppparams} '$hosturl$args?pretty=true'"
cmd="agave-curl -sk -H \"${authheader}\" -X PUT --data-urlencode \"action=publish\" ${publicAppparams} '$hosturl$args?pretty=true'"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -sk -H "${authheader}" -X PUT --data-urlencode "action=publish" ${publicAppparams} "$hosturl$args?pretty=true"`
response=`agave-curl -sk -H "${authheader}" -X PUT --data-urlencode "action=publish" ${publicAppparams} "$hosturl$args?pretty=true"`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
4 changes: 2 additions & 2 deletions bin/apps-search
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ main() {
done
fi

cmd="curl -G -sk -H \"${authheader}\" '$appsurl?pretty=true$(pagination)' $querystring)"
cmd="agave-curl -G -sk -H \"${authheader}\" '$appsurl?pretty=true$(pagination)' $querystring)"

if ((veryverbose)); then
[ "$piped" -eq 0 ] && log "Calling $cmd"
fi

response=`curl -G -sk -H "${authheader}" $appsurl?pretty=true$(pagination) ${querystring}`
response=`agave-curl -G -sk -H "${authheader}" $appsurl?pretty=true$(pagination) ${querystring}`

if [[ $(jsonquery "$response" "status") = 'success' ]]; then
result=$(format_api_json "$response")
Expand Down
Loading