Commits

bigdoor_jeff  committed f6afba1

First commit! Whoop!

  • Participants

Comments (0)

Files changed (7)

+To use bigdoor_bashkit:
+1. Go to http://publisher.bigdoor.com, sign up for an account, read up on the documentation, and get your application and secret keys.
+2. Make sure the bigdoor_bashkit script is executable (chmod 700 bigdoor_bashkit). Make it easier to use if you like, by sym-linking it to /usr/bin.
+3. Check out usage : ./bigdoor_bashkit -h
+4. Create a ~/.bigdoor_bashkit.cnf config file containing your keys if you like, so that you don't have to pass in your private/public keypar in every command :
+	printf 'app_key=abc123...\napp_secret=abc123...\n' > ~/.bigdoor_bashkit.cnf
+5. Get the json output on a GET to the BigDoor currency endpoint : ./bigdoor_bashkit -e 'currency'
+6. Use the included scripts to get the ids of objects based on their associated key-value pair object attributes : ./bigdoor_id_by_property.sh currency pub_title=MYTITLE.
+7. If you're up for a true beta experience, use setup.sh and economy.cnf to create an entire economy.  
+7. Have good times.

File bigdoor_bashkit.sh

+#! /bin/sh
+# BigDoor BASH API wrapper script 
+# depends : 
+# 	curl 7.18.2 (x86_64-pc-linux-gnu) libcurl/7.18.2 OpenSSL/0.9.8g zlib/1.2.3.3 libidn/1.8 libssh2/0.18
+# 	OpenSSL 0.9.8g 19 Oct 2007
+# 	uuid-runtime
+
+protocol="http://"
+host='api.bigdoor.com'
+base_uri='/api/publisher'
+method='GET'
+entry_point='currency'
+app_key=''
+app_secret=''
+verbosity=
+t="$(date +%s)"
+token=$(uuidgen|sed -e "s/-//g")
+
+function usage()
+{
+	printf "\n%s\n" "	BigDoor API BASH wrapper. Creates tokens and timestamps, and signs requests. Outputs JSON format."
+	printf "\n%s\n" "	Keys are required, but can be saved in either the environment variable BIGDOOR_API_KEYS (e.g. export BIGDOOR_API_KEYS='app_key:app_secret'), or in ~/.bigdoor_bashkit.cnf like this:"
+	printf "\n%s\n" "		app_key=asdf123"
+	printf "%s\n" 	"		app_secret=asdf123"
+	printf "\n\n%s\n" "	Usage :  "
+	printf "%s\n" "	[-k] application_key:application_secret 	- colon-separated BigDoor application and secret key pair, retrievable after signing up for the BigDoor service. Required."
+	printf "%s\n" "	[-m] HTTP Method 				- HTTP method to be used in the request; defaults to GET. (e.g. PUT,POST,GET,DELETE)"
+	printf "%s\n" "	[-e] api entry point				- single-quoted api entry point; defaults to 'currency'. Everything between [http://api.bigdoor.com/api/publisher/{application key}/] and the '?' starting the query string."
+	printf "%s\n" "	[-d] data for envelope				- url-encoded key=value pairs delimited by ampersands, to be passed in the request body envelope. No default, not required."
+	printf "%s\n" "	[-q] data for query string			- url-encoded key=value pairs delimited by ampersands, to be passed in the query string. No default, not required."
+	printf "%s\n" "	[-v] output debugging information		- print out request information including the signature, and response headers."
+	printf "\n%s\n\n" "	Example : -k abcd123:zdef123 -m PUT -e 'currency/20' -d 'pub_title=my great title' "
+}
+
+function gen_api_full_path()
+{
+	api_full_path="${protocol}""${host}""${base_uri}"
+	[[ -n $1 ]] && api_full_path="${api_full_path}"\?"${1}"
+	printf "%s" "${api_full_path}"
+}
+
+function gen_payload()
+{
+	# create a token key-val pair, append any passed-in key-vals, alnum-sort them, and return the result.
+	payload="token="${token}"&time="${t}""
+	[[ -n $1 ]] && payload="${payload}"\&"${1}"
+	payload=$(printf "%s" "${payload}"|sed -e "s/\&/\\n/g" -e "s/ /\%20/g"|sort|xargs| sed -e "s/ /\&/g" -e "s/\%20/ /g")
+	printf "%s" "${payload}"
+}
+
+function gen_signature()
+{
+	# $1 = query-string values
+	query="$(printf "%s" "${1}"|sed -e "s/\&//g" -e "s/\=//g")"
+	# #2 = envelope values
+	envelope=
+	[[ $3 != 'GET' ]] && envelope="$(printf "%s" "${2}"|sed -e "s/\&//g" -e "s/\=//g")"
+	hash_vals=$(printf "%s" "${base_uri}""${query}""${envelope}""${app_secret}")
+	printf "%s" "${hash_vals}" | openssl dgst -sha256 -passin pass:"${app_secret}"
+}
+
+while getopts d:e:k:m:q:v opt
+do
+	case "$opt" in
+		d)	envelope_data="$OPTARG";;
+		e)	entry_point="$OPTARG";;
+		k)	keys="$OPTARG";;
+		m)	method="$OPTARG";;
+		q)	query_string_data="$OPTARG";;
+		v)	verbosity=1;;
+		\?)	usage; exit 1;;
+	esac
+done
+cmd_start=$(expr $OPTIND - 1)
+shift $cmd_start
+
+if [[ -z $keys ]]
+then
+	keys=$BIGDOOR_API_KEYS
+	if [[ -z $keys ]]
+	then
+		app_key=$(cat ~/.bigdoor_bashkit.cnf 2> /dev/null|grep app_key|sed -e "s/app_key\=//")
+		app_secret=$(cat ~/.bigdoor_bashkit.cnf 2> /dev/null|grep app_secret|sed -e "s/app_secret\=//")
+		[[ -z "${app_key}" ]] || [[ -z "${app_secret}" ]] && printf "\nNo keys found in command line, environment, or configuration script. Exiting.\n" && exit 1
+		keys="${app_key}":"${app_secret}"
+	fi
+fi
+
+printf "${keys}"|grep -q ':'
+if [[ $? -eq 0 ]]
+then
+	app_key=$(printf $keys|cut -d: -f1)
+	app_secret=$(printf $keys|cut -d: -f2)
+else
+	printf "\nApplication and/or secret keys not delimited by ':'. Exiting" && exit 1 
+fi
+base_uri="${base_uri}"/"${app_key}"/"${entry_point}"
+envelope=$(gen_payload "$envelope_data")
+query_string=$(gen_payload "$query_string_data")
+signature=$(gen_signature "${query_string}" "${envelope}" "${method}")
+query_string="${query_string}"\&'sig='"${signature}"\&'format=json'
+api_full_path=$(gen_api_full_path "${query_string}")
+
+if [[ $verbosity -gt 0 ]]
+then
+	printf "\n%s\n" "query string: $query_string"
+	printf "%s\n" "envelope:$envelope"
+	printf "%s\n" "signature:$signature"
+	printf "%s\n\n" "full path: $api_full_path"
+	verbosity='-v'
+fi
+response=$(curl $verbosity -w '\nHTTP Response Code:%{http_code}' -s -X "${method}" -d "${envelope}" "${api_full_path}")
+http_response_code=$(echo -n "${response}"|grep -o 'HTTP Response Code:.*$'|cut -d: -f2|sed -e "s/..$//")
+response=$(echo -n "${response}"|sed -e "s/^HTTP Response Code:.*//")
+
+if [[ $http_response_code -eq 4 ]] || [[ $http_response_code -eq 5 ]];then
+	exit 1
+else
+	printf "${response}\n"
+fi

