Skip to content

Commit 7dff76b

Browse files
feat(modelarmor): refactor code
1 parent 22f1b67 commit 7dff76b

File tree

6 files changed

+160
-2
lines changed

6 files changed

+160
-2
lines changed

modelarmor/src/main/java/modelarmor/CreateTemplate.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package modelarmor;
1818

19+
// [START modelarmor_create_template]
20+
1921
import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
2022
import com.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
2123
import com.google.cloud.modelarmor.v1.FilterConfig;
@@ -29,8 +31,15 @@
2931
import com.google.protobuf.util.JsonFormat;
3032
import java.util.List;
3133

34+
/** This class contains a main method that creates a template using the Model Armor API. */
3235
public class CreateTemplate {
3336

37+
/**
38+
* Main method that calls the createTemplate method to create a template.
39+
*
40+
* @param args command line arguments (not used)
41+
* @throws Exception if an error occurs during template creation
42+
*/
3443
public static void main(String[] args) throws Exception {
3544
// TODO(developer): Replace these variables before running the sample.
3645
String projectId = "your-project-id";
@@ -40,15 +49,29 @@ public static void main(String[] args) throws Exception {
4049
createTemplate(projectId, locationId, templateId);
4150
}
4251

52+
/**
53+
* Creates a template using the Model Armor API.
54+
*
55+
* @param projectId the ID of the project
56+
* @param locationId the ID of the location
57+
* @param templateId the ID of the template
58+
* @return the created template
59+
* @throws Exception if an error occurs during template creation
60+
*/
4361
public static Template createTemplate(String projectId, String locationId, String templateId)
4462
throws Exception {
63+
// Construct the API endpoint URL
4564
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
65+
66+
// Create a Model Armor settings object with the API endpoint
4667
ModelArmorSettings modelArmorSettings =
4768
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
4869

4970
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
71+
// Construct the parent resource name
5072
String parent = LocationName.of(projectId, locationId).toString();
5173

74+
// Create a template object with a filter config
5275
Template template =
5376
Template.newBuilder()
5477
.setFilterConfig(
@@ -65,16 +88,23 @@ public static Template createTemplate(String projectId, String locationId, Strin
6588
.build())
6689
.build();
6790

91+
// Create a create template request object
6892
CreateTemplateRequest request =
6993
CreateTemplateRequest.newBuilder()
7094
.setParent(parent)
7195
.setTemplateId(templateId)
7296
.setTemplate(template)
7397
.build();
7498

99+
// Create the template using the Model Armor client
75100
Template createdTemplate = client.createTemplate(request);
101+
102+
// Print the created template
76103
System.out.println("Created template: " + JsonFormat.printer().print(createdTemplate));
104+
77105
return createdTemplate;
78106
}
79107
}
80108
}
109+
110+
// [END modelarmor_create_template]

modelarmor/src/main/java/modelarmor/DeleteTemplate.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616

1717
package modelarmor;
1818

19+
// [START modelarmor_delete_template]
20+
1921
import com.google.cloud.modelarmor.v1.ModelArmorClient;
2022
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
2123
import com.google.cloud.modelarmor.v1.TemplateName;
2224

25+
/** This class contains a main method that deletes a template using the Model Armor API. */
2326
public class DeleteTemplate {
2427

28+
/**
29+
* Main method that calls the deleteTemplate method to delete a template.
30+
*
31+
* @param args command line arguments (not used)
32+
* @throws Exception if an error occurs during template deletion
33+
*/
2534
public static void main(String[] args) throws Exception {
2635
// TODO(developer): Replace these variables before running the sample.
2736
String projectId = "your-project-id";
@@ -31,15 +40,34 @@ public static void main(String[] args) throws Exception {
3140
deleteTemplate(projectId, locationId, templateId);
3241
}
3342

43+
/**
44+
* Deletes a template using the Model Armor API.
45+
*
46+
* @param projectId the ID of the project
47+
* @param locationId the ID of the location
48+
* @param templateId the ID of the template
49+
* @throws Exception if an error occurs during template deletion
50+
*/
3451
public static void deleteTemplate(String projectId, String locationId, String templateId)
3552
throws Exception {
53+
// Construct the API endpoint URL
3654
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
55+
56+
// Create a Model Armor settings object with the API endpoint
3757
ModelArmorSettings modelArmorSettings =
3858
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
59+
3960
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
61+
// Construct the template name
4062
String name = TemplateName.of(projectId, locationId, templateId).toString();
63+
64+
// Delete the template using the Model Armor client
4165
client.deleteTemplate(name);
66+
67+
// Print a success message
4268
System.out.println("Deleted template: " + name);
4369
}
4470
}
4571
}
72+
73+
// [END modelarmor_delete_template]

modelarmor/src/main/java/modelarmor/GetTemplate.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,28 @@
1616

1717
package modelarmor;
1818

19+
/**
20+
* This class demonstrates how to retrieve a template using the Model Armor API.
21+
*
22+
* @author [Your Name]
23+
*/
1924
import com.google.cloud.modelarmor.v1.ModelArmorClient;
2025
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
2126
import com.google.cloud.modelarmor.v1.Template;
2227
import com.google.cloud.modelarmor.v1.TemplateName;
2328
import com.google.protobuf.util.JsonFormat;
2429

30+
// [START modelarmor_get_template]
31+
32+
/** This class contains a main method that retrieves a template using the Model Armor API. */
2533
public class GetTemplate {
2634

35+
/**
36+
* Main method that calls the getTemplate method to retrieve a template.
37+
*
38+
* @param args command line arguments (not used)
39+
* @throws Exception if an error occurs during template retrieval
40+
*/
2741
public static void main(String[] args) throws Exception {
2842
// TODO(developer): Replace these variables before running the sample.
2943
String projectId = "your-project-id";
@@ -33,16 +47,34 @@ public static void main(String[] args) throws Exception {
3347
getTemplate(projectId, locationId, templateId);
3448
}
3549

50+
/**
51+
* Retrieves a template using the Model Armor API.
52+
*
53+
* @param projectId the ID of the project
54+
* @param locationId the ID of the location
55+
* @param templateId the ID of the template
56+
* @throws Exception if an error occurs during template retrieval
57+
*/
3658
public static void getTemplate(String projectId, String locationId, String templateId)
3759
throws Exception {
60+
// Construct the API endpoint URL
3861
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
62+
63+
// Create a Model Armor settings object with the API endpoint
3964
ModelArmorSettings modelArmorSettings =
4065
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
4166

4267
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
68+
// Construct the template name
4369
String name = TemplateName.of(projectId, locationId, templateId).toString();
70+
71+
// Retrieve the template using the Model Armor client
4472
Template template = client.getTemplate(name);
73+
74+
// Print the retrieved template
4575
System.out.println("Retrieved template: " + JsonFormat.printer().print(template));
4676
}
4777
}
4878
}
79+
80+
// [END modelarmor_get_template]

modelarmor/src/main/java/modelarmor/ListTemplates.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,30 @@
1616

1717
package modelarmor;
1818

19+
/**
20+
* This class demonstrates how to list templates using the Model Armor API.
21+
*
22+
* @author [Your Name]
23+
*/
24+
25+
// [START modelarmor_list_templates]
26+
1927
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
2028
import com.google.cloud.modelarmor.v1.LocationName;
2129
import com.google.cloud.modelarmor.v1.ModelArmorClient;
2230
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
2331
import com.google.cloud.modelarmor.v1.Template;
2432
import com.google.protobuf.util.JsonFormat;
2533

34+
/** This class contains a main method that lists templates using the Model Armor API. */
2635
public class ListTemplates {
2736

37+
/**
38+
* Main method that calls the listTemplates method to list templates.
39+
*
40+
* @param args command line arguments (not used)
41+
* @throws Exception if an error occurs during template listing
42+
*/
2843
public static void main(String[] args) throws Exception {
2944
// TODO(developer): Replace these variables before running the sample.
3045
String projectId = "your-project-id";
@@ -33,17 +48,35 @@ public static void main(String[] args) throws Exception {
3348
listTemplates(projectId, locationId);
3449
}
3550

51+
/**
52+
* Lists templates using the Model Armor API.
53+
*
54+
* @param projectId the ID of the project
55+
* @param locationId the ID of the location
56+
* @throws Exception if an error occurs during template listing
57+
*/
3658
public static void listTemplates(String projectId, String locationId) throws Exception {
59+
// Construct the API endpoint URL
3760
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
61+
62+
// Create a Model Armor settings object with the API endpoint
3863
ModelArmorSettings modelArmorSettings =
3964
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
4065

4166
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
67+
// Construct the parent resource name
4268
String parent = LocationName.of(projectId, locationId).toString();
69+
70+
// Create a list templates request object
4371
ListTemplatesRequest request = ListTemplatesRequest.newBuilder().setParent(parent).build();
72+
73+
// List templates using the Model Armor client
4474
for (Template template : client.listTemplates(request).iterateAll()) {
45-
System.out.println("Retrived Templates: " + JsonFormat.printer().print(template));
75+
// Print each retrieved template
76+
System.out.println("Retrieved Templates: " + JsonFormat.printer().print(template));
4677
}
4778
}
4879
}
4980
}
81+
82+
// [END modelarmor_list_templates]

modelarmor/src/main/java/modelarmor/ListTemplatesWithFilter.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,31 @@
1616

1717
package modelarmor;
1818

19+
/**
20+
* This class demonstrates how to list templates with a filter using the Model Armor API.
21+
*
22+
* @author [Your Name]
23+
*/
1924
import com.google.cloud.modelarmor.v1.ListTemplatesRequest;
2025
import com.google.cloud.modelarmor.v1.LocationName;
2126
import com.google.cloud.modelarmor.v1.ModelArmorClient;
2227
import com.google.cloud.modelarmor.v1.ModelArmorSettings;
2328
import com.google.cloud.modelarmor.v1.Template;
2429
import com.google.protobuf.util.JsonFormat;
2530

31+
// [START modelarmor_list_templates_with_filter]
32+
33+
/**
34+
* This class contains a main method that lists templates with a filter using the Model Armor API.
35+
*/
2636
public class ListTemplatesWithFilter {
2737

38+
/**
39+
* Main method that calls the listTemplatesWithFilter method to list templates with a filter.
40+
*
41+
* @param args command line arguments (not used)
42+
* @throws Exception if an error occurs during template listing
43+
*/
2844
public static void main(String[] args) throws Exception {
2945
// TODO(developer): Replace these variables before running the sample.
3046
String projectId = "your-project-id";
@@ -34,18 +50,37 @@ public static void main(String[] args) throws Exception {
3450
listTemplatesWithFilter(projectId, locationId, templateId);
3551
}
3652

53+
/**
54+
* Lists templates with a filter using the Model Armor API.
55+
*
56+
* @param projectId the ID of the project
57+
* @param locationId the ID of the location
58+
* @param templateId the ID of the template
59+
* @throws Exception if an error occurs during template listing
60+
*/
3761
public static void listTemplatesWithFilter(String projectId, String locationId, String templateId)
3862
throws Exception {
63+
// Construct the API endpoint URL
3964
String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
65+
66+
// Create a Model Armor settings object with the API endpoint
4067
ModelArmorSettings modelArmorSettings =
4168
ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint).build();
4269

4370
try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
71+
// Construct the parent resource name
4472
String parent = LocationName.of(projectId, locationId).toString();
73+
74+
// Construct the filter string
4575
String filter = String.format("name=\"%s/templates/%s\"", parent, templateId);
76+
77+
// Create a list templates request object with the filter
4678
ListTemplatesRequest request =
4779
ListTemplatesRequest.newBuilder().setParent(parent).setFilter(filter).build();
80+
81+
// List templates using the Model Armor client
4882
for (Template template : client.listTemplates(request).iterateAll()) {
83+
// Print each retrieved template
4984
System.out.println("Template with filter: " + JsonFormat.printer().print(template));
5085
}
5186
}

modelarmor/src/test/java/modelarmor/SnippetsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void testGetModelArmorTemplate() throws Exception {
9797
public void testListModelArmorTemplates() throws Exception {
9898
CreateTemplate.createTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
9999
ListTemplates.listTemplates(PROJECT_ID, LOCATION);
100-
assertThat(stdOut.toString()).contains("Retrived Templates");
100+
assertThat(stdOut.toString()).contains("Retrieved Templates");
101101
DeleteTemplate.deleteTemplate(PROJECT_ID, LOCATION, TEMPLATE_ID);
102102
}
103103

0 commit comments

Comments
 (0)