Skip to content

Commit 92ef58e

Browse files
committed
add new API
1 parent d8578c2 commit 92ef58e

File tree

2 files changed

+160
-7
lines changed

2 files changed

+160
-7
lines changed

README.md

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ A Java client library for interacting with the DatabunkerPro API. DatabunkerPro
55
## Features
66

77
- Complete implementation of the DatabunkerPro API
8-
- User management (create, get, update, delete)
8+
- User management (create, get, update, delete, patch)
99
- App data management
1010
- Legal basis and agreement management
1111
- Connector management
1212
- Group and role management
1313
- Policy management
14-
- Token management
14+
- Token management (including bulk operations)
1515
- Audit management
1616
- Tenant management
1717
- Session management
18-
- System configuration
18+
- System configuration and metrics
1919
- Bulk operations
20+
- Shared record management
21+
- Wrapping key generation for Shamir's Secret Sharing
22+
- Typed options classes for better type safety
2023
- Thread-safe implementation
2124
- Comprehensive test suite
25+
- Enhanced error handling
2226

2327
## Requirements
2428

@@ -89,6 +93,24 @@ Then add the dependency to your `pom.xml`:
8993
</dependency>
9094
```
9195

96+
## New Features in Latest Version
97+
98+
### Enhanced API Methods
99+
- **Wrapping Key Generation**: Generate wrapping keys from Shamir's Secret Sharing keys
100+
- **Typed Patch Operations**: Use structured `PatchOperation` objects for user updates
101+
- **Bulk Token Operations**: Create multiple tokens efficiently with typed options
102+
- **Shared Record Management**: Create and retrieve shared records with partner organizations
103+
104+
### Improved Error Handling
105+
- Better HTTP status code handling
106+
- Enhanced error messages and logging
107+
- Graceful handling of API errors
108+
109+
### Type Safety Improvements
110+
- Structured patch operations with `PatchOperation` class
111+
- Builder pattern for complex options
112+
- Enhanced error handling
113+
92114
## Usage
93115

94116
### Basic Setup
@@ -129,7 +151,13 @@ api.updateUser("email", "[email protected]", updates, null);
129151

130152
// Delete a user
131153
api.deleteUser("email", "[email protected]", null);
132-
```
154+
155+
// Patch a user with typed operations
156+
PatchOperation[] patchOps = {
157+
new PatchOperation("replace", "/profile/name", "Jane Doe"),
158+
new PatchOperation("add", "/profile/age", 25)
159+
};
160+
Map<String, Object> patchResult = api.patchUser("email", "[email protected]", patchOps, null);
133161

134162
### App Data Management
135163

