Skip to content

Commit b3cde7c

Browse files
authored
Merge pull request #17 from orq-ai/set-deployment-stream-dynamically-in-hooks
fix: set deployment stream dynamically in hooks
2 parents 8f44dd6 + 3c6b1e5 commit b3cde7c

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from typing import Union
12
from .types import BeforeRequestContext, BeforeRequestHook
23

34
import json
45
import httpx
56

67

78
class GlobalHook(BeforeRequestHook):
8-
def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request) -> Union[requests.PreparedRequest, Exception]:
9+
def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request) -> Union[httpx.Request, Exception]:
910
contact_id = request.headers['contactId']
1011

1112
if contact_id:
@@ -19,6 +20,11 @@ def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request)
1920
raw_payload = request.content.decode('utf-8')
2021
payload = json.loads(raw_payload)
2122

23+
if hook_ctx.operation_id == 'DeploymentStream':
24+
payload['stream'] = True
25+
else:
26+
payload['stream'] = False
27+
2228
if 'context' in payload and type(payload['context']) is dict:
2329
payload['context']['environments'] = environment
2430
else:
@@ -33,4 +39,4 @@ def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request)
3339

3440
return httpx.Request(method=request.method, url=request.url, extensions=request.extensions, headers=request.headers, content=data)
3541

36-
return request
42+
return request

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +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())
14+
hooks.register_before_request_hook(GlobalHook())

src/orq_ai_sdk/_hooks/globalhook.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from typing import Union
12
from .types import BeforeRequestContext, BeforeRequestHook
23

34
import json
45
import httpx
56

67

78
class GlobalHook(BeforeRequestHook):
8-
def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request) -> Union[requests.PreparedRequest, Exception]:
9+
def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request) -> Union[httpx.Request, Exception]:
910
contact_id = request.headers['contactId']
1011

1112
if contact_id:
@@ -19,6 +20,11 @@ def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request)
1920
raw_payload = request.content.decode('utf-8')
2021
payload = json.loads(raw_payload)
2122

23+
if hook_ctx.operation_id == 'DeploymentStream':
24+
payload['stream'] = True
25+
else:
26+
payload['stream'] = False
27+
2228
if 'context' in payload and type(payload['context']) is dict:
2329
payload['context']['environments'] = environment
2430
else:
@@ -33,4 +39,4 @@ def before_request(self, hook_ctx: BeforeRequestContext, request: httpx.Request)
3339

3440
return httpx.Request(method=request.method, url=request.url, extensions=request.extensions, headers=request.headers, content=data)
3541

36-
return request
42+
return request

src/orq_ai_sdk/_hooks/registration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +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())
14+
hooks.register_before_request_hook(GlobalHook())

0 commit comments

Comments
 (0)