File bigdoor_id_by.py

+#! /usr/bin/env python
+from cjson import decode
+
+try:
+    cmd=raw_input()
+except:
+    exit()
+cmd_list=cmd.split(' ',1)
+key=cmd_list[0].split('=')[0]
+val=cmd_list[0].split('=')[1]
+json=cmd_list[1]
+decoded_json=decode(json)
+objects=decoded_json[0]
+for object in objects:
+    if object[key] == val:
+        print object['id']

File bigdoor_id_by_property.sh

+#! /bin/sh
+
+# $1 : endpoint 
+# $2 : query string
+# $3 : property key=val pair
+def_query='max_records=1000'
+query=$2
+prop_pair=$3
+# So that this can be used easier from the command line,
+# if only two params are sent in, assume the first is the endpoint and the second is the key/val pair, and use a default for the query param.
+[[ ! -n $3 ]] && query=$def_query && prop_pair=$2
+
+bashkit -e ${1} -q ${query} |sed -e "1 s/^/${prop_pair} /"|tr -d '\n'|./bigdoor_id_by.py
+# BigDoor ECONOMY CONFIGURATION FILE
+# 	setup.sh uses pub_title as a key to tie things together and associate objects.  Ideally this would be done entirely through the attribute endpoint instead.
+# 	Consider the act of using setup.sh and this file experimental.  It works, but some of the associations/dependencies don't necessarily work out yet. It may be buggy. 
+# 	the format of this file is very specific; small mistakes can cause big barfings
+# ATTRIBUTES
+# CREATE attribute : pub_title=EXAMPLE1&friendly_id=example1
+# CREATE attribute : pub_title=EXAMPLE2&friendly_id=whateveryouwant
+#
+# CURRENCIES
+# CREATE currency : pub_title=EXAMPLE1
+# 
+# URLS
+# CREATE url : pub_title=EXAMPLE1&url=www.example.com/images/example.png
+# 
+# NAMED GOOD COLLECTIONS
+# CREATE named_good_collection : pub_title=EXAMPLE1 # META associate:attribute:EXAMPLE1:attribute associate:url:EXAMPLE1:url 
+# CREATE named_good : pub_title=EXAMPLE1 # META associate:named_good_collection:EXAMPLE1:named_good_collection_id associate:attribute:EXAMPLE1:attribute associate:url:EXAMPLE1:url 
+
+# NAMED TRANSACTION GROUPS
+# CREATE named_transaction_group : pub_title=EXAMPLE1
+#
+# NAMED TRANSACTIONS
+# CREATE named_transaction : pub_title=EXAMPLE1&default_amount=2  # META  depends_on:currency:EXAMPLE1:currency_id associate:attribute:EXAMPLE1:attribute associate:named_transaction_group:EXAMPLE1:named_transaction_group:named_transaction_is_primary=1
+# CREATE named_transaction : pub_title=EXAMPLE2&default_amount=10  # META  depends_on:currency:EXAMPLE2:currency_id associate:attribute:EXAMPLE2:attribute associate:named_transaction_group:EXAMPLE1:named_transaction_group:named_transaction_is_primary=0
+#
+# NAMED LEVEL COLLECTIONS
+# CREATE named_level_collection : pub_title=EXAMPLE1 # META depends_on:currency:EXAMPLE1:currency_id associate:attribute:EXAMPLE1:attribute associate:attribute:EXAMPLE2:attribute associate:url:EXAMPLE1:url
+# CREATE named_level : pub_title=EXAMPLE1&threshold=1 # META associate:named_level_collection:EXAMPLE1:named_level_collection_id associate:attribute:EXAMPLE1:attribute associate:url:EXAMPLE1:url
+
+#  NAMED AWARD COLLECTIONS
+#
+# CREATE named_award_collection : pub_title=EXAMPLE1 # META associate:url:EXAMPLE1:url 
+# CREATE named_award : pub_title=EXAMPLE1 # META associate:named_award_collection:EXAMPLE1:named_award_collection_id associate:url:EXAMPLE1:url 
+
+#! /bin/sh
+# BigDoor 
+# Q: what is this script for?
+# A: [TODO:answer the question, mofo]
+
+utilities="/var/www/ops.bigdoor.com/trunk/tools/util"
+if [[ -e $utilities ]]
+then
+	. $utilities
+else
+        logger -t BigDoor.unknownscript -p syslog.err "util script not found on $HOSTNAME"
+		printf "Error - utilities file not found \n"
+		exit
+fi
+# Use these for stamps occuring lateer than the source of util.
+# now_file=$(date +%Y_%m_%d_%H_%M_%S_%N)
+# now_readable=$(date +"%A, %b %d %Y %I:%M %Ss %Nns %p %Z %ss since epoch")
+# now_epoch=$(date +%s)
+
+# LOCKFILE # To use lockfile, run		:%s/# LOCKFILE[ \t]//
+# LOCKFILE # To clear lockfile, run	:%s/# LOCKFILE.*$//
+# LOCKFILE if [[ -e $lockfile ]]
+# LOCKFILE then 
+# LOCKFILE 	$logger syslog.warn "Lockfile exists: $(ls -l -A -h $lockfile 2> /dev/null)"
+# LOCKFILE 	exit
+# LOCKFILE fi
+# LOCKFILE touch $lockfile
+echo -e "Starting $scrpt [$now_readable]\n" > $stat_log
+
+# PRODUCTION
+# live_demo : a46dbb1a7e8e4685b1b0a96d53c0f4f0:a392bca98a3e45898556dae7b8d86d54
+printf 'app_key=a46dbb1a7e8e4685b1b0a96d53c0f4f0\napp_secret=a392bca98a3e45898556dae7b8d86d54\n' > ~/.bigdoor_bashkit.cnf
+#printf 'app_key=\napp_secret=\n' > ~/.bigdoor_bashkit.cnf
+#printf 'app_key=58eda42b0b9d4c0e9e8586d7b694ccea\napp_secret=0297923e6429401c80c460445de338e2\n' > ~/.bigdoor_bashkit.cnf
+#printf 'app_key=2b4dc207c6134351b4003fc20f573ac6\napp_secret=c7524a93d46d48499b865e65b00f44fc\n' > ~/.bigdoor_bashkit.cnf
+
+echo -e "\nFinished $scrpt [$(date +"%A, %b %d %Y %I:%M %Ss %Nns %p %Z %ss since epoch")]\n" >> $stat_log
+# Uncomment the next line to see a report on this script.
+# cat $stat_log|sed s/\\t/\\n/g
+# LOCKFILE [[ -e $lockfile ]] && rm $lockfile
+#! /bin/sh
+# BigDoor 
+# Q: what is this script for?
+# A: setup the environment for a demo 
+
+utilities="/var/www/ops.bigdoor.com/trunk/tools/util"
+if [[ -e $utilities ]]
+then
+	. $utilities
+else
+        logger -t BigDoor.unknownscript -p syslog.err "util script not found on $HOSTNAME"
+		printf "Error - utilities file not found \n"
+		exit
+fi
+echo -e "Starting $scrpt [$now_readable]\n" > $stat_log
+
+debug=1
+
+function get_object_ids_by_property()
+{
+	# $1 : entry point ; $2 : property key-val pair
+	#bashkit_path='/var/www/demo.bigdoor.com/trunk/volitile/bigdoorkits/bash'
+	bashkit_path='../../bigdoorkits/bash'
+	cd "$bashkit_path"
+	id=$(./bigdoor_id_by_property.sh "$1" 'max_records=10000' "$2")
+	cd - > /dev/null
+	printf "$id"
+}
+function create_unique_by_pub_title()
+{
+	property_pair='pub_title='"${2}"
+	# get the first id, if it exists
+	id=$(get_object_ids_by_property "${1}" "${property_pair}" |tr '\n' ' '|sed -e "s/ .*$//g")
+	if [[ ! -n $id ]] ; then
+		bashkit -m POST -e $1 -d "$3" > /dev/null
+		id=$(get_object_ids_by_property "${1}" "${property_pair}")
+	else
+		bashkit -m PUT -e ${1}/${id} -d "$3" > /dev/null
+	fi
+	printf "$id"
+}
+function get_meta_ids()
+{
+	meta_array=($1)
+	for meta in $(seq 0 $((${#meta_array[@]}-1)))
+	do
+		meta_key=$(printf "${meta_array[meta]}"|cut -d: -f1)
+		meta_object=$(printf "${meta_array[meta]}"|cut -d: -f2)
+		meta_pub_title=$(printf "${meta_array[meta]}"|cut -d: -f3)
+		meta_parameter=$(printf "${meta_array[meta]}"|cut -d: -f4)
+		meta_extra_key=$(printf "${meta_array[meta]}"|cut -d: -f5)
+		meta_extra_val=$(printf "${meta_array[meta]}"|cut -d: -f6)
+		meta_id=$(get_object_ids_by_property "$meta_object" 'pub_title='${meta_pub_title})
+		meta_objects="${meta_objects} ${meta_key}:${meta_parameter}:${meta_id}:${meta_extra_key}:${meta_extra_val}"
+	done
+	meta_array=($meta_objects)
+	for meta in $(seq 0 $((${#meta_array[@]}-1)))
+	do
+		meta_key=$(printf "${meta_array[meta]}"|cut -d: -f1)
+		meta_parameter=$(printf "${meta_array[meta]}"|cut -d: -f2)
+		meta_id=$(printf "${meta_array[meta]}"|cut -d: -f3)
+		meta_extra_key=$(printf "${meta_array[meta]}"|cut -d: -f4)
+		[[ $meta_key == 'depends_on' ]] && meta_depends="${meta_depends}${meta_parameter}=${meta_id}+"
+		[[ $meta_key == 'associate' ]] && meta_associate="${meta_associate}${meta_parameter}=${meta_id}+"
+		[[ $meta_key == 'associate' ]] && [[ -n $meta_extra_key ]] && meta_associate="${meta_associate}:${meta_extra_key}+"
+	done
+	meta_depends=$(printf "$meta_depends"|sed -e "s/+$//" -e "s/^ //" -e "s/ $//")
+	meta_associate=$(printf "$meta_associate"|sed -e "s/+$//" -e "s/^ //"  -e "s/ $//")
+	meta_objects=$(printf "$meta_objects"|sed -e "s/^ //" -e "s/ $//")
+	meta_objects="${meta_depends} ${meta_associate} ${meta_objects}"
+	printf "$meta_objects"
+}
+function create_associations()
+{
+	dest_associations=$(printf "$1"|sed -e "s/+:/:/g"|sed -e "s/+/ /g")
+	source_id=$2
+	source_endpoint=$3
+	ass_array=($dest_associations)
+	for ass in $(seq 0 $((${#ass_array[@]}-1)))
+	do
+		ass_endpoint=$(printf "${ass_array[ass]}"|cut -d'=' -f1)
+		ass_id=$(printf "${ass_array[ass]}"|cut -d'=' -f2|cut -d':' -f1)
+		ass_data=
+		printf "${ass_array[ass]}"|grep -q -o ':';
+		[[ $? -eq 0 ]] && ass_data=$(printf "${ass_array[ass]}"|cut -d':' -f2) && ass_data='-d '${ass_data}''
+		#[[ $ass_endpoint == 'named_transaction_group' ]] && ass_endpoint="${ass_endpoint}/${ass_id}/${source_endpoint}/${source_id}"
+		#[[ $ass_endpoint == 'attribute' ]] && ass_endpoint="${ass_endpoint}/${ass_id}/${source_endpoint}/${source_id}"
+		full_ass_endpoint="${ass_endpoint}/${ass_id}/${source_endpoint}/${source_id}"
+		bashkit -e "$full_ass_endpoint" > /dev/null
+		if [[ $? -ne 0 ]];then
+			bashkit -m POST -e $full_ass_endpoint $ass_data  > /dev/null
+		elif [[ $ass_endpoint == 'named_transaction_group' ]];then
+			bashkit -m PUT -e $full_ass_endpoint $ass_data  > /dev/null
+		fi
+	done
+}
+function create_children()
+{
+	child_endpoint=$1
+	child_title=$2
+	parent=$(printf "$3"|sed -e "s/_id\=/\//" )
+	parent=${parent}/${child_endpoint}
+	child_params=$4
+	property_pair='pub_title='"${child_title}"
+	# get the first id, if it exists
+	id=$(get_object_ids_by_property "${child_endpoint}" "${property_pair}" |tr '\n' ' '|sed -e "s/ .*$//g")
+	if [[ ! -n $id ]] ; then
+		bashkit -m POST -e $parent -d $child_params > /dev/null
+		id=$(get_object_ids_by_property "${child_endpoint}" "${property_pair}")
+	else
+		bashkit -m PUT -e $parent/$id -d $child_params > /dev/null
+	fi
+	printf "$id"
+}
+
+while read line
+do
+	key_val=$(printf "$line"|grep '^# CREATE .* :'|sed -e "s/^# CREATE //")
+	if [[ -n $key_val ]];then
+		meta=$(printf "$key_val"|grep -o "# META .*$"|sed -e "s/# META //")
+		key_val=$(printf "$key_val"|sed -e "s/ # META .*$//")
+		endpoint=$(printf "$key_val"|cut -d: -f1|sed -e "s/ $//")
+		pub_title=$(printf "$key_val"|grep -o 'pub_title=[^&]*'|cut -d\= -f2)
+		params=$(printf "$key_val"|cut -d: -f2|sed -e "s/^ //" -e 's/ $//')
+		[[ -n $meta ]] && meta=$(get_meta_ids "$meta") && dependencies=$(printf "$meta"|cut -d' ' -f1) 
+		[[ -n $meta ]] && associations=$(printf "$meta"|cut -d' ' -f2)
+		[[ -n $dependencies ]] && dependencies=$(echo $dependencies|sed -e "s/+/\&/g") && params="${params}&${dependencies}"
+		printf "$associations"|grep -o -q 'collection'
+		if [[ $? -ne 0 ]]; then
+			id=$(create_unique_by_pub_title $endpoint $pub_title "$params")
+			create_associations $associations $id $endpoint
+		else
+			parent_end_point=$(printf "$associations"|grep -o "^.*collection[^+]*")
+			child_associations=$(printf "$associations"|sed -e "s/^[^+]*.//")
+			id=$(create_children $endpoint $pub_title $parent_end_point $params)
+			create_associations $child_associations $id $endpoint
+		fi
+		objects="${objects} ${endpoint}:${pub_title}:${id}"
+		output_status info "${endpoint}:${pub_title}:${id}"
+	fi
+done < $1
+
+echo -e "\nFinished $scrpt [$(date +"%A, %b %d %Y %I:%M %Ss %Nns %p %Z %ss since epoch")]\n" >> $stat_log
+# Uncomment the next line to see a report on this script.
+# cat $stat_log|sed s/\\t/\\n/g
+# LOCKFILE [[ -e $lockfile ]] && rm $lockfile