Skip to content

Commit 8914359

Browse files
committed
Migrate collection operations from APIHarnessV2 to CustomStorage
Use CustomStorage service class instead of APIHarnessV2 (Uber class) for collection CRUD operations. Service class imports enable the Foundry Functions Editor to auto-detect required OAuth scopes, which is not possible with Uber class .command() calls. Changes: - Replace APIHarnessV2 with CustomStorage(ext_headers=_app_headers()) - Replace .command("PutObject"/"SearchObjects") with .PutObject()/.SearchObjects() - Add defensive .get() for SearchObjects response body - Move header construction to _app_headers() helper
1 parent 733f87a commit 8914359

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

functions/log-event/main.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
import uuid
66

77
from crowdstrike.foundry.function import Function, Request, Response, APIError
8-
from falconpy import APIHarnessV2
8+
from falconpy import CustomStorage
99

1010
FUNC = Function.instance()
1111

1212

13+
def _app_headers() -> dict:
14+
"""Build app headers for CustomStorage construction."""
15+
app_id = os.environ.get("APP_ID")
16+
if app_id:
17+
return {"X-CS-APP-ID": app_id}
18+
return {}
19+
20+
1321
@FUNC.handler(method="POST", path="/log-event")
1422
def on_post(request: Request) -> Response:
1523
"""
@@ -40,22 +48,12 @@ def on_post(request: Request) -> Response:
4048
"timestamp": int(time.time())
4149
}
4250

43-
# Allow setting APP_ID as an env variable for local testing
44-
headers = {}
45-
if os.environ.get("APP_ID"):
46-
headers = {
47-
"X-CS-APP-ID": os.environ.get("APP_ID")
48-
}
49-
50-
api_client = APIHarnessV2()
51+
api_client = CustomStorage(ext_headers=_app_headers())
5152
collection_name = "event_logs"
5253

53-
response = api_client.command("PutObject",
54-
body=json_data,
55-
collection_name=collection_name,
56-
object_key=event_id,
57-
headers=headers
58-
)
54+
response = api_client.PutObject(body=json_data,
55+
collection_name=collection_name,
56+
object_key=event_id)
5957

6058
if response["status_code"] != 200:
6159
error_message = response.get("error", {}).get("message", "Unknown error")
@@ -68,17 +66,14 @@ def on_post(request: Request) -> Response:
6866
)
6967

7068
# Query the collection to retrieve the event by id
71-
query_response = api_client.command("SearchObjects",
72-
filter=f"event_id:'{event_id}'",
73-
collection_name=collection_name,
74-
limit=5,
75-
headers=headers
76-
)
69+
query_response = api_client.SearchObjects(filter=f"event_id:'{event_id}'",
70+
collection_name=collection_name,
71+
limit=5)
7772

7873
return Response(
7974
body={
8075
"stored": True,
81-
"metadata": query_response.get("body").get("resources", [])
76+
"metadata": query_response.get("body", {}).get("resources", [])
8277
},
8378
code=200
8479
)

0 commit comments

Comments
 (0)