-
Notifications
You must be signed in to change notification settings - Fork 1.8k
vision: add set_endpoint sample #1113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tbpg
merged 3 commits into
GoogleCloudPlatform:master
from
davidcavazos:vision_set_endpoint
Dec 23, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.)