-
Notifications
You must be signed in to change notification settings - Fork 2
fix(examples): improve logging and setup logic in examples #316
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
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ad70a3
fix(examples): improve logging and setup logic in examples
cshamrick c1c62f1
fix(examples): wrap sdk initialization in try-with-resources
cshamrick c73e9ce
fix(examples): remove needless throws exception
cshamrick bb3e626
fix(examples): enhance error handling and logging
cshamrick 5b7c299
fix(examples): update namespace name in examples
cshamrick a86c1f7
Merge branch 'main' into refactor-examples
cshamrick 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
71 changes: 54 additions & 17 deletions
71
examples/src/main/java/io/opentdf/platform/CreateAttribute.java
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 |
|---|---|---|
| @@ -1,37 +1,74 @@ | ||
| package io.opentdf.platform; | ||
|
|
||
| import com.connectrpc.ResponseMessageKt; | ||
| import io.opentdf.platform.policy.Attribute; | ||
| import io.opentdf.platform.policy.AttributeRuleTypeEnum; | ||
| import io.opentdf.platform.policy.Namespace; | ||
| import io.opentdf.platform.policy.attributes.CreateAttributeRequest; | ||
| import io.opentdf.platform.policy.attributes.CreateAttributeResponse; | ||
| import io.opentdf.platform.policy.namespaces.GetNamespaceRequest; | ||
| import io.opentdf.platform.sdk.*; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public class CreateAttribute { | ||
| public static void main(String[] args) { | ||
|
|
||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
| private static final Logger logger = LogManager.getLogger(CreateAttribute.class); | ||
|
|
||
| SDKBuilder builder = new SDKBuilder(); | ||
| SDK sdk = builder.platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) | ||
| .build(); | ||
| public static void main(String[] args) { | ||
|
|
||
| CreateAttributeRequest request = CreateAttributeRequest.newBuilder() | ||
| .setNamespaceId("877990d1-609b-42ab-a273-4253b8b321eb") | ||
| .setName("test") | ||
| .setRule(AttributeRuleTypeEnum.forNumber(AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) | ||
| .addAllValues(Arrays.asList("test1", "test2")).build(); | ||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
|
|
||
| CreateAttributeResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().attributes().createAttributeBlocking(request, Collections.emptyMap()).execute()); | ||
| SDKBuilder builder = new SDKBuilder(); | ||
| SDK sdk = | ||
| builder | ||
| .platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret) | ||
| .useInsecurePlaintextConnection(true) | ||
| .build(); | ||
|
|
||
| Attribute attribute = resp.getAttribute(); | ||
| Namespace namespace; | ||
| String namespaceName = "mynamespace.com"; | ||
|
|
||
| try { | ||
| namespace = | ||
| ResponseMessageKt.getOrThrow( | ||
| sdk.getServices() | ||
| .namespaces() | ||
| .getNamespaceBlocking( | ||
| GetNamespaceRequest.newBuilder() | ||
| .setFqn("https://" + namespaceName) | ||
| .build(), | ||
| Collections.emptyMap()) | ||
| .execute()) | ||
| .getNamespace(); | ||
| } catch (Exception e) { | ||
| logger.error("Namespace '{}' not found", namespaceName); | ||
| throw e; | ||
| } | ||
|
|
||
| CreateAttributeRequest request = | ||
| CreateAttributeRequest.newBuilder() | ||
| .setNamespaceId(namespace.getId()) | ||
| .setName("test") | ||
| .setRule( | ||
| AttributeRuleTypeEnum.forNumber( | ||
| AttributeRuleTypeEnum.ATTRIBUTE_RULE_TYPE_ENUM_ALL_OF_VALUE)) | ||
| .addAllValues(Arrays.asList("test1", "test2")) | ||
| .build(); | ||
|
|
||
| CreateAttributeResponse resp = | ||
| ResponseMessageKt.getOrThrow( | ||
| sdk.getServices() | ||
| .attributes() | ||
| .createAttributeBlocking(request, Collections.emptyMap()) | ||
| .execute()); | ||
|
|
||
| logger.info("Successfully created attribute with ID: {}", resp.getAttribute().getId()); | ||
| } | ||
| } | ||
36 changes: 22 additions & 14 deletions
36
examples/src/main/java/io/opentdf/platform/CreateNamespace.java
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 |
|---|---|---|
| @@ -1,29 +1,37 @@ | ||
| package io.opentdf.platform; | ||
|
|
||
| import com.connectrpc.ResponseMessageKt; | ||
| import io.opentdf.platform.policy.namespaces.CreateNamespaceRequest; | ||
| import io.opentdf.platform.policy.namespaces.CreateNamespaceResponse; | ||
| import io.opentdf.platform.sdk.*; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public class CreateNamespace { | ||
| public static void main(String[] args) { | ||
| public static void main(String[] args) { | ||
|
|
||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
| String clientId = "opentdf"; | ||
| String clientSecret = "secret"; | ||
| String platformEndpoint = "localhost:8080"; | ||
|
|
||
| SDKBuilder builder = new SDKBuilder(); | ||
| SDK sdk = builder.platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret).useInsecurePlaintextConnection(true) | ||
| .build(); | ||
| SDKBuilder builder = new SDKBuilder(); | ||
| SDK sdk = | ||
| builder | ||
| .platformEndpoint(platformEndpoint) | ||
| .clientSecret(clientId, clientSecret) | ||
| .useInsecurePlaintextConnection(true) | ||
| .build(); | ||
cshamrick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| CreateNamespaceRequest request = CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build(); | ||
| CreateNamespaceRequest request = | ||
| CreateNamespaceRequest.newBuilder().setName("mynamespace.com").build(); | ||
|
|
||
| CreateNamespaceResponse resp = ResponseMessageKt.getOrThrow(sdk.getServices().namespaces().createNamespaceBlocking(request, Collections.emptyMap()).execute()); | ||
| CreateNamespaceResponse resp = | ||
| ResponseMessageKt.getOrThrow( | ||
| sdk.getServices() | ||
| .namespaces() | ||
| .createNamespaceBlocking(request, Collections.emptyMap()) | ||
| .execute()); | ||
|
|
||
| System.out.println(resp.getNamespace().getName()); | ||
|
|
||
| } | ||
| System.out.println(resp.getNamespace().getName()); | ||
cshamrick marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.