Skip to content

Commit ec27182

Browse files
Merge pull request #307 from skyflowapi/SK-2824-Sample-and-readme-for-custom-header
SK-2824 add sample and readme for custom headers
2 parents 851bae8 + fd6057e commit ec27182

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The Skyflow Java SDK is designed to help with integrating Skyflow into a Java ba
2323
- [Bulk Detokenize](#bulk-detokenize)
2424
- [Bulk Tokenize](#bulk-tokenize)
2525
- [Bulk Delete Tokens](#bulk-delete-tokens)
26+
- [Custom request headers](#custom-request-headers)
2627
- [Authenticate with bearer tokens](#authenticate-with-bearer-tokens)
2728
- [Generate a bearer token](#generate-a-bearer-token)
2829
- [Generate bearer tokens with context](#generate-bearer-tokens-with-context)
@@ -1461,6 +1462,74 @@ Sample response:
14611462
}
14621463
```
14631464

1465+
## Custom request headers
1466+
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.
1467+
1468+
### Available custom header keys
1469+
1470+
| `CustomHeaderKey` | HTTP header name |
1471+
|---|---|
1472+
| `SkyflowAccountID` | `x-skyflow-account-id` |
1473+
| `SkyflowAccountName` | `x-skyflow-account-name` |
1474+
| `RequestIDHeader` | `x-request-id` |
1475+
1476+
### Example
1477+
1478+
```java
1479+
import com.skyflow.enums.CustomHeaderKey;
1480+
import com.skyflow.errors.SkyflowException;
1481+
import com.skyflow.vault.data.InsertOptions;
1482+
import com.skyflow.vault.data.InsertRecord;
1483+
import com.skyflow.vault.data.InsertRequest;
1484+
import com.skyflow.vault.data.InsertResponse;
1485+
1486+
import java.util.ArrayList;
1487+
import java.util.HashMap;
1488+
1489+
public class BulkInsertWithHeaders {
1490+
public static void main(String[] args) {
1491+
try {
1492+
// Initialize Skyflow client
1493+
1494+
HashMap<String, Object> data = new HashMap<>();
1495+
data.put("<YOUR_COLUMN_NAME>", "<YOUR_VALUE>");
1496+
InsertRecord record = InsertRecord.builder()
1497+
.data(data)
1498+
.table("<YOUR_TABLE_NAME>")
1499+
.build();
1500+
1501+
ArrayList<InsertRecord> records = new ArrayList<>();
1502+
records.add(record);
1503+
1504+
InsertRequest insertRequest = InsertRequest.builder()
1505+
.records(records)
1506+
.build();
1507+
1508+
// Pass custom headers via an interceptor in the options object
1509+
InsertOptions options = InsertOptions.builder()
1510+
.interceptor(context -> {
1511+
context.addHeader(CustomHeaderKey.RequestIDHeader, "<YOUR_REQUEST_ID>");
1512+
})
1513+
.build();
1514+
1515+
InsertResponse insertResponse = skyflowClient.vault().bulkInsert(insertRequest, options);
1516+
System.out.println(insertResponse);
1517+
} catch (SkyflowException e) {
1518+
e.printStackTrace();
1519+
}
1520+
}
1521+
}
1522+
```
1523+
1524+
The same pattern applies to all operations using the corresponding options class:
1525+
1526+
| Operation | Options class |
1527+
|---|---|
1528+
| `bulkInsert` / `bulkInsertAsync` | `InsertOptions` |
1529+
| `bulkDetokenize` / `bulkDetokenizeAsync` | `DetokenizeOptions` |
1530+
| `bulkTokenize` / `bulkTokenizeAsync` | `TokenizeOptions` |
1531+
| `bulkDeleteTokens` / `bulkDeleteTokensAsync` | `DeleteTokensOptions` |
1532+
14641533
# Authenticate with bearer tokens
14651534

14661535
This section covers methods for generating and managing tokens to authenticate API calls:
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.example.vault;
2+
3+
import com.skyflow.Skyflow;
4+
import com.skyflow.config.Credentials;
5+
import com.skyflow.config.VaultConfig;
6+
import com.skyflow.enums.CustomHeaderKey;
7+
import com.skyflow.enums.Env;
8+
import com.skyflow.enums.LogLevel;
9+
import com.skyflow.enums.UpsertType;
10+
import com.skyflow.vault.data.InsertOptions;
11+
import com.skyflow.vault.data.InsertRecord;
12+
import com.skyflow.vault.data.InsertRequest;
13+
import com.skyflow.vault.data.InsertResponse;
14+
15+
import java.util.ArrayList;
16+
import java.util.HashMap;
17+
import java.util.UUID;
18+
import java.util.concurrent.CompletableFuture;
19+
import java.util.concurrent.CompletionException;
20+
21+
public class CustomHeaderExample {
22+
public static void main(String[] args) {
23+
try {
24+
// Step 1: Initialize credentials with the path to your service account key file
25+
// String filePath = "<YOUR_CREDENTIALS_FILE_PATH>";
26+
Credentials credentials = new Credentials();
27+
credentials.setToken("<BEARER_TOKEN>");
28+
29+
// Step 2: Configure the vault with required parameters
30+
VaultConfig vaultConfig = new VaultConfig();
31+
vaultConfig.setVaultId("<VAULT_ID>");
32+
vaultConfig.setClusterId("<CLUSTER_ID>");
33+
vaultConfig.setEnv(Env.DEV);
34+
vaultConfig.setCredentials(credentials);
35+
36+
// Step 3: Create Skyflow client instance with error logging
37+
Skyflow skyflowClient = Skyflow.builder()
38+
.setLogLevel(LogLevel.DEBUG)
39+
.addVaultConfig(vaultConfig)
40+
.build();
41+
ArrayList<InsertRecord> insertRecords = new ArrayList<>();
42+
43+
for (int i = 0; i < 100; i++) {
44+
// Step 4: Prepare first record for insertion
45+
HashMap<String, Object> recordData1 = new HashMap<>();
46+
recordData1.put("<YOUR_COLUMN_NAME_2>", "<YOUR_VALUE_1>");
47+
48+
InsertRecord insertRecord1 = InsertRecord
49+
.builder()
50+
.data(recordData1)
51+
.build();
52+
53+
// Step 6: Combine records into a Insert record list
54+
insertRecords.add(insertRecord1);
55+
}
56+
ArrayList<String> upsertColumns = new ArrayList<>();
57+
upsertColumns.add("<UPSERT_COLUMN_NAME>");
58+
InsertRequest request = InsertRequest.builder()
59+
.table("<TABLE_NAME>")
60+
.upsert(upsertColumns)
61+
.upsertType(UpsertType.REPLACE)
62+
.records(insertRecords)
63+
.build();
64+
InsertOptions options = InsertOptions.builder()
65+
.interceptor((ctx) ->{
66+
ctx.addHeader(CustomHeaderKey.RequestIDHeader, getRequestId()); // pass the request id here
67+
})
68+
.build();
69+
// Step 8: Execute the async bulk insert operation and handle response using callbacks
70+
CompletableFuture<InsertResponse> future = skyflowClient.vault().bulkInsertAsync(request, options);
71+
// Add success and error callbacks
72+
future.thenAccept(response -> {
73+
System.out.println("Async bulk insert resolved with response:\t" + response);
74+
}).exceptionally(throwable -> {
75+
System.err.println("Async bulk insert rejected with error:\t" + throwable.getMessage());
76+
throw new CompletionException(throwable);
77+
});
78+
} catch (Exception e) {
79+
// Step 9: Handle any synchronous errors that occur during setup
80+
System.err.println("Error in Skyflow operations:\t" + e.getMessage());
81+
}
82+
}
83+
public static String getRequestId(){
84+
String id = UUID.randomUUID().toString();
85+
System.out.println("id=>"+ id);
86+
return id;
87+
}
88+
}

0 commit comments

Comments
 (0)