diff --git a/README.md b/README.md index 0ce47dc8..938c7b89 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ The Skyflow Java SDK is designed to help with integrating Skyflow into a Java ba - [Bulk Detokenize](#bulk-detokenize) - [Bulk Tokenize](#bulk-tokenize) - [Bulk Delete Tokens](#bulk-delete-tokens) + - [Custom request headers](#custom-request-headers) - [Authenticate with bearer tokens](#authenticate-with-bearer-tokens) - [Generate a bearer token](#generate-a-bearer-token) - [Generate bearer tokens with context](#generate-bearer-tokens-with-context) @@ -1461,6 +1462,74 @@ Sample response: } ``` +## Custom request headers +To include custom HTTP headers in outgoing requests, provide a `RequestInterceptor` via the options object for any vault operation. The headers you can set are defined by the `CustomHeaderKey` enum. + +### Available custom header keys + +| `CustomHeaderKey` | HTTP header name | +|---|---| +| `SkyflowAccountID` | `x-skyflow-account-id` | +| `SkyflowAccountName` | `x-skyflow-account-name` | +| `RequestIDHeader` | `x-request-id` | + +### Example + +```java +import com.skyflow.enums.CustomHeaderKey; +import com.skyflow.errors.SkyflowException; +import com.skyflow.vault.data.InsertOptions; +import com.skyflow.vault.data.InsertRecord; +import com.skyflow.vault.data.InsertRequest; +import com.skyflow.vault.data.InsertResponse; + +import java.util.ArrayList; +import java.util.HashMap; + +public class BulkInsertWithHeaders { + public static void main(String[] args) { + try { + // Initialize Skyflow client + + HashMap data = new HashMap<>(); + data.put("", ""); + InsertRecord record = InsertRecord.builder() + .data(data) + .table("") + .build(); + + ArrayList records = new ArrayList<>(); + records.add(record); + + InsertRequest insertRequest = InsertRequest.builder() + .records(records) + .build(); + + // Pass custom headers via an interceptor in the options object + InsertOptions options = InsertOptions.builder() + .interceptor(context -> { + context.addHeader(CustomHeaderKey.RequestIDHeader, ""); + }) + .build(); + + InsertResponse insertResponse = skyflowClient.vault().bulkInsert(insertRequest, options); + System.out.println(insertResponse); + } catch (SkyflowException e) { + e.printStackTrace(); + } + } +} +``` + +The same pattern applies to all operations using the corresponding options class: + +| Operation | Options class | +|---|---| +| `bulkInsert` / `bulkInsertAsync` | `InsertOptions` | +| `bulkDetokenize` / `bulkDetokenizeAsync` | `DetokenizeOptions` | +| `bulkTokenize` / `bulkTokenizeAsync` | `TokenizeOptions` | +| `bulkDeleteTokens` / `bulkDeleteTokensAsync` | `DeleteTokensOptions` | + # Authenticate with bearer tokens This section covers methods for generating and managing tokens to authenticate API calls: diff --git a/samples/src/main/java/com/example/vault/CustomHeaderExample.java b/samples/src/main/java/com/example/vault/CustomHeaderExample.java new file mode 100644 index 00000000..6ff77b51 --- /dev/null +++ b/samples/src/main/java/com/example/vault/CustomHeaderExample.java @@ -0,0 +1,88 @@ +package com.example.vault; + +import com.skyflow.Skyflow; +import com.skyflow.config.Credentials; +import com.skyflow.config.VaultConfig; +import com.skyflow.enums.CustomHeaderKey; +import com.skyflow.enums.Env; +import com.skyflow.enums.LogLevel; +import com.skyflow.enums.UpsertType; +import com.skyflow.vault.data.InsertOptions; +import com.skyflow.vault.data.InsertRecord; +import com.skyflow.vault.data.InsertRequest; +import com.skyflow.vault.data.InsertResponse; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; + +public class CustomHeaderExample { + public static void main(String[] args) { + try { + // Step 1: Initialize credentials with the path to your service account key file +// String filePath = ""; + Credentials credentials = new Credentials(); + credentials.setToken(""); + + // Step 2: Configure the vault with required parameters + VaultConfig vaultConfig = new VaultConfig(); + vaultConfig.setVaultId(""); + vaultConfig.setClusterId(""); + vaultConfig.setEnv(Env.DEV); + vaultConfig.setCredentials(credentials); + + // Step 3: Create Skyflow client instance with error logging + Skyflow skyflowClient = Skyflow.builder() + .setLogLevel(LogLevel.DEBUG) + .addVaultConfig(vaultConfig) + .build(); + ArrayList insertRecords = new ArrayList<>(); + + for (int i = 0; i < 100; i++) { + // Step 4: Prepare first record for insertion + HashMap recordData1 = new HashMap<>(); + recordData1.put("", ""); + + InsertRecord insertRecord1 = InsertRecord + .builder() + .data(recordData1) + .build(); + + // Step 6: Combine records into a Insert record list + insertRecords.add(insertRecord1); + } + ArrayList upsertColumns = new ArrayList<>(); + upsertColumns.add(""); + InsertRequest request = InsertRequest.builder() + .table("") + .upsert(upsertColumns) + .upsertType(UpsertType.REPLACE) + .records(insertRecords) + .build(); + InsertOptions options = InsertOptions.builder() + .interceptor((ctx) ->{ + ctx.addHeader(CustomHeaderKey.RequestIDHeader, getRequestId()); // pass the request id here + }) + .build(); + // Step 8: Execute the async bulk insert operation and handle response using callbacks + CompletableFuture future = skyflowClient.vault().bulkInsertAsync(request, options); + // Add success and error callbacks + future.thenAccept(response -> { + System.out.println("Async bulk insert resolved with response:\t" + response); + }).exceptionally(throwable -> { + System.err.println("Async bulk insert rejected with error:\t" + throwable.getMessage()); + throw new CompletionException(throwable); + }); + } catch (Exception e) { + // Step 9: Handle any synchronous errors that occur during setup + System.err.println("Error in Skyflow operations:\t" + e.getMessage()); + } + } + public static String getRequestId(){ + String id = UUID.randomUUID().toString(); + System.out.println("id=>"+ id); + return id; + } +}