From fa01482648c93d7a57b5f7159a995a6b13e4f941 Mon Sep 17 00:00:00 2001 From: Brad Miro Date: Tue, 26 Nov 2019 14:16:28 -0500 Subject: [PATCH] feat(samples): add sample of configuring alternate apiEndpoint (#473) --- README.md | 1 + linkinator.config.json | 3 ++- samples/README.md | 17 ++++++++++++ samples/setEndpoint.js | 36 +++++++++++++++++++++++++ samples/system-test/setEndpoint.test.js | 28 +++++++++++++++++++ 5 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 samples/setEndpoint.js create mode 100644 samples/system-test/setEndpoint.test.js diff --git a/README.md b/README.md index a67d392d..27b41e31 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,7 @@ has instructions for running the samples. | Detect.v1p3beta1 | [source code](https://github.com/googleapis/nodejs-vision/blob/master/samples/detect.v1p3beta1.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-vision&page=editor&open_in_editor=samples/detect.v1p3beta1.js,samples/README.md) | | Face Detection | [source code](https://github.com/googleapis/nodejs-vision/blob/master/samples/faceDetection.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-vision&page=editor&open_in_editor=samples/faceDetection.js,samples/README.md) | | Quickstart | [source code](https://github.com/googleapis/nodejs-vision/blob/master/samples/quickstart.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-vision&page=editor&open_in_editor=samples/quickstart.js,samples/README.md) | +| Set Endpoint | [source code](https://github.com/googleapis/nodejs-vision/blob/master/samples/setEndpoint.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-vision&page=editor&open_in_editor=samples/setEndpoint.js,samples/README.md) | | Text Detection | [source code](https://github.com/googleapis/nodejs-vision/blob/master/samples/textDetection.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-vision&page=editor&open_in_editor=samples/textDetection.js,samples/README.md) | diff --git a/linkinator.config.json b/linkinator.config.json index d780d6bf..cad97afd 100644 --- a/linkinator.config.json +++ b/linkinator.config.json @@ -2,6 +2,7 @@ "recurse": true, "skip": [ "https://codecov.io/gh/googleapis/", - "www.googleapis.com" + "www.googleapis.com", + "setEndpoint.js" ] } diff --git a/samples/README.md b/samples/README.md index 67a854b8..8c8cdf5a 100644 --- a/samples/README.md +++ b/samples/README.md @@ -17,6 +17,7 @@ * [Detect.v1p3beta1](#detect.v1p3beta1) * [Face Detection](#face-detection) * [Quickstart](#quickstart) + * [Set Endpoint](#set-endpoint) * [Text Detection](#text-detection) ## Before you begin @@ -113,6 +114,22 @@ __Usage:__ +### Set Endpoint + +View the [source code]https://github.com/googleapis/nodejs-vision/blob/master/samples/setEndpoint.js) + +[![Open in Cloud shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-vision&page=editor&open_in_editor=samples/setEndpoint.js,samples/README.md) + +__Usage:__ + +`node setEndpoint.js` + +----- + + + + + ### Text Detection View the [source code](https://github.com/googleapis/nodejs-vision/blob/master/samples/textDetection.js). diff --git a/samples/setEndpoint.js b/samples/setEndpoint.js new file mode 100644 index 00000000..42d90bd3 --- /dev/null +++ b/samples/setEndpoint.js @@ -0,0 +1,36 @@ +// 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. + +'use strict'; + +async function setEndpoint() { + // [START vision_set_endpoint] + // Imports the Google Cloud client library + const vision = require('@google-cloud/vision'); + + // Specifies the location of the api endpoint + const clientOptions = {apiEndpoint: 'eu-vision.googleapis.com'}; + + // Creates a client + const client = new vision.ImageAnnotatorClient(clientOptions); + // [END vision_set_endpoint] + + // Performs label detection on the image file + const [result] = await client.labelDetection('./resources/wakeupcat.jpg'); + const labels = result.labelAnnotations; + console.log('Labels:'); + labels.forEach(label => console.log(label.description)); +} + +setEndpoint().catch(console.error); diff --git a/samples/system-test/setEndpoint.test.js b/samples/system-test/setEndpoint.test.js new file mode 100644 index 00000000..3c7aa2a7 --- /dev/null +++ b/samples/system-test/setEndpoint.test.js @@ -0,0 +1,28 @@ +// 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. + +'use strict'; + +const {assert} = require('chai'); +const cp = require('child_process'); + +const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); + +describe(`set endpoint for vision api call`, () => { + it(`should detect labels in a remote file from a pre-set api endpoint`, async () => { + const stdout = execSync('node setEndpoint.js'); + assert.match(stdout, /Labels:/); + assert.match(stdout, /cat/); + }); +});