Skip to content

Commit

Permalink
Improve SDK bindings examples (#377)
Browse files Browse the repository at this point in the history
## Type of change
```
- [ ] Bug fix
- [ ] New feature development
- [x] Tech debt (refactoring, code cleanup, dependency upgrades, etc)
- [ ] Build/deploy pipeline (DevOps)
- [ ] Other
```

## Objective
Updated all the samples to load access token, organization id and urls
from env variables. This made it much easier to quickly test all the
bindings. Also added one Java example.
  • Loading branch information
dani-garcia authored Dec 14, 2023
1 parent a8ebf5a commit 7ec2183
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 58 deletions.
15 changes: 8 additions & 7 deletions languages/cpp/examples/Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ int main() {
const char* accessTokenEnv = std::getenv("ACCESS_TOKEN");
const char* organizationIdEnv = std::getenv("ORGANIZATION_ID");

const char* apiUrl = std::getenv("API_URL");
const char* identityUrl = std::getenv("IDENTITY_URL");

if (!accessTokenEnv || !organizationIdEnv) {
std::cerr << "Error: Environment variables ACCESS_TOKEN or ORGANIZATION_ID not set." << std::endl;
return 1;
Expand All @@ -15,15 +18,13 @@ int main() {
std::string accessToken = accessTokenEnv;
std::string organizationId = organizationIdEnv;



// Optional - commented to use default values
// BitwardenSettings bitwardenSettings;
// bitwardenSettings.set_api_url("<bitwarden-url>");
// bitwardenSettings.set_identity_url("<bitwarden-identity>");
// Configuring the URLS is optional, remove them to use the default values
BitwardenSettings bitwardenSettings;
bitwardenSettings.set_api_url(apiUrl);
bitwardenSettings.set_identity_url(identityUrl);

// Create a Bitwarden client instance
BitwardenClient bitwardenClient = BitwardenClient();
BitwardenClient bitwardenClient = BitwardenClient(bitwardenSettings);
// // Access token login
bitwardenClient.accessTokenLogin(accessToken);
// Organization ID
Expand Down
1 change: 1 addition & 0 deletions languages/go/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
)

func main() {
// Configuring the URLS is optional, set them to nil to use the default values
apiURL := os.Getenv("API_URL")
identityURL := os.Getenv("IDENTITY_URL")

Expand Down
33 changes: 33 additions & 0 deletions languages/java/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.lang.System;
import java.util.UUID;

import com.bitwarden.sdk.*;
import com.bitwarden.sdk.schema.*;

class Example {
public static void main(String[] args) {

String accessToken = System.getenv("ACCESS_TOKEN");
UUID organizationId = UUID.fromString(System.getenv("ORGANIZATION_ID"));
String apiUrl = System.getenv("API_URL");
String identityUrl = System.getenv("IDENTITY_URL");

// Configuring the URLS is optional, remove them to use the default values
BitwardenSettings bitwardenSettings = new BitwardenSettings();
bitwardenSettings.setApiUrl(apiUrl);
bitwardenSettings.setIdentityUrl(identityUrl);
BitwardenClient client = new BitwardenClient(bitwardenSettings);
client.accessTokenLogin(accessToken);


ProjectResponse project = client.projects().create(organizationId, "Test Project");
ProjectsResponse list = client.projects().list(organizationId);

SecretResponse secret = client.secrets().create("Secret Key", "Secret Value", "Secret Note", organizationId, new UUID[] { project.getID() });

System.out.println("Secret: " + secret.getValue());

client.secrets().delete(new UUID[] { secret.getID() });
client.projects().delete(new UUID[] { project.getID() });
}
}
42 changes: 0 additions & 42 deletions languages/java/src/main/java/com/bitwarden/sdk/ExampleProgram.java

This file was deleted.

10 changes: 7 additions & 3 deletions languages/php/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

require_once 'vendor/autoload.php';

$access_token = '<you access token here>';
$organization_id = "<your organization id here>";
$access_token = getenv('ACCESS_TOKEN');
$organization_id = getenv('ORGANIZATION_ID');

$client_settings = new \Bitwarden\Sdk\BitwardenSettings();
// Configuring the URLS is optional, set them to null to use the default values
$api_url = getenv('API_URL');
$identity_url = getenv('IDENTITY_URL');

$client_settings = new \Bitwarden\Sdk\BitwardenSettings($api_url, $identity_url);

$bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($client_settings);
$bitwarden_client->access_token_login($access_token);
Expand Down
13 changes: 7 additions & 6 deletions languages/ruby/examples/example.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# NOTE - for example purpose only - import gem instead
require 'bitwarden-sdk'

token = '<insert access token here>'
organization_id = '<organization id here>'
token = ENV['ACCESS_TOKEN']
organization_id = ENV['ORGANIZATION_ID']

bitwarden_settings = BitwardenSDK::BitwardenSettings.new(
'https://api.bitwarden.com',
'https://identity.bitwarden.com/connect/token'
)
# Configuring the URLS is optional, set them to nil to use the default values
api_url = ENV['API_URL']
identity_url = ENV['IDENTITY_URL']

bitwarden_settings = BitwardenSDK::BitwardenSettings.new(api_url, identity_url)

bw_client = BitwardenSDK::BitwardenClient.new(bitwarden_settings)
response = bw_client.access_token_login(token)
Expand Down

0 comments on commit 7ec2183

Please sign in to comment.