Skip to content

Commit 0bbd75b

Browse files
Add command-line SDK usage example classes (#49)
* Add simple command line example classes. * Update README to reference the example classes.
1 parent 2307913 commit 0bbd75b

File tree

3 files changed

+151
-50
lines changed

3 files changed

+151
-50
lines changed

README.md

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -63,30 +63,22 @@ snippet shows an example of how to scan using pre-built detectors.
6363

6464
#### Sample Code
6565

66-
```java
67-
// By default, the client reads the API key from the environment variable NIGHTFALL_API_KEY
68-
try (NightfallClient c = NightfallClient.Builder.defaultClient()) {
69-
70-
// Define some detectors to use to scan your data
71-
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
72-
creditCard.setMinConfidence(Confidence.LIKELY);
73-
creditCard.setMinNumFindings(1);
74-
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
75-
ssn.setMinConfidence(Confidence.POSSIBLE);
76-
ssn.setMinNumFindings(1);
77-
78-
// A rule contains a set of detectors to scan with
79-
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
80-
81-
List<String> payload = Arrays.asList("hello world", "my SSN is 678-99-8212", "4242-4242-4242-4242");
82-
ScanTextConfig config = ScanTextConfig.fromDetectionRuleUUIDs(Arrays.asList(rule), 20);
83-
ScanTextRequest req = new ScanTextRequest(payload, config);
84-
85-
ScanTextResponse response = c.scanText(req);
86-
System.out.println("findings: " + response.getFindings());
87-
}
66+
To run the [the TextScannerExample class](src/main/java/ai/nightfall/examples/TextScannerExample.java),
67+
68+
first compile:
69+
```bash
70+
make jar
71+
```
72+
73+
and then set your API key as an environment variable and run the sample program (changing version number in the jar if necessary):
74+
75+
```bash
76+
export NIGHTFALL_API_KEY="NF-XXXXXX" # replace with your API key
77+
java -cp build/scan-api-1.1.0.jar ai.nightfall.examples.FileScannerExample /path/to/file
8878
```
8979

80+
81+
9082
### Scanning Files
9183

9284
Scanning common file types like PDF's or office documents typically requires cumbersome text
@@ -101,37 +93,30 @@ provides a single method that wraps the steps required to upload your file. Plea
10193

10294
The file is uploaded synchronously, but as files can be arbitrarily large, the scan itself is conducted asynchronously.
10395
The results from the scan are delivered by webhook; for more information about setting up a webhook server, refer to
104-
[the docs](https://docs.nightfall.ai/docs/creating-a-webhook-server).
96+
[the webhook server docs](https://docs.nightfall.ai/docs/creating-a-webhook-server).
10597

10698
#### Sample Code
10799

108-
```java
109-
// By default, the client reads the API key from the environment variable NIGHTFALL_API_KEY
110-
try (NightfallClient c = NightfallClient.Builder.defaultClient()) {
111-
112-
// Define some detectors to use to scan your data
113-
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
114-
creditCard.setMinConfidence(Confidence.LIKELY);
115-
creditCard.setMinNumFindings(1);
116-
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
117-
ssn.setMinConfidence(Confidence.POSSIBLE);
118-
ssn.setMinNumFindings(1);
119-
120-
// A rule contains a set of detectors to scan with
121-
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
122-
123-
// File scans are conducted asynchronously, so provide a webhook route to an HTTPS server to send results to.
124-
String webhookResponseListenerURL = "https://my-service.com/nightfall/listener";
125-
ScanPolicy policy = ScanPolicy.fromDetectionRules(Arrays.asList(rule), webhookResponseListenerURL);
126-
ScanFileRequest req = new ScanFileRequest(policy, "my request metadata");
127-
128-
// Upload the data to the API, then trigger the async scan
129-
File file = new File("./super-secret-credit-cards.pdf");
130-
try (InputStream stream = new FileInputStream(file)) {
131-
ScanFileResponse response = c.scanFile(req, stream, file.length());
132-
System.out.println("started scan: " + response.toString());
133-
}
134-
}
100+
To run the [the FileScannerExample class](src/main/java/ai/nightfall/examples/FileScannerExample.java), first start a [webhook server](https://docs.nightfall.ai/docs/creating-a-webhook-server) to which results will be delivered. [ngrok](https://ngrok.com/) is a good way to expose a locally running webhook service on a publically-reachable URL:
101+
```bash
102+
# ngrok creates a public webhook URL that tunnels to your local machine. Change the port if you're not listening on port 8075.
103+
ngrok http 8075
104+
# copy the HTTPS URL ngrok displays, for example https://myurl.ngrok.io
105+
# supposing you're running a Python webhook server
106+
python webhook.py
107+
```
108+
109+
Compile the SDK and example code:
110+
```bash
111+
make jar
112+
```
113+
114+
then set your API key as an environment variable and run the sample program (changing version number in the jar if necessary):
115+
116+
```bash
117+
export NIGHTFALL_API_KEY="NF-XXXXXX" # replace with your API key
118+
NGROK_URL="myurl" # replace with the URL from running ngrok above
119+
java -cp build/scan-api-1.1.0.jar ai.nightfall.examples.FileScannerExample "$NGROK_URL" /path/to/file
135120
```
136121

137122

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package ai.nightfall.examples;
2+
3+
import ai.nightfall.scan.NightfallClient;
4+
import ai.nightfall.scan.model.Confidence;
5+
import ai.nightfall.scan.model.DetectionRule;
6+
import ai.nightfall.scan.model.Detector;
7+
import ai.nightfall.scan.model.LogicalOp;
8+
import ai.nightfall.scan.model.ScanFileRequest;
9+
import ai.nightfall.scan.model.ScanFileResponse;
10+
import ai.nightfall.scan.model.ScanPolicy;
11+
12+
import java.io.File;
13+
import java.io.FileInputStream;
14+
import java.io.IOException;
15+
import java.io.InputStream;
16+
import java.util.Arrays;
17+
18+
/**
19+
* FileScannerExample provides a simple example of a program that sends text to Nightfall's file detection API.
20+
* NIGHTFALL_API_KEY must be set as an environment variable, and webhookURL must be a running webhook endpoint
21+
* that is ready to receive a response.
22+
*
23+
* <p>For an example webhook server, see https://docs.nightfall.ai/docs/creating-a-webhook-server
24+
*/
25+
public class FileScannerExample {
26+
public static final String usage = "Usage: scanner <webhookURL> <filename>";
27+
28+
/**
29+
* Submit the provided files for scanning and print the result.
30+
*/
31+
public static void main(String[] args) {
32+
if (args.length != 2) {
33+
throw new RuntimeException(usage);
34+
}
35+
// File scans are conducted asynchronously, so provide a webhook route to an HTTPS server to send results to.
36+
String webhookResponseListenerURL = args[0];
37+
String file = args[1];
38+
try (NightfallClient client = NightfallClient.Builder.defaultClient()) {
39+
// Define some detectors to use to scan your data
40+
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
41+
creditCard.setMinConfidence(Confidence.LIKELY);
42+
creditCard.setMinNumFindings(1);
43+
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
44+
ssn.setMinConfidence(Confidence.POSSIBLE);
45+
ssn.setMinNumFindings(1);
46+
47+
// A rule contains a set of detectors to scan with
48+
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
49+
50+
ScanPolicy policy = ScanPolicy.fromDetectionRules(Arrays.asList(rule), webhookResponseListenerURL);
51+
ScanFileRequest req = new ScanFileRequest(policy, "my request metadata");
52+
53+
// Upload the data to the API, then trigger the async scan
54+
File file1 = new File(file);
55+
try (InputStream stream = new FileInputStream(file1)) {
56+
ScanFileResponse response = client.scanFile(req, stream, file1.length());
57+
System.out.println("started scan: " + response.toString());
58+
} catch (IOException e) {
59+
e.printStackTrace();
60+
}
61+
}
62+
}
63+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package ai.nightfall.examples;
2+
3+
4+
import ai.nightfall.scan.NightfallClient;
5+
import ai.nightfall.scan.model.Confidence;
6+
import ai.nightfall.scan.model.DetectionRule;
7+
import ai.nightfall.scan.model.Detector;
8+
import ai.nightfall.scan.model.LogicalOp;
9+
import ai.nightfall.scan.model.ScanTextConfig;
10+
import ai.nightfall.scan.model.ScanTextRequest;
11+
import ai.nightfall.scan.model.ScanTextResponse;
12+
13+
import java.util.Arrays;
14+
import java.util.List;
15+
16+
17+
/**
18+
* TextScannerExample provides a simple example of a program that sends text to Nightfall's detection API.
19+
* NIGHTFALL_API_KEY must be set as an environment variable.
20+
*/
21+
public class TextScannerExample {
22+
public static final String usage = "Usage: scanner <string> ...";
23+
24+
/**
25+
* Submit the provided args for scanning and print the result.
26+
*/
27+
public static void main(String[] args) {
28+
if (args.length == 0) {
29+
throw new RuntimeException(usage);
30+
}
31+
try (NightfallClient client = NightfallClient.Builder.defaultClient()) {
32+
33+
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
34+
creditCard.setMinConfidence(Confidence.LIKELY);
35+
creditCard.setMinNumFindings(1);
36+
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
37+
ssn.setMinConfidence(Confidence.POSSIBLE);
38+
ssn.setMinNumFindings(1);
39+
40+
// A rule contains a set of detectors to scan with
41+
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
42+
43+
List<String> payload = Arrays.asList(args).subList(1, args.length);
44+
// Define some detectors to use to scan your data
45+
ScanTextConfig config = ScanTextConfig.fromDetectionRules(Arrays.asList(rule), 20);
46+
ScanTextRequest req = new ScanTextRequest(payload, config);
47+
48+
ScanTextResponse response = client.scanText(req);
49+
System.out.println("findings: " + response.getFindings());
50+
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)