Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/complex_inputs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Complex Inputs with Multiple Files

In this example, we see how to author a Foundry Function authored in Python which can accept a JSON payload and a
collection of files as input.
The files are provided in two ways. The first is as an array and the second is as an individual field.
The files are read in full and then concatenated and returned.
JSON schemas are provided which show how to integrate successfully with Fusion.

### Fusion Workflow

<img src="fusion_screenshot.png" />
33 changes: 0 additions & 33 deletions examples/complex_inputs/complex_inputs_example.py

This file was deleted.

35 changes: 35 additions & 0 deletions examples/complex_inputs/functions/complex_input_tester/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from crowdstrike.foundry.function import Function, Request, Response, APIError
from logging import Logger

func = Function.instance()


@func.handler(method='POST', path='/complex-test')
def complex_handler(request: Request, config: [dict[str, any], None], logger: Logger) -> Response:
"""
Implement example function handler to showcase how to handle complex inputs.

This example demonstrates how to provide multiple inputs to a function, some of which happen to be files.
In this case, it simply echoes back the contents of those files concatenated together with spaces.
This could easily be changed to something more advanced to work with arbitrary binary.

:param request: :class:`Request` to handle.
:return: :class:`Response`
"""
greeting = f'Welcome {request.body.get("name", "<unknown>")}, age {request.body.get("age", "<unknown>")}'
file_contents = []
for v in request.files.values():
file_contents.append(v.decode('utf-8').strip())

all_text = ' '.join(file_contents)
return Response(
body={
'allText': all_text[0:max(len(all_text), 10000)], # returns up to the first 10,000 characters
'greeting': greeting,
},
code=200,
)


if __name__ == '__main__':
func.run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"files": {
"type": "array",
"title": "Files",
"description": "LogScale files to process. Expected to be JSON.",
"items": {
"type": "string",
"format": "downloadableFile"
}
},
"file2": {
"title": "Other File",
"description": "Some other file field.",
"type": "string",
"format": "downloadableFile"
},
"name": {
"type": "string",
"title": "Name",
"description": "First name of the user."
},
"age": {
"type": "integer",
"title": "Age",
"description": "Age of the user."
}
},
"required": [
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crowdstrike-foundry-function==1.1.3
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"allText": {
"type": "string",
"title": "File Contents"
},
"greeting": {
"type": "string",
"title": "Greeting"
}
},
"required": [
]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
curl -X POST --location "http://localhost:8081" \
-H "Content-Type: multipart/form-data" \
-F meta='{
"url": "/my-endpoint",
"url": "/complex-test",
"method": "POST"
}' \
-F body='{
Expand Down
Binary file added examples/complex_inputs/fusion_screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions examples/complex_inputs/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: complex-fn-python-example
description: ""
logo: ""
vendor: ""
vendor_products: []
use_case: Cloud security
manifest_version: "2023-05-09"
ignored:
- .+/node_modules$
- .+/node_modules/.+
- .+/venv$
- .+/venv/.+
ui:
homepage: ""
extensions: []
pages: {}
dashboards: {}
navigation: {}
api_integrations: []
rtr_scripts: []
collections: []
auth:
scopes: []
permissions: {}
roles: []
functions:
- name: complex_input_tester
config: null
description: Exercises reading in a variety of input types, including an array of files.
path: functions/complex_input_tester
environment_variables: {}
handlers:
- name: handle_complex_inputs
description: Exercises reading in a variety of input types, including an array of files.
method: POST
api_path: /complex-test
payload_type: complex
request_schema: request_schema.json
response_schema: response_schema.json
workflow_integration:
disruptive: false
system_action: false
tags:
- complex-fn-python-example
- Utility
permissions: []
language: python
workflows: []
parsers: []
logscale:
saved_searches: []
lookup_files: []
docs: {}