Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions vision/detect/set_endpoint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

// [START vision_set_endpoint]
import (
"context"
"fmt"

vision "cloud.google.com/go/vision/apiv1"
"google.golang.org/api/option"
)

// setEndpoint changes your endpoint.
func setEndpoint(endpoint string) error {
// endpoint := "eu-vision.googleapis.com:443"

ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
if err != nil {
return fmt.Errorf("NewImageAnnotatorClient: %v", err)
}
defer client.Close()

return nil
}

// [END vision_set_endpoint]
61 changes: 61 additions & 0 deletions vision/detect/set_endpoint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"context"
"strings"
"testing"

vision "cloud.google.com/go/vision/apiv1"
"google.golang.org/api/option"
visionpb "google.golang.org/genproto/googleapis/cloud/vision/v1"
)

func TestSetEndpoint(t *testing.T) {
const endpoint = "eu-vision.googleapis.com:443"

// Run the code sample to check for errors.
err := setEndpoint(endpoint)
if err != nil {
t.Fatalf("setEndpoint: %v", err)
}

// Since we're not returning the client from the code sample, we create an equivalent client here.
ctx := context.Background()
client, err := vision.NewImageAnnotatorClient(ctx, option.WithEndpoint(endpoint))
if err != nil {
t.Fatalf("NewImageAnnotatorClient: %v", err)
}
defer client.Close()

image := &visionpb.Image{
Source: &visionpb.ImageSource{
GcsImageUri: "gs://cloud-samples-data/vision/text/screen.jpg",
},
}
texts, err := client.DetectTexts(ctx, image, nil, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the output of this change based on the endpoint? Is there a more direct way we can test the endpoint was actually set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the output changes, this is what they're doing in the canonical

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nnegrey do you know if there is a more direct way to test if the endpoint was actually used? Or is it completely transparent for users?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I know of for this API, you can pass an invalid endpoint and get an error. But no resource is created.

Natural Language for example has a different set of endpoints they support as documented here: https://cloud.google.com/natural-language/docs/locations

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we remove this portion of the test if it's not testing the sample? Or is it needed to make sure it still works after changing the endpoint?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly to just verify the endpoint works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbpg how do you feel about this? Would you still want to change anything or should we go ahead and merge?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me.

Copy link
Contributor

@broady broady Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the test, you could set the endpoint to a dummy gRPC server.

(But I think this is good enough.)

if err != nil {
t.Fatalf("DetectTexts: %v", err)
}

text := texts[0]
if got, want := text.GetDescription(), "System"; !strings.Contains(got, want) {
t.Errorf("text.GetDescription() got:\n----\n%s----\nWant to contain:\n----\n%s\n----", got, want)
}
if len(text.GetBoundingPoly().GetVertices()) == 0 {
t.Errorf("text.GetBoundingPoly().getVertices() must have at least one vertex")
}
}