Skip to content

Commit

Permalink
unwrap sdk responses, addressing PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lukpotSym committed Oct 18, 2023
1 parent 251ef47 commit bb2af7f
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 253 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ crates/bitwarden-napi/src-ts/bitwarden_client/schemas.ts
languages/csharp/Bitwarden.Sdk/schemas.cs
languages/js_webassembly/bitwarden_client/schemas.ts
languages/python/BitwardenClient/schemas.py
languages/java/src/main/java/bit/sdk/schema
languages/java/src/main/java/com/bitwarden/sdk/schema
7 changes: 2 additions & 5 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@
{
"label": "buildJava",
"type": "shell",
"command": "mvn",
"args": [
"clean",
"install"
],
"command": "gradle",
"args": ["build"],
"dependsOrder": "sequence",
"dependsOn": ["rust: bitwarden-c build"]
}
Expand Down
9 changes: 6 additions & 3 deletions languages/java/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Bitwarden Secrets Manager SDK

Java bindings for interacting with the [Bitwarden Secrets Manager]. This is a beta release and might be missing some functionality.
Java bindings for interacting with the [Bitwarden Secrets Manager]. This is a beta release and might be missing some
functionality.

## Create access token

Expand Down Expand Up @@ -34,7 +35,7 @@ var projectsResponse = bitwardenClient.projects().list(organizationId);
### Update project

```java
UUID projectId = projectResponse.getData().getID();
UUID projectId = projectResponse.getID();
projectResponse = bitwardenClient.projects().get(projectId);
projectResponse = bitwardenClient.projects.update(projectId, organizationId, "TestProjectUpdated");
```
Expand All @@ -46,10 +47,11 @@ String key = "key";
String value = "value";
String note = "note";
var secretResponse = bitwardenClient.secrets().create(key, value, note, organizationId, new UUID[]{projectId});
UUID secretId = secretResponse.getData().getID();
UUID secretId = secretResponse.getID();
```

### Update secret

```java
bitwardenClient.secrets().update(secretId, key2, value2, note2, organizationId, new UUID[]{projectId});
```
Expand All @@ -68,4 +70,5 @@ bitwardenClient.projects().delete(new UUID[]{projectId});
```

[Access Tokens]: https://bitwarden.com/help/access-tokens/

[Bitwarden Secrets Manager]: https://bitwarden.com/products/secrets-manager/
2 changes: 1 addition & 1 deletion languages/java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
publications {
maven(MavenPublication) {
groupId = 'com.bitwarden'
artifactId = 'sdk-java'
artifactId = 'sdk'

// Determine the version from the git history.
//
Expand Down
48 changes: 0 additions & 48 deletions languages/java/src/main/java/bit/sdk/ExampleProgram.java

This file was deleted.

83 changes: 0 additions & 83 deletions languages/java/src/main/java/bit/sdk/Projects.java

This file was deleted.

91 changes: 0 additions & 91 deletions languages/java/src/main/java/bit/sdk/Secrets.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bit.sdk;
package com.bitwarden.sdk;

import bit.sdk.schema.*;
import com.bitwarden.sdk.schema.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
Expand All @@ -15,7 +15,7 @@ static <T, R> Function<T, R> throwingFunctionWrapper(ThrowingFunction<T, R, Exce
try {
return throwingFunction.accept(i);
} catch (Exception ex) {
throw new RuntimeException(ex);
throw new BitwardenClientException("Response deserialization failed");
}
};
}
Expand All @@ -28,42 +28,46 @@ static <T, R> Function<T, R> throwingFunctionWrapper(ThrowingFunction<T, R, Exce

private boolean isClientOpen;

private Projects projects;
private ProjectsClient projects;

private Secrets secrets;
private SecretsClient secrets;

public BitwardenClient(BitwardenSettings bitwardenSettings) {
ClientSettings clientSettings = new ClientSettings();
clientSettings.setAPIURL(bitwardenSettings.getApiUrl());
clientSettings.setIdentityURL(bitwardenSettings.getIdentityUrl());
clientSettings.setDeviceType(DeviceType.SDK);
clientSettings.setUserAgent("Bitwarden Java SDK");
clientSettings.setUserAgent("Bitwarden JAVA-SDK");
library = Native.load("bitwarden_c", BitwardenLibrary.class);
try {
client = library.init(Converter.ClientSettingsToJsonString(clientSettings));
} catch (JsonProcessingException e) {
throw new RuntimeException("Error while processing client settings", e);
throw new BitwardenClientException("Error while processing client settings");
}
commandRunner = new CommandRunner(library, client);
projects = new Projects(commandRunner);
secrets = new Secrets(commandRunner);
projects = new ProjectsClient(commandRunner);
secrets = new SecretsClient(commandRunner);
isClientOpen = true;
}

public ResponseForAPIKeyLoginResponse accessTokenLogin(String accessToken) {
public APIKeyLoginResponse accessTokenLogin(String accessToken) {
Command command = new Command();
AccessTokenLoginRequest accessTokenLoginRequest = new AccessTokenLoginRequest();
accessTokenLoginRequest.setAccessToken(accessToken);
command.setAccessTokenLogin(accessTokenLoginRequest);
return commandRunner.runCommand(command,
ResponseForAPIKeyLoginResponse response = commandRunner.runCommand(command,
throwingFunctionWrapper(Converter::ResponseForAPIKeyLoginResponseFromJsonString));
if (response == null || !response.getSuccess()) {
throw new BitwardenClientException(response != null ? response.getErrorMessage() : "Login failed");
}
return response.getData();
}

public Projects projects() {
public ProjectsClient projects() {
return projects;
}

public Secrets secrets() {
public SecretsClient secrets() {
return secrets;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.bitwarden.sdk;

public class BitwardenClientException extends RuntimeException {

public BitwardenClientException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bit.sdk;
package com.bitwarden.sdk;

import com.sun.jna.Library;
import com.sun.jna.Pointer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bit.sdk;
package com.bitwarden.sdk;

public class BitwardenSettings {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bit.sdk;
package com.bitwarden.sdk;

import bit.sdk.schema.Command;
import bit.sdk.schema.Converter;
import com.bitwarden.sdk.schema.Command;
import com.bitwarden.sdk.schema.Converter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.jna.Pointer;
Expand Down
Loading

0 comments on commit bb2af7f

Please sign in to comment.