-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreq_plugin_env.py
More file actions
253 lines (197 loc) · 6.84 KB
/
Copy pathtreq_plugin_env.py
File metadata and controls
253 lines (197 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env python3
"""
Example: Python Subprocess Plugin
This plugin demonstrates the subprocess plugin protocol:
- NDJSON communication over stdin/stdout
- Protocol initialization and capability declaration
- Custom resolvers ($env, $timestamp, $uuid)
- Hook implementation (request.before)
- Event subscription
Usage in treq.config.ts:
```typescript
import { defineConfig } from '@t-req/core';
export default defineConfig({
plugins: [
{
command: ['python3', './examples/plugins/treq_plugin_env.py'],
config: { prefix: 'TREQ_' }
},
],
});
```
Or in treq.jsonc:
```jsonc
{
"plugins": [
{
"command": ["python3", "./examples/plugins/treq_plugin_env.py"],
"config": { "prefix": "TREQ_" }
}
]
}
```
Usage in .http files:
```http
GET {{baseUrl}}/api/data
X-Request-ID: {{$uuid()}}
X-Timestamp: {{$timestamp()}}
X-Api-Key: {{$env('API_KEY')}}
```
"""
import json
import os
import sys
import uuid
from datetime import datetime, timezone
# Configuration (set during init)
config = {"prefix": ""}
def log_debug(message: str):
"""Log to stderr for debugging (won't interfere with protocol)."""
print(f"[treq-plugin-env] {message}", file=sys.stderr, flush=True)
def handle_init(msg: dict) -> dict:
"""Handle initialization request."""
global config
# Store configuration
if msg.get("config"):
config.update(msg["config"])
log_debug(f"Initialized with config: {config}")
return {
"name": "treq-plugin-env",
"version": "1.0.0",
"protocolVersion": 1,
"capabilities": ["resolvers", "hooks"],
"resolvers": ["$env", "$timestamp", "$uuid", "$randomInt"],
"hooks": ["request.before"],
"permissions": ["env"]
}
def handle_resolver(msg: dict) -> dict:
"""Handle resolver requests."""
name = msg.get("name", "")
args = msg.get("args", [])
if name == "$env":
# Get environment variable, optionally with prefix
if not args:
return {"value": ""}
var_name = args[0]
prefix = config.get("prefix", "")
full_name = f"{prefix}{var_name}" if prefix else var_name
value = os.environ.get(full_name, "")
log_debug(f"$env({var_name}) -> {full_name} = {'***' if value else '(empty)'}")
return {"value": value}
elif name == "$timestamp":
# Get current timestamp in ISO format or custom format
fmt = args[0] if args else "iso"
now = datetime.now(timezone.utc)
if fmt == "iso":
value = now.isoformat()
elif fmt == "unix":
value = str(int(now.timestamp()))
elif fmt == "unix_ms":
value = str(int(now.timestamp() * 1000))
else:
# Custom strftime format
value = now.strftime(fmt)
log_debug(f"$timestamp({fmt}) -> {value}")
return {"value": value}
elif name == "$uuid":
# Generate a UUID
version = args[0] if args else "4"
if version == "4":
value = str(uuid.uuid4())
elif version == "1":
value = str(uuid.uuid1())
else:
value = str(uuid.uuid4())
log_debug(f"$uuid({version}) -> {value}")
return {"value": value}
elif name == "$randomInt":
# Generate a random integer in range
import random
min_val = int(args[0]) if len(args) > 0 else 0
max_val = int(args[1]) if len(args) > 1 else 1000000
value = str(random.randint(min_val, max_val))
log_debug(f"$randomInt({min_val}, {max_val}) -> {value}")
return {"value": value}
else:
log_debug(f"Unknown resolver: {name}")
return {"value": ""}
def handle_hook(msg: dict) -> dict:
"""Handle hook requests."""
hook_name = msg.get("name", "")
input_data = msg.get("input", {})
output_data = msg.get("output", {})
if hook_name == "request.before":
# Add custom headers to every request
request = output_data.get("request", {})
headers = request.get("headers", {})
# Add a correlation ID if not present
if "X-Correlation-ID" not in headers:
headers["X-Correlation-ID"] = str(uuid.uuid4())
# Add plugin identification header
headers["X-Plugin"] = "treq-plugin-env/1.0.0"
request["headers"] = headers
output_data["request"] = request
log_debug(f"request.before: added correlation ID")
return {"output": output_data}
# For other hooks, return unchanged output
return {"output": output_data}
def handle_event(msg: dict):
"""Handle event notifications (fire-and-forget)."""
event = msg.get("event", {})
event_type = event.get("type", "")
# Just log events for debugging
if event_type == "fetchStarted":
log_debug(f"Event: {event.get('method')} {event.get('url')}")
elif event_type == "fetchFinished":
log_debug(f"Event: {event.get('method')} {event.get('url')} -> {event.get('status')}")
elif event_type == "error":
log_debug(f"Event: Error - {event.get('message')}")
def main():
"""Main loop: read NDJSON from stdin, write responses to stdout."""
log_debug("Plugin starting...")
for line in sys.stdin:
line = line.strip()
if not line:
continue
try:
msg = json.loads(line)
except json.JSONDecodeError as e:
log_debug(f"JSON parse error: {e}")
continue
msg_type = msg.get("type", "")
msg_id = msg.get("id", "")
try:
if msg_type == "init":
result = handle_init(msg)
response = {"id": msg_id, "type": "response", "result": result}
elif msg_type == "resolver":
result = handle_resolver(msg)
response = {"id": msg_id, "type": "response", "result": result}
elif msg_type == "hook":
result = handle_hook(msg)
response = {"id": msg_id, "type": "response", "result": result}
elif msg_type == "event":
handle_event(msg)
continue # Events don't send a response
elif msg_type == "shutdown":
log_debug("Shutdown received, exiting...")
break
else:
response = {
"id": msg_id,
"type": "error",
"error": {"message": f"Unknown message type: {msg_type}"}
}
# Write response as NDJSON
print(json.dumps(response), flush=True)
except Exception as e:
log_debug(f"Error handling message: {e}")
response = {
"id": msg_id,
"type": "error",
"error": {"message": str(e)}
}
print(json.dumps(response), flush=True)
log_debug("Plugin exiting")
if __name__ == "__main__":
main()