From 1a9d31e5fc2ea66f2d2f0accbe2c3d77505f9e27 Mon Sep 17 00:00:00 2001 From: skyflow-bharti Date: Wed, 17 Dec 2025 13:55:16 +0530 Subject: [PATCH] SK-2448 add sample for vault url --- README.md | 35 +++++++-- .../vaultapi/baseVaultURLAndCustomHeaders.go | 72 +++++++++++++++++++ 2 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 samples/v2/vaultapi/baseVaultURLAndCustomHeaders.go diff --git a/README.md b/README.md index fa6eca2..8e8158d 100644 --- a/README.md +++ b/README.md @@ -154,9 +154,10 @@ In V2, the log level is tied to each individual client instance. During client initialization, you can pass the following parameters: -- `VaultID` and `VaultURL`: These values are derived from the vault ID & vault URL. +- `VaultID` and `ClusterId`: These values are derived from the vault ID & vault URL. - `Env`: Specify the environment (e.g., SANDBOX or PROD). - `Credentials`: The necessary authentication credentials. +- `BaseVaultURL`: URL of the vault that the client should connect to. #### V1 (Old): ```go @@ -440,15 +441,40 @@ func main() { Env: common.SANDBOX, // Set the environment for this vault. Credentials: tertiaryCredentials, // Attach the secondary credentials to this configuration. } - // Step 9: Build and initialize the Skyflow client. - // Skyflow client is configured with multiple vaults and credentials. + + // Step 9: Another vault config with Base Vault URL + credentials := common.Credentials { + ApiKey: "", + } // Replace with your API Key for authentication. + // A vault configuration can be used for operations involving multiple vaults. + ConfigWithVaultURL := common.VaultConfig { + VaultId: "", // Replace with your secondary vault's ID. + BaseVaultURL: "", // URL of the vault that the client should connect to. + Env: common.SANDBOX, // Set the environment for this vault. + Credentials: credentials, // Attach the secondary credentials to this configuration. + } + + // Step 10: Build and initialize the Skyflow client. var arr[] common.VaultConfig - arr = append(arr, primaryConfig, secondaryConfig, tertiaryConfig) + arr = append(arr, primaryConfig, secondaryConfig, tertiaryConfig, ConfigWithVaultURL) + + // Skyflow client is configured with multiple vaults and credentials. + skyflowClient, err: = client.NewSkyflow( + client.WithVaults(arr...), + client.WithCredentials(skyflowCredentials), // Add JSON-formatted credentials if applicable. + client.WithLogLevel(logger.DEBUG), // Set log level for debugging or monitoring purposes. + ) + + // OR Build and initialize the Skyflow client with custom headers. + var customHeaders = make(map[string]string) // Create a map for custom headers + customHeaders["x-custom-header"] = "custom-header-value" + skyflowClient, err: = client.NewSkyflow( client.WithVaults(arr...), client.WithCredentials(skyflowCredentials), // Add JSON-formatted credentials if applicable. client.WithLogLevel(logger.DEBUG), // Set log level for debugging or monitoring purposes. + client.WithCustomHeaders(customHeaders), // Added custom headers ) // The Skyflow client is now fully initialized. // Use the `skyflowClient` object to perform secure operations such as: @@ -463,6 +489,7 @@ func main() { - If both Skyflow common credentials and individual credentials at the configuration level are specified, the individual credentials at the configuration level will take precedence. - If neither Skyflow common credentials nor individual configuration-level credentials are provided, the SDK attempts to retrieve credentials from the `SKYFLOW_CREDENTIALS` environment variable. - All Vault operations require a client instance. +- If `BaseVaultURL` is specified in the vault configuration, it will be used for all API calls for that vault. ### Insert data into the vault To insert data into your vault, use the `Insert` method. The `InsertRequest` struct creates an insert request, which includes the values to be inserted as a list of records. Below is a simple example to get started. For advanced options, check out [Insert data into the vault]() section. diff --git a/samples/v2/vaultapi/baseVaultURLAndCustomHeaders.go b/samples/v2/vaultapi/baseVaultURLAndCustomHeaders.go new file mode 100644 index 0000000..6ff2886 --- /dev/null +++ b/samples/v2/vaultapi/baseVaultURLAndCustomHeaders.go @@ -0,0 +1,72 @@ +/* +Copyright (c) 2022 Skyflow, Inc. +*/ +package main + +import ( + "context" + "fmt" + + "github.com/skyflowapi/skyflow-go/v2/client" + "github.com/skyflowapi/skyflow-go/v2/utils/common" + "github.com/skyflowapi/skyflow-go/v2/utils/logger" +) + +func main() { + // Step 1: Set up Skyflow vault credentials + vaultConfig1 := common.VaultConfig{ + VaultId: "", + BaseVaultURL: "", // Custom base vault URL + Env: common.SANDBOX, + Credentials: common.Credentials{ + Token: "", + }, + } + var arr []common.VaultConfig + arr = append(arr, vaultConfig1) + + var customHeaders = make(map[string]string) // Create a map for custom headers + customHeaders["x-custom-header"] = "custom-header-value" + customHeaders["X-Gateway-Route-ID"] = "UNIQUE_VAULT_ROUTING_ID" + customHeaders["X-Application-Source"] = "sample-application" + + // Step 2: Configure the skyflow client + skyflowInstance, err := client.NewSkyflow( + client.WithVaults(arr...), + client.WithLogLevel(logger.INFO), + client.WithCustomHeaders(customHeaders), // Added custom headers + ) + if err != nil { + fmt.Println(*err) + } else { + // Step 3: Configure the vault + service, serviceError := skyflowInstance.Vault("") // Replace with your vault ID from the vault config + if serviceError != nil { + fmt.Println(*serviceError) + } else { + ctx := context.TODO() + values := make([]map[string]interface{}, 0) + values = append(values, map[string]interface{}{ + "": "", + }) + values = append(values, map[string]interface{}{ + "": "", + "": "", + }) + + // Step 4: Insert records with proper data and receive tokens + insert, insertErr := service.Insert(ctx, common.InsertRequest{ + Table: "", // Replace with actual table + Values: values, + }, common.InsertOptions{ContinueOnError: false, ReturnTokens: true}) + + // Step 5: Handle the response and errors + if insertErr != nil { + fmt.Println("ERROR: ", *insertErr) + } else { + fmt.Println("RESPONSE: ", insert) + } + } + } + +}