github.com/s7techlab/cckit@v0.10.5/examples/cpaper_asservice/bin/api/_common.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  API_HOST=${API_HOST:-''}
     4  VERBOSE=${VERBOSE:-'s'}
     5  
     6  if [[ -z "$API_HOST" ]]; then echo "need to set env API_HOST"; exit 1; fi
     7  
     8  
     9  GET(){
    10      if [[ -z "$1" ]]; then echo "\$1 - PATH"; exit 1; fi
    11  
    12      SHOW "`
    13            curl -${VERBOSE} -X GET -H "Content-Type: application/json"  \
    14            ${API_HOST}$1
    15           `"
    16  }
    17  
    18  
    19  DELETE(){
    20      if [[ -z "$1" ]]; then echo "\$1 - PATH"; exit 1; fi
    21  
    22      SHOW "`
    23           curl -${VERBOSE} -X DELETE -H "Content-Type: application/json" \
    24           ${API_HOST}$1
    25          `"
    26  }
    27  
    28  POST(){
    29      if [[ -z "$1" ]]; then echo "\$1 - PATH"; exit 1; fi
    30      if [[ -z "$2" ]]; then echo "\$2 - BODY"; exit 1;  fi
    31  
    32      SHOW "`
    33          curl -${VERBOSE} -X POST -H "Content-Type: application/json"  -d "$2" \
    34               ${API_HOST}$1
    35          `"
    36  }
    37  
    38  
    39  PUT(){
    40      if [[ -z "$1" ]]; then echo "\$1 - PATH"; exit 1; fi
    41  
    42      SHOW "`
    43            curl -${VERBOSE} -X PUT -H "Content-Type: application/json"  \
    44            ${API_HOST}$1
    45           `"
    46  }
    47  
    48  SHOW() {
    49    PRETTY=${PRETTY:-''}
    50  
    51    if [[ -z "$PRETTY" ]]; then
    52      echo "$1"
    53    elif [[ "$PRETTY" == "python" ]]; then
    54      printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
    55    else
    56      echo $1 | jq
    57    fi
    58  }