play.sh (1890B)
1 #!/bin/sh 2 3 base_url="http://localhost:8080/blueprints" 4 5 with_error=0 6 7 while getopts "e" arg; do 8 case $arg in 9 e) with_error=1;; 10 *);; 11 esac 12 done 13 14 shift $((OPTIND-1)) 15 16 id=$1 17 body_name= 18 body_number= 19 20 get_id () { 21 if [ -z "${id}" ]; then 22 echo "For what UUID?" 23 read -r id 24 fi 25 } 26 27 get_url () { 28 base_url="${base_url}$1" 29 if [ "${with_error}" -eq 1 ]; then 30 base_url="${base_url}?errors=true" 31 fi 32 echo "${base_url}" 33 } 34 35 get_body_name () { 36 echo "Request name?" 37 read -r body_name 38 } 39 40 get_body_number () { 41 echo "Request number?" 42 read -r body_number 43 } 44 45 request= 46 47 while [[ "${method}" != @(q|quit) ]] 48 do 49 echo "Request method? (GET/POST/PUT/DELETE) - 'q' or 'quit' to exit script." 50 read -r method 51 52 case ${method^^} in 53 54 GET) 55 get_id 56 if [ -z "${id}" ]; then 57 request=$(curl -X GET -sw '%{http_code}' $(get_url) | jq) 58 else 59 request=$(curl -X GET -sw '%{http_code}' $(get_url "/${id}") | jq) 60 fi 61 ;; 62 63 POST) 64 get_body_name 65 get_body_number 66 request=$(curl -X POST -sw '%{http_code}' $(get_url) -H 'Content-Type: application/json' -d "{\"name\":\"${body_name}\",\"number\":\"${body_number}\"}" | jq) 67 ;; 68 69 PUT) 70 get_id 71 get_body_name 72 get_body_number 73 request=$(curl -X PUT -sw '%{http_code}' $(get_url "/${id}") -H 'Content-Type: application/json' -d "{\"name\":\"${body_name}\",\"number\":\"${body_number}\"}"| jq) 74 ;; 75 76 Q|QUIT) 77 exit 0 78 ;; 79 80 DELETE) 81 get_id 82 request=$(curl -k -X 'DELETE' -sw '%{http_code}' $(get_url "/${id}") | jq) 83 ;; 84 85 *) 86 echo "Unknown http method." 87 ;; 88 esac 89 90 echo ${method^^} 91 echo "${request}" 92 retrieved_id=$(echo "${request}" | jq "try .[0].id catch \"JQ_ERROR\"" | head -n 1 | tr -d \") 93 94 if [ "${retrieved_id}" == "JQ_ERROR" ]; then 95 retrieved_id=$(echo "${request}" | jq "try .id catch \"JQ_ERROR\"" | head -n 1 | tr -d \") 96 fi 97 98 echo "${retrieved_id}" | xclip 99 100 id= 101 body_name= 102 body_number= 103 done