Skip to content

Commit 11dc69d

Browse files
author
Harold James Quilang
committed
feat: support global parameters for contact id and deployment context environment
1 parent 8a8d605 commit 11dc69d

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from .types import BeforeRequestContext, BeforeRequestHook
2+
3+
import json
4+
import httpx
5+
6+
7+
class GlobalHook(BeforeRequestHook):
8+
def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request) -> Union[requests.PreparedRequest, Exception]:
9+
contact_id = request.headers['contactId']
10+
11+
if contact_id:
12+
del request.headers['contactId']
13+
request.headers['X-ORQ-CONTACT-ID'] = contact_id
14+
15+
environment = request.headers['environment']
16+
17+
if hook_ctx.operation_id in ["DeploymentInvoke", "DeploymentStream"] and environment:
18+
del request.headers['environment']
19+
raw_payload = request.content.decode('utf-8')
20+
payload = json.loads(raw_payload)
21+
22+
if 'context' in payload and type(payload['context']) is dict:
23+
payload['context']['environment'] = environment
24+
else:
25+
payload['context'] = {
26+
'environment': environment
27+
}
28+
29+
data = json.dumps(payload).encode('utf-8')
30+
31+
# solve error related to Too much declared Content-Length (will not be dynamically set if its also pass in the init below for httpx.Request)
32+
del request.headers['Content-Length']
33+
34+
return httpx.Request(method=request.method, url=request.url, extensions=request.extensions, headers=request.headers, content=data)
35+
36+
return request

packages/orq-rc/src/orq_ai_sdk/_hooks/registration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .types import Hooks
2-
2+
from .globalhook import GlobalHook
33

44
# This file is only ever generated once on the first generation and then is free to be modified.
55
# Any hooks you wish to add should be registered in the init_hooks function. Feel free to define them
@@ -11,3 +11,4 @@ def init_hooks(hooks: Hooks):
1111
"""Add hooks by calling hooks.register{sdk_init/before_request/after_success/after_error}Hook
1212
with an instance of a hook that implements that specific Hook interface
1313
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance"""
14+
hooks.register_before_request_hook(GlobalHook())

src/orq_ai_sdk/_hooks/globalhook.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from .types import BeforeRequestContext, BeforeRequestHook
2+
3+
import json
4+
import httpx
5+
6+
7+
class GlobalHook(BeforeRequestHook):
8+
def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request) -> Union[requests.PreparedRequest, Exception]:
9+
contact_id = request.headers['contactId']
10+
11+
if contact_id:
12+
del request.headers['contactId']
13+
request.headers['X-ORQ-CONTACT-ID'] = contact_id
14+
15+
environment = request.headers['environment']
16+
17+
if hook_ctx.operation_id in ["DeploymentInvoke", "DeploymentStream"] and environment:
18+
del request.headers['environment']
19+
raw_payload = request.content.decode('utf-8')
20+
payload = json.loads(raw_payload)
21+
22+
if 'context' in payload and type(payload['context']) is dict:
23+
payload['context']['environment'] = environment
24+
else:
25+
payload['context'] = {
26+
'environment': environment
27+
}
28+
29+
data = json.dumps(payload).encode('utf-8')
30+
31+
# solve error related to Too much declared Content-Length (will not be dynamically set if its also pass in the init below for httpx.Request)
32+
del request.headers['Content-Length']
33+
34+
return httpx.Request(method=request.method, url=request.url, extensions=request.extensions, headers=request.headers, content=data)
35+
36+
return request

src/orq_ai_sdk/_hooks/registration.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .types import Hooks
2-
2+
from .globalhook import GlobalHook
33

44
# This file is only ever generated once on the first generation and then is free to be modified.
55
# Any hooks you wish to add should be registered in the init_hooks function. Feel free to define them
@@ -11,3 +11,4 @@ def init_hooks(hooks: Hooks):
1111
"""Add hooks by calling hooks.register{sdk_init/before_request/after_success/after_error}Hook
1212
with an instance of a hook that implements that specific Hook interface
1313
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance"""
14+
hooks.register_before_request_hook(GlobalHook())

0 commit comments

Comments
 (0)