Skip to content
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

Improve SDK bindings examples #377

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>");
// Optional - Remove it to use 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
32 changes: 32 additions & 0 deletions languages/java/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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");

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() });
}
}
9 changes: 6 additions & 3 deletions languages/php/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

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();
$api_url = getenv('API_URL');
$identity_url = getenv('IDENTITY_URL');

$client_settings = new \Bitwarden\Sdk\BitwardenSettings($api_url, $identity_url);
Hinton marked this conversation as resolved.
Show resolved Hide resolved

$bitwarden_client = new \Bitwarden\Sdk\BitwardenClient($client_settings);
$bitwarden_client->access_token_login($access_token);
Expand Down
12 changes: 6 additions & 6 deletions languages/ruby/examples/example.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# 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'
)
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