diff --git a/cmd/registry/cmd/compute/vocabulary/vocabulary.go b/cmd/registry/cmd/compute/vocabulary/vocabulary.go index da6a48db0..e962d4ad7 100644 --- a/cmd/registry/cmd/compute/vocabulary/vocabulary.go +++ b/cmd/registry/cmd/compute/vocabulary/vocabulary.go @@ -18,6 +18,7 @@ import ( "context" "fmt" + "github.com/apigee/registry/cmd/registry/compress" "github.com/apigee/registry/cmd/registry/tasks" "github.com/apigee/registry/pkg/connection" "github.com/apigee/registry/pkg/log" @@ -27,6 +28,7 @@ import ( "github.com/apigee/registry/rpc" "github.com/google/gnostic/metrics/vocabulary" "github.com/spf13/cobra" + "google.golang.org/grpc/metadata" "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/proto" @@ -117,12 +119,19 @@ func (task *computeVocabularyTask) String() string { } func (task *computeVocabularyTask) Run(ctx context.Context) error { + ctx = metadata.AppendToOutgoingContext(ctx, "accept-encoding", "gzip") contents, err := task.client.GetApiSpecContents(ctx, &rpc.GetApiSpecContentsRequest{ Name: task.specName, }) if err != nil { return err } + if mime.IsGZipCompressed(contents.ContentType) { + contents.Data, err = compress.GUnzippedBytes(contents.Data) + if err != nil { + return err + } + } log.Debugf(ctx, "Computing %s/artifacts/vocabulary", task.specName) var vocab *metrics.Vocabulary @@ -155,7 +164,7 @@ func (task *computeVocabularyTask) Run(ctx context.Context) error { return nil } } else { - return fmt.Errorf("we don't know how to summarize %s", task.specName) + return fmt.Errorf("we don't know how to compute the vocabulary of %s", task.specName) } if task.dryRun { diff --git a/pkg/visitor/fetch_test.go b/pkg/visitor/fetch_test.go index c036ba687..2930f2bff 100644 --- a/pkg/visitor/fetch_test.go +++ b/pkg/visitor/fetch_test.go @@ -15,9 +15,11 @@ package visitor import ( + "bytes" "context" "testing" + "github.com/apigee/registry/cmd/registry/compress" "github.com/apigee/registry/pkg/connection/grpctest" "github.com/apigee/registry/pkg/names" "github.com/apigee/registry/rpc" @@ -29,6 +31,10 @@ func TestFetch(t *testing.T) { parent := project.String() + "/locations/global" specContents := "hello" + gzippedSpecContents, err := compress.GZippedBytes([]byte(specContents)) + if err != nil { + t.Fatalf("Setup: Failed to compress test data: %s", err) + } artifactContents := "hello" ctx := context.Background() @@ -62,8 +68,8 @@ func TestFetch(t *testing.T) { ApiSpecId: "s", Parent: versionName.String(), ApiSpec: &rpc.ApiSpec{ - Contents: []byte(specContents), - MimeType: "text/plain", + Contents: gzippedSpecContents, + MimeType: "text/plain+gzip", }, }) if err != nil { @@ -91,7 +97,7 @@ func TestFetch(t *testing.T) { if err != nil { t.Fatalf("Failed to fetch spec contents: %s", err) } - if string(spec.Contents) != specContents { + if !bytes.Equal(spec.Contents, gzippedSpecContents) { t.Fatalf("Fetched unexpected spec contents: wanted %q got %q", specContents, spec.Contents) } }) @@ -111,7 +117,7 @@ func TestFetch(t *testing.T) { t.Fatalf("Failed to parse spec name: %s", err) } err = GetSpec(ctx, registryClient, specName, true, func(ctx context.Context, spec *rpc.ApiSpec) error { - if string(spec.Contents) != specContents { + if !bytes.Equal(spec.Contents, gzippedSpecContents) { t.Fatalf("Fetched unexpected spec contents: wanted %q got %q", specContents, spec.Contents) } return nil @@ -146,7 +152,7 @@ func TestFetch(t *testing.T) { t.Fatalf("Failed to parse spec revision name: %s", err) } err = GetSpecRevision(ctx, registryClient, specRevisionName, true, func(ctx context.Context, spec *rpc.ApiSpec) error { - if string(spec.Contents) != specContents { + if !bytes.Equal(spec.Contents, gzippedSpecContents) { t.Fatalf("Fetched unexpected spec contents: wanted %q got %q", specContents, spec.Contents) } return nil diff --git a/pkg/visitor/list.go b/pkg/visitor/list.go index a2a39e157..eb0907bd6 100644 --- a/pkg/visitor/list.go +++ b/pkg/visitor/list.go @@ -18,11 +18,14 @@ import ( "context" "fmt" + "github.com/apigee/registry/cmd/registry/compress" "github.com/apigee/registry/gapic" + "github.com/apigee/registry/pkg/mime" "github.com/apigee/registry/pkg/names" "github.com/apigee/registry/rpc" "google.golang.org/api/iterator" "google.golang.org/grpc/codes" + "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" ) @@ -186,6 +189,7 @@ func ListSpecs(ctx context.Context, } if getContents { + ctx = metadata.AppendToOutgoingContext(ctx, "accept-encoding", "gzip") resp, err := client.GetApiSpecContents(ctx, &rpc.GetApiSpecContentsRequest{ Name: r.GetName(), }) @@ -193,6 +197,12 @@ func ListSpecs(ctx context.Context, return err } r.Contents = resp.GetData() + if mime.IsGZipCompressed(resp.ContentType) { + r.Contents, err = compress.GUnzippedBytes(r.Contents) + if err != nil { + return err + } + } } if err := handler(ctx, r); err != nil { @@ -217,6 +227,7 @@ func ListSpecRevisions(ctx context.Context, } if getContents { + ctx = metadata.AppendToOutgoingContext(ctx, "accept-encoding", "gzip") resp, err := client.GetApiSpecContents(ctx, &rpc.GetApiSpecContentsRequest{ Name: r.GetName(), }) @@ -224,6 +235,12 @@ func ListSpecRevisions(ctx context.Context, return err } r.Contents = resp.GetData() + if mime.IsGZipCompressed(resp.ContentType) { + r.Contents, err = compress.GUnzippedBytes(r.Contents) + if err != nil { + return err + } + } } if err := handler(ctx, r); err != nil {