updated docs

This commit is contained in:
Stephen G Pope committed 2024-12-15 19:51:47 -08:00
1 parent c5f5cc2964
commit 1f59de5b2e
11 files changed
+341 -593

No files matched your search

+27 -36
View File
@@ -1,7 +1,7 @@
# `/v1/code/execute/python` API Documentation
## Overview
This endpoint allows users to execute Python code on the server. It accepts a Python code snippet, an optional timeout value, an optional webhook URL, and an optional ID. The code is executed in a secure environment, and the output (including stdout, stderr, and return value) is returned as a JSON response.
This endpoint allows users to execute Python code on the server. The code is executed in a secure, sandboxed environment with a configurable timeout. The endpoint returns the output (stdout, stderr) and the return value of the executed code.
## Endpoint
**URL Path:** `/v1/code/execute/python`
@@ -16,30 +16,23 @@ This endpoint allows users to execute Python code on the server. It accepts a Py
- `code` (string, required): The Python code to be executed.
- `timeout` (integer, optional): The maximum execution time in seconds, between 1 and 300. Default is 30 seconds.
- `webhook_url` (string, optional): A URL to receive a webhook notification upon completion.
- `id` (string, optional): An identifier for the execution request.
- `id` (string, optional): A unique identifier for the execution request.
### Example Request
```json
{
"code": "print('Hello, World!')",
"timeout": 10,
"webhook_url": "https://example.com/webhook",
"id": "abc123"
"code": "print('Hello, World!')",
"timeout": 10
}
```
```bash
curl -X POST \
https://api.example.com/v1/code/execute/python \
-H 'Authorization: Bearer <token>' \
-H 'Content-Type: application/json' \
-d '{
"code": "print('Hello, World!')",
"timeout": 10,
"webhook_url": "https://example.com/webhook",
"id": "abc123"
}'
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{"code": "print('Hello, World!')", "timeout": 10}' \
https://api.example.com/v1/code/execute/python
```
## Response
@@ -49,10 +42,10 @@ curl -X POST \
```json
{
"result": null,
"stdout": "Hello, World!\n",
"stderr": "",
"exit_code": 0
"result": null,
"stdout": "Hello, World!\n",
"stderr": "",
"exit_code": 0
}
```
@@ -61,9 +54,9 @@ curl -X POST \
```json
{
"error": "Execution failed",
"stdout": "",
"exit_code": 1
"error": "Execution failed",
"stdout": "",
"exit_code": 1
}
```
@@ -71,7 +64,7 @@ curl -X POST \
```json
{
"error": "Execution timed out after 10 seconds"
"error": "Execution timed out after 10 seconds"
}
```
@@ -79,30 +72,28 @@ curl -X POST \
```json
{
"error": "Failed to parse execution result",
"stdout": "...",
"stderr": "...",
"exit_code": 1
"error": "Failed to parse execution result"
}
```
## Error Handling
- Missing or invalid `code` parameter: `400 Bad Request`
- Execution timeout: `408 Request Timeout`
- Execution failure or error: `400 Bad Request` (with error details in the response)
- Execution failure or error: `400 Bad Request` with error details
- Internal server error: `500 Internal Server Error`
## Usage Notes
- The `code` parameter should contain valid Python code.
- The `timeout` parameter is optional and defaults to 30 seconds if not provided.
- The `webhook_url` and `id` parameters are optional and can be used for tracking or notification purposes.
- The executed code runs in a sandboxed environment, so it cannot access external resources or make system calls.
- The `timeout` parameter can be used to limit the execution time and prevent long-running or infinite loops.
- The `webhook_url` and `id` parameters are optional and can be used for tracking and notification purposes.
## Common Issues
- Code execution may fail due to syntax errors, runtime errors, or resource constraints.
- Long-running or resource-intensive code may be terminated due to the timeout.
- Code that takes too long to execute may be terminated due to the timeout.
- Code that attempts to access external resources or make system calls will fail.
- Large output (stdout, stderr) may be truncated or cause performance issues.
## Best Practices
- Keep the executed code concise and focused on the specific task.
- Use the `timeout` parameter to prevent long-running or infinite loops.
- Validate and sanitize user input to prevent code injection attacks.
- Set appropriate timeouts to prevent resource exhaustion.
- Implement rate limiting and access controls to prevent abuse.
- Log and monitor execution errors and failures for debugging and security purposes.
- Monitor and limit the usage of this endpoint to prevent abuse or resource exhaustion.