Skip to content

Commit d51b13d

Browse files
committed
fold into existing endpoint
1 parent 8ebe409 commit d51b13d

File tree

2 files changed

+16
-40
lines changed

2 files changed

+16
-40
lines changed

nightfall/api.py

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,24 @@ def __init__(self, key: Optional[str] = None, signing_secret: Optional[str] = No
5555
'Authorization': f'Bearer {self.key}',
5656
}
5757

58-
def scan_text(self, texts: List[str], detection_rules: Optional[List[DetectionRule]] = None,
58+
def scan_text(self, texts: List[str], policy_uuids: List[str] = None, detection_rules: Optional[List[DetectionRule]] = None,
5959
detection_rule_uuids: Optional[List[str]] = None, context_bytes: Optional[int] = None,
6060
default_redaction_config: Optional[RedactionConfig] = None) ->\
6161
Tuple[List[List[Finding]], List[str]]:
6262
"""Scan text with Nightfall.
6363
6464
This method takes the specified config and then makes
6565
one or more requests to the Nightfall API for scanning.
66-
At least one of detection_rule_uuids or detection_rules is required.
6766
67+
A caller must provide exactly one of the following:
68+
* a non-empty policy_uuids list (current maximum supported length = 1)
69+
* at least one of detection_rule_uuids or detection_rules
6870
6971
:param texts: List of strings to scan.
7072
:type texts: List[str]
73+
:param policy_uuids: List of policy UUIDs to scan each text with.
74+
These can be created in the Nightfall UI.
75+
:type policy_uuids: List[str] or None
7176
:param detection_rules: List of detection rules to scan each text with.
7277
:type detection_rules: List[DetectionRule] or None
7378
:param detection_rule_uuids: List of detection rule UUIDs to scan each text with.
@@ -81,8 +86,8 @@ def scan_text(self, texts: List[str], detection_rules: Optional[List[DetectionRu
8186
:returns: list of findings, list of redacted input texts
8287
"""
8388

84-
if not detection_rule_uuids and not detection_rules:
85-
raise NightfallUserError("at least one of detection_rule_uuids or detection_rules required", 40001)
89+
if not policy_uuids and not detection_rule_uuids and not detection_rules:
90+
raise NightfallUserError("at least one of policy_uuids, detection_rule_uuids, or detection_rules is required", 40001)
8691

8792
policy = {}
8893
if detection_rule_uuids:
@@ -93,43 +98,14 @@ def scan_text(self, texts: List[str], detection_rules: Optional[List[DetectionRu
9398
policy["contextBytes"] = context_bytes
9499
if default_redaction_config:
95100
policy["defaultRedactionConfig"] = default_redaction_config.as_dict()
96-
request_body = {
97-
"payload": texts,
98-
"policy": policy
99-
}
100-
response = self._scan_text_v3(request_body)
101-
102-
_validate_response(response, 200)
103-
104-
parsed_response = response.json()
105-
106-
findings = [[Finding.from_dict(f) for f in item_findings] for item_findings in parsed_response["findings"]]
107-
return findings, parsed_response.get("redactedPayload")
108-
109-
110-
def scan_text_with_policy_uuids(self, texts: List[str], policy_uuids: List[str] = None) ->\
111-
Tuple[List[List[Finding]], List[str]]:
112-
"""Scan text with Nightfall.
113-
114-
This method takes the specified config and then makes
115-
one or more requests to the Nightfall API for scanning.
116-
The maximum supported length of policy_uuids is currently 1.
117-
118-
:param texts: List of strings to scan.
119-
:type texts: List[str]
120-
:param policy_uuids: List of policy UUIDs to scan each text with.
121-
These can be created in the Nightfall UI.
122-
:type policy_uuids: List[str] or None
123-
:returns: list of findings, list of redacted input texts
124-
"""
125-
126-
if not policy_uuids:
127-
raise NightfallUserError("policy_uuids may not be empty", 40001)
128101

129102
request_body = {
130-
"payload": texts,
131-
"policyUUIDs": policy_uuids
103+
"payload": texts
132104
}
105+
if policy:
106+
request_body["policy"] = policy
107+
if policy_uuids:
108+
request_body["policyUUIDs"] = policy_uuids
133109
response = self._scan_text_v3(request_body)
134110

135111
_validate_response(response, 200)

tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,9 @@ def test_scan_text_with_policy_uuids():
388388
"491👀-👀👀👀👀-👀👀👀👀-👀👀👀👀 is my credit card number, [REDACTED] ssn"
389389
]
390390
})
391-
result, redactions = nightfall.scan_text_with_policy_uuids(
391+
result, redactions = nightfall.scan_text(
392392
["4916-6734-7572-5015 is my credit card number, 489-36-8350 ssn"],
393-
["2388f83f-cd31-4689-971b-4ee94f798281"]
393+
policy_uuids=["2388f83f-cd31-4689-971b-4ee94f798281"]
394394
)
395395

396396
assert len(responses.calls) == 1

0 commit comments

Comments
 (0)