Skip to content

Instantly share code, notes, and snippets.

@martinell
Forked from davidarias/guide.sh
Created May 10, 2016
Embed
What would you like to do?
CraftAR M-API curl guide
#!/bin/bash
####### CraftAR M-API curl guide #######
# Some raw curl examples of Managmenet API usage
# NOT INTENDED FOR PRODUCTION CODE, use native libraries when posible
# like CraftAR-PHP or CraftAR-Python.
echo "To run this script you need python and the command line tool jq!!"
echo $'\n'
# Use your own api key!
APIKEY="YOUR_API_KEY"
HOST="https://my.craftar.net"
# some functions to parse results
function get_headers {
local resposne=${1}
echo "$response" | sed "/^\s*$(printf '\r')*$/q"
}
function get_body {
local resposne=${1}
echo "$response" | sed "1,/^\s*$(printf '\r')*$/d"
}
function get_json_field {
local field=${1}
local json_response=${2}
echo $json_response | jq -r ".$field"
}
##############################
# Create a Cloud Collection ##
##############################
collection_name="My Collection"
echo "Creating a cloud Collection named $collection_name"
echo "--------------------------------------------------"
echo $'\n'
# encode the data required for collection creation in json
data='{"name":"'"$collection_name"'"}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/collection/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# later we'll use this to create an item in this collection
collection_resource_uri=$(get_json_field "resource_uri" "$body")
# echo to stout the coammand executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
##########################################
# Create an Image Recognition (IR) item ##
##########################################
item_name="My Item"
echo "Creating a Image Recognition (IR) Item named $item_name"
echo "---------------------------------"
echo $'\n'
# encode the data required for item creation in json
data='{"name":"'"$item_name"'", "collection": "'"$collection_resource_uri"'"}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/item/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# later we'll use this to create an image in this item
item_resource_uri=$(get_json_field "resource_uri" "$body")
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
######################
# Create Image ##
######################
filename="myimage.jpg"
echo "Creating an Image usign file $filename"
echo "--------------------------------------"
echo $'\n'
# curl command.
# Note that we are no usign json content-type.
# images must be created using multipart/form-data encoding
cmd="curl -si -X POST '$HOST/api/v0/image/?api_key=$APIKEY' -F 'item=$item_resource_uri' -F 'file=@$filename'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
################################
# Create On-Device Collection ##
################################
echo $'\n'
collection_name="My On-Device Collection"
echo "Creating an On-Device Collection named $collection_name"
echo "--------------------------------------------------"
echo $'\n'
# encode the data required for collection creation in json.
# Note the 'offline' parameter to mark this collection as On-Device
data='{"name":"'"$collection_name"'", "offline": true}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/collection/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# later we'll use this to create an item in this collection
collection_resource_uri=$(get_json_field "resource_uri" "$body")
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
###########################################
# Create an Augmented Reallity (AR) item ##
###########################################
item_name="My Item"
echo "Creating an Augmented Reallity (AR) Item named $item_name"
echo "---------------------------------"
echo $'\n'
# Note the 'trackable' parameter to mark this
# item as an Augmented Reallity Item. Also have in mind that the scenes of AR are described in a json
# in the content field. Please check the format or use an existing scene as a template.
data='{"name":"'"$item_name"'", "collection": "'"$collection_resource_uri"'", "trackable": true}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/item/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# later we'll use this to create an image in this item
item_resource_uri=$(get_json_field "resource_uri" "$body")
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
################################################
# Associate an Application ID to a collection ##
################################################
# In this example we are using the previous On-Device Collection created,
# but it will work the same with cloud collections
appid_name="com.testing.mapi"
appid_description="My AppID"
echo "Associate the Application ID $appid_name to collection $collection_name"
echo "---------------------------------"
echo $'\n'
# requried parameters
data='{"name":"'"$appid_name"'", "collection": "'"$collection_resource_uri"'"}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/app/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# later we'll use this to create an On-Device Bundle
appid_resource_uri=$(get_json_field "resource_uri" "$body")
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
############################################
# Obtain SDK Version for On-Device Bundle ##
############################################
# sdk version parameters
sdk_version="4"
sdk_name="ARSDK"
echo "Obtain SDK version"
echo "---------------------------------"
echo $'\n'
# note the extra GET parameters sdk_version and sdk_name for filtering SDK Versions
cmd="curl -si -H 'Content-Type: application/json' '$HOST/api/v0/version/?api_key=$APIKEY&sdk_version=$sdk_version&sdk_name=$sdk_name'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# later we'll use this to create an On-Device Bundle
version_resource_uri=$(echo $body | jq '.objects | .[0] | .resource_uri')
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
############################
# Create On-Device Bundle ##
############################
echo "Create Collection bundle for $appid_name in collection $collection_name"
echo "---------------------------------"
echo $'\n'
data='{"app":"'"$appid_resource_uri"'", "collection": "'"$collection_resource_uri"'", "version": '"$version_resource_uri"'}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/collectionbundle/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
echo "If you don't have the Augmented Reallity SDK v4 this call will probably fail"
echo $'\n'
############################################################
# Obtain the development Application ID com.catchoom.test ##
############################################################
# sdk version parameters
dev_appid="com.catchoom.test"
echo "Obtain the development Application ID $dev_appid"
echo "---------------------------------"
echo $'\n'
cmd="curl -si -H 'Content-Type: application/json' '$HOST/api/v0/app/?api_key=$APIKEY&name=$dev_appid'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# obtain the appid resource uri
appid_resource_uri=$(echo $body | jq '.objects | .[0] | .resource_uri')
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
################################################
# Create On-Device Bundle with the dev Appid ##
################################################
echo "Create Collection bundle for $dev_appid in collection $collection_name"
echo "---------------------------------"
echo $'\n'
data='{"app":'"$appid_resource_uri"', "collection": "'"$collection_resource_uri"'", "version": '"$version_resource_uri"'}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/collectionbundle/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
####################################################
# Regenerate On-Device Bundle with the dev Appid ##
####################################################
# just use the same call as create
echo "Regenerate Collection bundle for $dev_appid in collection $collection_name"
echo "---------------------------------"
echo $'\n'
data='{"app":'"$appid_resource_uri"', "collection": "'"$collection_resource_uri"'", "version": '"$version_resource_uri"'}'
#curl command
cmd="curl -si -X POST -H 'Content-Type: application/json' '$HOST/api/v0/collectionbundle/?api_key=$APIKEY' -d '$data'"
#eval command
response=$(eval $cmd)
#parse results
headers=$(get_headers $response)
body=$(get_body $response)
# echo to stout the command executed and the response from the server
echo "CURL COMMAND:"
echo $cmd; echo $'\n'
echo "RESPONSE FROM SERVER:"
echo "$headers"; echo $body | python -m json.tool
echo $'\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment