Skip to content

Commit 87f9b23

Browse files
nnegreybradmiro
authored andcommitted
Set Endpoint Samples (#1644)
* Set Endpoint Samples * Update pom.xml
1 parent b75a860 commit 87f9b23

File tree

5 files changed

+303
-0
lines changed

5 files changed

+303
-0
lines changed

automl/beta/pom.xml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!--
2+
Copyright 2019 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
-->
13+
<project>
14+
<modelVersion>4.0.0</modelVersion>
15+
<groupId>com.example.automl</groupId>
16+
<artifactId>automl-google-cloud-samples</artifactId>
17+
<packaging>jar</packaging>
18+
19+
<!--
20+
The parent pom defines common style checks and testing strategies for our samples.
21+
Removing or replacing it should not affect the execution of the samples in anyway.
22+
-->
23+
<parent>
24+
<groupId>com.google.cloud.samples</groupId>
25+
<artifactId>shared-configuration</artifactId>
26+
<version>1.0.11</version>
27+
</parent>
28+
29+
<properties>
30+
<maven.compiler.target>1.11</maven.compiler.target>
31+
<maven.compiler.source>1.11</maven.compiler.source>
32+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
33+
</properties>
34+
35+
<dependencies>
36+
<!-- [START automl_java_dependencies] -->
37+
<dependency>
38+
<groupId>com.google.cloud</groupId>
39+
<artifactId>google-cloud-automl</artifactId>
40+
<version>0.114.0-beta</version>
41+
</dependency>
42+
<!-- [END automl_java_dependencies] -->
43+
<dependency>
44+
<groupId>com.google.cloud</groupId>
45+
<artifactId>google-cloud-storage</artifactId>
46+
<version>1.83.0</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>net.sourceforge.argparse4j</groupId>
50+
<artifactId>argparse4j</artifactId>
51+
<version>0.8.1</version>
52+
</dependency>
53+
54+
<!-- Test dependencies -->
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.12</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>com.google.truth</groupId>
63+
<artifactId>truth</artifactId>
64+
<version>0.41</version>
65+
<scope>test</scope>
66+
</dependency>
67+
</dependencies>
68+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.automl;
18+
19+
import com.google.cloud.automl.v1beta1.AutoMlClient;
20+
import com.google.cloud.automl.v1beta1.AutoMlSettings;
21+
import com.google.cloud.automl.v1beta1.Dataset;
22+
import com.google.cloud.automl.v1beta1.ListDatasetsRequest;
23+
import com.google.cloud.automl.v1beta1.LocationName;
24+
25+
import java.io.IOException;
26+
27+
class SetEndpoint {
28+
29+
// Change your endpoint
30+
static void setEndpoint(String projectId) throws IOException {
31+
// [START automl_set_endpoint]
32+
AutoMlSettings settings = AutoMlSettings.newBuilder()
33+
.setEndpoint("eu-automl.googleapis.com:443")
34+
.build();
35+
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests. After completing all of your requests, call
38+
// the "close" method on the client to safely clean up any remaining background resources.
39+
AutoMlClient client = AutoMlClient.create(settings);
40+
41+
// A resource that represents Google Cloud Platform location.
42+
LocationName projectLocation = LocationName.of(projectId, "eu");
43+
// [END automl_set_endpoint]
44+
45+
ListDatasetsRequest request =
46+
ListDatasetsRequest.newBuilder()
47+
.setParent(projectLocation.toString())
48+
.setFilter("translation_dataset_metadata:*")
49+
.build();
50+
// List all the datasets available
51+
System.out.println("List of datasets:");
52+
for (Dataset dataset : client.listDatasets(request).iterateAll()) {
53+
System.out.println(dataset);
54+
}
55+
client.close();
56+
}
57+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.automl;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Tests for Automl Set Endpoint */
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class SetEndpointIT {
35+
36+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
37+
private ByteArrayOutputStream bout;
38+
private PrintStream out;
39+
40+
41+
@Before
42+
public void setUp() {
43+
bout = new ByteArrayOutputStream();
44+
out = new PrintStream(bout);
45+
System.setOut(out);
46+
}
47+
48+
@After
49+
public void tearDown() {
50+
System.setOut(null);
51+
}
52+
53+
54+
@Test
55+
public void testSetEndpoint() throws IOException {
56+
// Act
57+
SetEndpoint.setEndpoint(PROJECT_ID);
58+
59+
// Assert
60+
String got = bout.toString();
61+
assertThat(got).contains("display_name: \"do_not_delete_me_eu\"");
62+
}
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.language;
18+
19+
import com.google.cloud.language.v1.Document;
20+
import com.google.cloud.language.v1.LanguageServiceClient;
21+
import com.google.cloud.language.v1.LanguageServiceSettings;
22+
import com.google.cloud.language.v1.Sentiment;
23+
24+
import java.io.IOException;
25+
26+
class SetEndpoint {
27+
28+
// Change your endpoint
29+
static void setEndpoint() throws IOException {
30+
// [START language_set_endpoint]
31+
LanguageServiceSettings settings = LanguageServiceSettings.newBuilder()
32+
.setEndpoint("eu-language.googleapis.com:443")
33+
.build();
34+
35+
// Initialize client that will be used to send requests. This client only needs to be created
36+
// once, and can be reused for multiple requests. After completing all of your requests, call
37+
// the "close" method on the client to safely clean up any remaining background resources.
38+
LanguageServiceClient client = LanguageServiceClient.create(settings);
39+
// [END language_set_endpoint]
40+
41+
// The text to analyze
42+
String text = "Hello, world!";
43+
Document doc = Document.newBuilder()
44+
.setContent(text).setType(Document.Type.PLAIN_TEXT).build();
45+
46+
// Detects the sentiment of the text
47+
Sentiment sentiment = client.analyzeSentiment(doc).getDocumentSentiment();
48+
49+
System.out.printf("Text: %s%n", text);
50+
System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude());
51+
client.close();
52+
}
53+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.language;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
25+
import org.junit.After;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
import org.junit.runner.RunWith;
29+
import org.junit.runners.JUnit4;
30+
31+
/** Tests for Natural Language Set Endpoint */
32+
@RunWith(JUnit4.class)
33+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
34+
public class SetEndpointIT {
35+
36+
private ByteArrayOutputStream bout;
37+
private PrintStream out;
38+
39+
40+
@Before
41+
public void setUp() {
42+
bout = new ByteArrayOutputStream();
43+
out = new PrintStream(bout);
44+
System.setOut(out);
45+
}
46+
47+
@After
48+
public void tearDown() {
49+
System.setOut(null);
50+
}
51+
52+
53+
@Test
54+
public void testSetEndpoint() throws IOException {
55+
// Act
56+
SetEndpoint.setEndpoint();
57+
58+
// Assert
59+
String got = bout.toString();
60+
assertThat(got).contains("Sentiment");
61+
}
62+
}

0 commit comments

Comments
 (0)