Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<String, Object> data = new HashMap<>();
data.put("<YOUR_COLUMN_NAME>", "<YOUR_VALUE>");
InsertRecord record = InsertRecord.builder()
.data(data)
.table("<YOUR_TABLE_NAME>")
.build();

ArrayList<InsertRecord> 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, "<YOUR_REQUEST_ID>");
})
.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:
Expand Down
88 changes: 88 additions & 0 deletions samples/src/main/java/com/example/vault/CustomHeaderExample.java
Original file line number Diff line number Diff line change
@@ -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 = "<YOUR_CREDENTIALS_FILE_PATH>";
Credentials credentials = new Credentials();
credentials.setToken("<BEARER_TOKEN>");

// Step 2: Configure the vault with required parameters
VaultConfig vaultConfig = new VaultConfig();
vaultConfig.setVaultId("<VAULT_ID>");
vaultConfig.setClusterId("<CLUSTER_ID>");
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<InsertRecord> insertRecords = new ArrayList<>();

for (int i = 0; i < 100; i++) {
// Step 4: Prepare first record for insertion
HashMap<String, Object> recordData1 = new HashMap<>();
recordData1.put("<YOUR_COLUMN_NAME_2>", "<YOUR_VALUE_1>");

InsertRecord insertRecord1 = InsertRecord
.builder()
.data(recordData1)
.build();

// Step 6: Combine records into a Insert record list
insertRecords.add(insertRecord1);
}
ArrayList<String> upsertColumns = new ArrayList<>();
upsertColumns.add("<UPSERT_COLUMN_NAME>");
InsertRequest request = InsertRequest.builder()
.table("<TABLE_NAME>")
.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<InsertResponse> 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;
}
}
Loading