@@ -149,6 +177,53 @@ Map<String, Object> appData = api.getAppData("email", "[email protected]", "appna
149177
// Get system statistics
150178
Map<String, Object> stats = api.getSystemStats(null);
151179
System.out.println("System statistics: " + stats.get("stats"));
180+
181+
// Generate wrapping key from Shamir's Secret Sharing keys
182+
Map<String, Object> wrappingKey = api.generateWrappingKey("key1", "key2", "key3", null);
183+
System.out.println("Generated wrapping key: " + wrappingKey.get("wrappingkey"));
184+
185+
// Get system metrics
186+
Map<String, Object> metrics = api.getSystemMetrics(null);
187+
System.out.println("System metrics: " + metrics);
188+
```
189+
190+
### Token Management
191+
192+
```java
193+
// Create a single token
194+
TokenOptions tokenOptions = TokenOptions.builder()
195+
.slidingtime("1d")
196+
.finaltime("10d")
197+
.unique(true)
198+
.build();
199+
Map<String, Object> token = api.createToken("creditcard", "1234567890", tokenOptions, null);
200+
201+
// Create multiple tokens in bulk
202+
Map<String, Object>[] tokenRecords = new Map[2];
203+
tokenRecords[0] = new HashMap<>();
204+
tokenRecords[0].put("tokentype", "creditcard");
205+
tokenRecords[0].put("record", "1234567890");
206+
tokenRecords[1] = new HashMap<>();
207+
tokenRecords[1].put("tokentype", "email");
208+
tokenRecords[1].put("record", "[email protected]");
209+
210+
Map<String, Object> bulkTokens = api.createTokensBulk(tokenRecords, tokenOptions, null);
211+
```
212+
213+
### Shared Record Management
214+
215+
```java
216+
// Create a shared record
217+
SharedRecordOptions sharedOptions = SharedRecordOptions.builder()
218+
.fields("name,email")
219+
.partner("partner-org")
220+
.appname("myapp")
221+
.finaltime("100d")
222+
.build();
223+
Map<String, Object> sharedRecord = api.createSharedRecord("email", "[email protected]", sharedOptions, null);
224+
225+
// Get a shared record
226+
Map<String, Object> retrievedRecord = api.getSharedRecord("record-uuid", null);
152227
```
153228

154229

src/main/java/org/databunker/DatabunkerproApi.java

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.databunker.options.RoleOptions;
2424
import org.databunker.options.TokenOptions;
2525
import org.databunker.options.PolicyOptions;
26+
import org.databunker.options.PatchOperation;
2627
import org.databunker.options.OptionsConverter;
2728

2829
import java.io.IOException;
@@ -89,7 +90,21 @@ private Map<String, Object> makeRequest(String endpoint, Map<String, Object> dat
8990
try (CloseableHttpResponse response = httpClient.execute(request)) {
9091
HttpEntity entity = response.getEntity();
9192
String responseString = EntityUtils.toString(entity);
92-
return objectMapper.readValue(responseString, Map.class);
93+
Map<String, Object> result = objectMapper.readValue(responseString, Map.class);
94+
95+
if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 300) {
96+
if (result.containsKey("status")) {
97+
return result;
98+
} else {
99+
throw new IOException(result.containsKey("message") ?
100+
(String) result.get("message") : "API request failed");
101+
}
102+
}
103+
104+
return result;
105+
} catch (Exception error) {
106+
System.err.println("Error making request: " + error.getMessage());
107+
throw new IOException("API request failed", error);
93108
}
94109
}
95110

@@ -125,6 +140,9 @@ private byte[] rawRequest(String endpoint, Map<String, Object> data, Map<String,
125140
try (CloseableHttpResponse response = httpClient.execute(request)) {
126141
HttpEntity entity = response.getEntity();
127142
return EntityUtils.toByteArray(entity);
143+
} catch (Exception error) {
144+
System.err.println("Error making raw request: " + error.getMessage());
145+
throw new IOException("Raw API request failed", error);
128146
}
129147
}
130148

@@ -217,6 +235,24 @@ public Map<String, Object> patchUser(String mode, String identity, Map<String, O
217235
return makeRequest("UserPatch", data, requestMetadata);
218236
}
219237

238+
/**
239+
* Patches a user with typed patch operations
240+
*
241+
* @param mode User identification mode
242+
* @param identity User's identifier
243+
* @param patch Array of patch operations
244+
* @param requestMetadata Optional request metadata
245+
* @return The patched user information
246+
* @throws IOException If an I/O error occurs
247+
*/
248+
public Map<String, Object> patchUser(String mode, String identity, PatchOperation[] patch, Map<String, Object> requestMetadata) throws IOException {
249+
Map<String, Object> data = new HashMap<>();
250+
data.put("mode", mode);
251+
data.put("identity", identity);
252+
data.put("patch", patch);
253+
return makeRequest("UserPatch", data, requestMetadata);
254+
}
255+
220256
public Map<String, Object> requestUserUpdate(String mode, String identity, Map<String, Object> profile, Map<String, Object> requestMetadata) throws IOException {
221257
Map<String, Object> data = new HashMap<>();
222258
data.put("mode", mode);
@@ -233,6 +269,24 @@ public Map<String, Object> requestUserPatch(String mode, String identity, Map<St
233269
return makeRequest("UserPatchRequest", data, requestMetadata);
234270
}
235271

272+
/**
273+
* Requests a user patch with typed patch operations
274+
*
275+
* @param mode User identification mode
276+
* @param identity User's identifier
277+
* @param patch Array of patch operations
278+
* @param requestMetadata Optional request metadata
279+
* @return The patch request result
280+
* @throws IOException If an I/O error occurs
281+
*/
282+
public Map<String, Object> requestUserPatch(String mode, String identity, PatchOperation[] patch, Map<String, Object> requestMetadata) throws IOException {
283+
Map<String, Object> data = new HashMap<>();
284+
data.put("mode", mode);
285+
data.put("identity", identity);
286+
data.put("patch", patch);
287+
return makeRequest("UserPatchRequest", data, requestMetadata);
288+
}
289+
236290
public Map<String, Object> deleteUser(String mode, String identity, Map<String, Object> requestMetadata) throws IOException {
237291
Map<String, Object> data = new HashMap<>();
238292
data.put("mode", mode);
@@ -485,6 +539,8 @@ public Map<String, Object> updateLegalBasis(String brief, LegalBasisOptions opti
485539
return makeRequest("LegalBasisUpdate", data, requestMetadata);
486540
}
487541

542+
543+
488544
public Map<String, Object> deleteLegalBasis(String brief, Map<String, Object> requestMetadata) throws IOException {
489545
Map<String, Object> data = new HashMap<>();
490546
data.put("brief", brief);
@@ -616,6 +672,8 @@ public Map<String, Object> updateProcessingActivity(String activity, ProcessingA
616672
return makeRequest("ProcessingActivityUpdate", data, requestMetadata);
617673
}
618674

675+
676+
619677
public Map<String, Object> deleteProcessingActivity(String activity, Map<String, Object> requestMetadata) throws IOException {
620678
Map<String, Object> data = new HashMap<>();
621679
data.put("activity", activity);
@@ -940,7 +998,7 @@ public Map<String, Object> createToken(String tokentype, String record, Map<Stri
940998
* @return The created tokens information
941999
* @throws IOException If an I/O error occurs
9421000
*/
943-
public Map<String, Object> createTokensBulk(Map<String, String>[] records, TokenOptions options, Map<String, Object> requestMetadata) throws IOException {
1001+
public Map<String, Object> createTokensBulk(Map<String, Object>[] records, TokenOptions options, Map<String, Object> requestMetadata) throws IOException {
9441002
Map<String, Object> data = new HashMap<>();
9451003
data.put("records", records);
9461004
if (options != null) {
@@ -957,7 +1015,7 @@ public Map<String, Object> createTokensBulk(Map<String, String>[] records, Token
9571015
* @return The created tokens information
9581016
* @throws IOException If an I/O error occurs
9591017
*/
960-
public Map<String, Object> createTokensBulk(Map<String, String>[] records, Map<String, Object> requestMetadata) throws IOException {
1018+
public Map<String, Object> createTokensBulk(Map<String, Object>[] records, Map<String, Object> requestMetadata) throws IOException {
9611019
return createTokensBulk(records, null, requestMetadata);
9621020
}
9631021

@@ -1152,6 +1210,8 @@ public Map<String, Object> updatePolicy(String policyid, PolicyOptions options,
11521210
return makeRequest("PolicyUpdate", data, requestMetadata);
11531211
}
11541212

1213+
1214+
11551215
public Map<String, Object> getPolicy(String policyref, Map<String, Object> requestMetadata) throws IOException {
11561216
Map<String, Object> data = new HashMap<>();
11571217
if (policyref != null) {
@@ -1353,6 +1413,24 @@ public Map<String, Object> getSystemStats(Map<String, Object> requestMetadata) t
13531413
return makeRequest("SystemGetSystemStats", null, requestMetadata);
13541414
}
13551415

1416+
/**
1417+
* Generates a wrapping key from three Shamir's Secret Sharing keys
1418+
*
1419+
* @param key1 First Shamir secret sharing key
1420+
* @param key2 Second Shamir secret sharing key
1421+
* @param key3 Third Shamir secret sharing key
1422+
* @param requestMetadata Optional request metadata
1423+
* @return Generated wrapping key
1424+
* @throws IOException If an I/O error occurs
1425+
*/
1426+
public Map<String, Object> generateWrappingKey(String key1, String key2, String key3, Map<String, Object> requestMetadata) throws IOException {
1427+
Map<String, Object> data = new HashMap<>();
1428+
data.put("key1", key1);
1429+
data.put("key2", key2);
1430+
data.put("key3", key3);
1431+
return makeRequest("SystemGenerateWrappingKey", data, requestMetadata);
1432+
}
1433+
13561434
public Map<String, Object> getSystemMetrics(Map<String, Object> requestMetadata) throws IOException {
13571435
String url = baseURL + "/metrics";
13581436
HttpGet request = new HttpGet(url);

0 commit comments

Comments
 (0)