├── README.md └── algoliasearch-cmd.sh /README.md: -------------------------------------------------------------------------------- 1 | AlgoliaSearch REST API CLI Client 2 | ================================= 3 | 4 | This API client has been done to ease the usage of Algolia's REST API from the command line. It's a [cURL](https://curl.haxx.se/docs/manpage.html) wrapper handling the retry strategy complexity for you. 5 | 6 | # Usage 7 | 8 | ``` 9 | $ ./algoliasearch-cmd.sh (GET|POST|PUT|DELETE) /1/ [cURL arguments] 10 | ``` 11 | 12 | Examples: 13 | 14 | * Test if the service is alive: 15 | 16 | ``` 17 | $ APPLICATION_ID=******** API_KEY=******** ./algoliasearch-cmd.sh GET /1/isalive 18 | ``` 19 | 20 | * Delete an index: 21 | 22 | ``` 23 | $ APPLICATION_ID=******** API_KEY=******** ./algoliasearch-cmd.sh DELETE /1/indexes/myindextodelete 24 | ``` 25 | 26 | * Set the settings of an index: 27 | 28 | ``` 29 | $ APPLICATION_ID=******** API_KEY=******** ./algoliasearch-cmd.sh PUT /1/indexes/myindex/settings -d '{"attributesToIndex": ["title", "description"]}' 30 | ``` 31 | 32 | * Search: 33 | 34 | ``` 35 | $ APPLICATION_ID=******** API_KEY=******** ./algoliasearch-cmd.sh POST /1/indexes/myindex/query -d '{"query": "test"}' 36 | ``` 37 | 38 | -------------------------------------------------------------------------------- /algoliasearch-cmd.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | HOST="" 4 | 5 | usage() { 6 | echo "Usage: $0 (GET|POST|PUT|DELETE) /1/ [cURL arguments]" 7 | echo 8 | echo "Ex:" 9 | echo " Test if the service is alive:" 10 | echo " APPLICATION_ID=******** API_KEY=************************* $0 GET /1/isalive" 11 | echo " Delete an index:" 12 | echo " APPLICATION_ID=******** API_KEY=************************* $0 DELETE /1/indexes/myindextodelete" 13 | echo " Set the settings of an index:" 14 | echo " APPLICATION_ID=******** API_KEY=************************* $0 PUT /1/indexes/myindex/settings -d '{\"attributesToIndex\": [\"title\", \"description\"]}'" 15 | echo " Search:" 16 | echo " APPLICATION_ID=******** API_KEY=************************* $0 POST /1/indexes/myindex/query -d '{\"query\": \"test\"}'" 17 | echo 18 | echo 19 | exit 1 20 | } 21 | 22 | if [ -z "$APPLICATION_ID" ]; then 23 | echo "error: Please set your APPLICATION_ID environment variable." 24 | echo 25 | usage 26 | fi 27 | if [ -z "$API_KEY" ]; then 28 | echo "error: Please set your API_KEY environment variable." 29 | echo 30 | usage 31 | fi 32 | 33 | headers=(--header "Content-Type: application/json; charset=utf-8") 34 | headers+=(--header "X-Algolia-Application-Id: $APPLICATION_ID") 35 | headers+=(--header "X-Algolia-API-Key: $API_KEY") 36 | 37 | method= 38 | case $1 in 39 | POST) 40 | method="$1" 41 | ;; 42 | PUT) 43 | method="$1" 44 | ;; 45 | DELETE) 46 | method="$1" 47 | ;; 48 | GET) 49 | method="$1" 50 | ;; 51 | *) 52 | usage 53 | ;; 54 | esac 55 | shift 56 | 57 | path= 58 | case $1 in 59 | /*) 60 | path="$1" 61 | ;; 62 | *) 63 | usage 64 | ;; 65 | esac 66 | shift 67 | 68 | request() { 69 | stdout=`mktemp -t algolia.XXX` 70 | stderr=`mktemp -t algolia.XXX` 71 | code=0 72 | for domain in ".algolia.net" "-1.algolianet.com" "-2.algolianet.com" "-3.algolianet.com"; do 73 | url="https://$APPLICATION_ID$domain$path" 74 | curl --silent --show-error "${headers[@]}" --request $method "$url" "$@" > $stdout 2> $stderr 75 | code=$? 76 | if [ $code -eq 0 ]; then 77 | break 78 | fi 79 | done 80 | cat "$stdout" 81 | cat "$stderr" >&2 82 | rm "$stdout" 83 | rm "$stderr" 84 | exit $code 85 | } 86 | 87 | request "$@" 88 | --------------------------------------------------------------------------------