#!/usr/bin/env bash set -euo pipefail for required in OUTRESERVE_SUPPLY_BASE_URL OUTRESERVE_SUPPLY_API_KEY OUTRESERVE_EXTERNAL_PROPERTY_ID OUTRESERVE_COUNTRY_CODE OUTRESERVE_STATE_CODE OUTRESERVE_DESTINATION_SLUG OUTRESERVE_PROPERTY_TIMEZONE; do if [[ -z "${!required:-}" ]]; then echo "Set $required" >&2; exit 2; fi done if [[ "${OUTRESERVE_CONFIRM_CREATE:-}" != "yes" ]]; then echo 'Set OUTRESERVE_CONFIRM_CREATE=yes to create a pending property and unit' >&2 exit 2 fi if ! command -v jq >/dev/null; then echo 'Install jq to run this cURL example' >&2; exit 2; fi base="${OUTRESERVE_SUPPLY_BASE_URL%/}" auth="Authorization: Bearer ${OUTRESERVE_SUPPLY_API_KEY}" property_key="${OUTRESERVE_PROPERTY_IDEMPOTENCY_KEY:-$(openssl rand -hex 16)}" unit_key="${OUTRESERVE_UNIT_IDEMPOTENCY_KEY:-$(openssl rand -hex 16)}" submit_key="${OUTRESERVE_SUBMIT_IDEMPOTENCY_KEY:-$(openssl rand -hex 16)}" echo "Persist these keys before any retry: property=$property_key unit=$unit_key submit=$submit_key" >&2 curl --fail-with-body --silent --show-error -H "$auth" "$base/connection" | jq '.data | {id,status,capabilities,supported_unit_types,supported_currencies}' location_response="$(curl --fail-with-body --silent --show-error -G "$base/locations" -H "$auth" \ --data-urlencode 'level=destinations' --data-urlencode "country_code=$OUTRESERVE_COUNTRY_CODE" \ --data-urlencode "state_code=$OUTRESERVE_STATE_CODE" \ --data-urlencode "destination_slug=$OUTRESERVE_DESTINATION_SLUG")" if [[ "$(jq '.data | length' <<<"$location_response")" != "1" ]]; then echo 'Expected exactly one matching destination; inspect GET /locations' >&2 exit 1 fi echo "$location_response" | jq '.data[0] | {id,name,status}' property_body="$(jq -n --arg external "$OUTRESERVE_EXTERNAL_PROPERTY_ID" --arg country "$OUTRESERVE_COUNTRY_CODE" \ --arg state "$OUTRESERVE_STATE_CODE" --arg slug "$OUTRESERVE_DESTINATION_SLUG" \ --arg timezone "$OUTRESERVE_PROPERTY_TIMEZONE" \ '{property:{external_property_id:$external,name:"Example Cabin Property",currency:"USD",country_code:$country,state_code:$state,destination_slug:$slug,timezone:$timezone}}')" property_response="$(curl --fail-with-body --silent --show-error -X POST "$base/properties" -H "$auth" -H 'Content-Type: application/json' -H "Idempotency-Key: $property_key" --data "$property_body")" property_id="$(jq -r '.data.id // empty' <<<"$property_response")" if [[ -z "$property_id" ]]; then echo 'Property response omitted data.id' >&2; exit 1; fi echo "$property_response" | jq '{data: .data | {id,external_property_id,approval_status,booking_ready}}' unit_body="$(jq -n --arg external "${OUTRESERVE_EXTERNAL_UNIT_ID:-sample-cabin-1}" \ '{unit:{external_unit_id:$external,name:"Cabin 1",unit_type:"Cabin",inventory_mode:"physical",capacity:1}}')" curl --fail-with-body --silent --show-error -X POST "$base/properties/$property_id/units" -H "$auth" -H 'Content-Type: application/json' -H "Idempotency-Key: $unit_key" --data "$unit_body" | jq '{data: .data | {id,external_unit_id,unit_type,inventory_mode,capacity}}' curl --fail-with-body --silent --show-error -X POST "$base/properties/$property_id/submit" -H "$auth" -H "Idempotency-Key: $submit_key" | jq '{data: .data | {id,approval_status,booking_ready}}' echo "Pending property UUID: $property_id"