« Back to Index

[Fastly Edge Dictionaries API Examples]

View original Gist on GitHub

Tags: #fastly #api #cdn #edge #dictionaries #bash #shell

Fastly Edge Dictionaries API Examples.bash

#!/usr/bin/env bash

# API documentation here...
# https://docs.fastly.com/api/config
#
# at the end of the day, these are just simple `curl` requests
# along with a HTTP header that holds our api token.
#
# meaning, you could rewrite all of this much more cleanly in
# a more fully feature language such as Go or Python.

set -e

myservice="123"

api_token="Fastly-Key: $FASTLY_API_TOKEN_ADMIN"
api_host="https://api.fastly.com"

#-----------------------------------------------------------------------------------------------

# GET LATEST SERVICE VERSION

service_latest=$(curl -s -H "$api_token" "$api_host/service/$myservice" | jq .versions[-1])
printf "\\nlatest service:\\n\\n"
echo "$service_latest" | jq

service_version=$(echo "$service_latest" | jq .number)

#-----------------------------------------------------------------------------------------------

# LIST ALL DICTIONARIES FOR LATEST SERVICE VERSION

printf "\\nlist of edge dictionaries:\\n\\n"
dict_list=$(curl -s -H "$api_token" "$api_host/service/$myservice/version/$service_version/dictionary")
echo "$dict_list" | jq

#-----------------------------------------------------------------------------------------------

# LIST METADATA FOR A SPECIFIC DICTIONARY (e.g. the foo_bar dictionary already exists)

dict_name="foo_bar"
dict_id=$(echo "$dict_list" | jq -r ".[] | select(.name == \"$dict_name\") | .id")

printf "\\nlist dictionary meta data:\\n\\n"
curl -s -H "$api_token" "$api_host/service/$myservice/version/$service_version/dictionary/$dict_id/info" | jq

printf "\\nlist dictionary content:\\n\\n"
curl -s -H "$api_token" "$api_host/service/$myservice/dictionary/$dict_id/items" | jq

#-----------------------------------------------------------------------------------------------

# CREATE NEW DICTIONARY FOR SERVICE

printf "\\ncreate new service version (required before we can create a new edge dictionary):\\n\\n"
service_latest=$(curl -s -H "$api_token" -X PUT "$api_host/service/$myservice/version/$service_version/clone")
echo "$service_latest" | jq
service_version=$(echo "$service_latest" | jq .number)

printf "\\ncreate new edge dictionary:\\n\\n"
dict_name="my_new_edge_dict"
curl -s -H "$api_token" -X POST -d "name=$dict_name" "$api_host/service/$myservice/version/$service_version/dictionary" | jq

printf "\\nlist of edge dictionaries:\\n\\n"
dict_list=$(curl -s -H "$api_token" "$api_host/service/$myservice/version/$service_version/dictionary")
echo "$dict_list" | jq

printf "\\nlist of edge dictionaries:\\n\\n"
curl -s -H "$api_token" "$api_host/service/$myservice/version/$service_version/dictionary" | jq

#-----------------------------------------------------------------------------------------------

# LIST METADATA FOR A SPECIFIC DICTIONARY (e.g. my_new_edge_dict)

dict_id=$(echo "$dict_list" | jq -r ".[] | select(.name == \"$dict_name\") | .id")

printf "\\nlist dictionary meta data:\\n\\n"
curl -s -H "$api_token" "$api_host/service/$myservice/version/$service_version/dictionary/$dict_id/info" | jq

printf "\\nlist dictionary content:\\n\\n"
curl -s -H "$api_token" "$api_host/service/$myservice/dictionary/$dict_id/items" | jq

#-----------------------------------------------------------------------------------------------

# CREATE NEW KEY/VALUE FOR NEW DICTIONARY

printf "\\ncreate new key/value:\\n\\n"
dict_key="west"
dict_val="false"
curl -s -H "$api_token" -X POST -d "item_key=$dict_key&item_value=$dict_val" "$api_host/service/$myservice/dictionary/$dict_id/item" | jq

printf "\\nlist dictionary content:\\n\\n"
curl -s -H "$api_token" "$api_host/service/$myservice/dictionary/$dict_id/items" | jq

#-----------------------------------------------------------------------------------------------

# if you need to delete a dictionary...
#
# curl -s -H "$api_token" -X DELETE "$api_host/service/$myservice/version/$service_version/dictionary/www_buzzfeed_com"
#
# if you need to delete a dictionary key/value...
#
# curl -s -H "$api_token" -X DELETE "$api_host/service/$myservice/dictionary/$dict_id/item/$dict_key" | jq
#
# if you need to update a dictionary key/value...
#
# dict_val="true"
# curl -s -H "$api_token" -X PATCH -d "item_value=$dict_val" "$api_host/service/$myservice/dictionary/$dict_id/item/$dict_key" | jq