Skip to content

Instantly share code, notes, and snippets.

@timburks
Last active April 20, 2023 03:41
Show Gist options
  • Save timburks/cb06dd9de202aec9be85cb9dfb761cd6 to your computer and use it in GitHub Desktop.
Save timburks/cb06dd9de202aec9be85cb9dfb761cd6 to your computer and use it in GitHub Desktop.
#!/bin/sh
# If PROJECT_ID is set, use it. If not, default to the value after the dash.
PROJECT_ID=${PROJECT_ID:-registry-eval}
TOKEN=`gcloud auth print-access-token`
# Delete the API and its child resources if it already exists.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample?force=true \
-X DELETE \
-H "Authorization: Bearer ${TOKEN}"
# Create an API.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis?api_id=sample \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d @- \
<<EOF
{
display_name: "sample",
description: "This is my sample API",
}
EOF
# Create a version.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions?api_version_id=v1 \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d @- \
<<EOF
{
display_name: "v1",
description: "Version 1"
}
EOF
SPECDATA=`base64 - <<EOF
openapi: 3.0
EOF`
echo $SPECDATA
# Create an API spec.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions/v1/specs?api_spec_id=openapi \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d @- \
<<EOF
{
filename: "openapi.yaml",
mime_type: "application/x.openapi;version=3.0.0",
contents: "${SPECDATA}",
}
EOF
# Get the API spec,
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions/v1/specs/openapi:getContents \
-X GET \
-H "Authorization: Bearer ${TOKEN}"
SPECDATA=`base64 - <<EOF
openapi: 3.0
info:
title: Sample
version: v1
EOF`
echo $SPECDATA
# Update the API spec.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions/v1/specs/openapi \
-X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d @- \
<<EOF
{
filename: "openapi.yaml",
mime_type: "application/x.openapi;version=3.0.0",
contents: "${SPECDATA}",
}
EOF
# Get the updated API spec.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions/v1/specs/openapi:getContents \
-X GET \
-H "Authorization: Bearer ${TOKEN}"
SPECDATA=`base64 - <<EOF
openapi: 3.0
info:
title: Sample
version: v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
EOF`
echo $SPECDATA
# Update the API spec again.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions/v1/specs/openapi \
-X PATCH \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
-d @- \
<<EOF
{
filename: "openapi.yaml",
mime_type: "application/x.openapi;version=3.0.0",
contents: "${SPECDATA}",
}
EOF
# Get the updated API spec.
curl https://apigeeregistry.googleapis.com/v1/projects/${PROJECT_ID}/locations/global/apis/sample/versions/v1/specs/openapi:getContents \
-X GET \
-H "Authorization: Bearer ${TOKEN}"
@timburks
Copy link
Author

Running the above script creates an API with a single version and a spec for that version that has three revisions (each of the two PATCH updates creates a new spec revision). The three revisions are listed in the HISTORY table of API hub:

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment