|
1 | | -from databricks.sdk.service.serving import ServingEndpointsAPI |
| 1 | +import json as js |
| 2 | +from typing import Dict, Optional |
| 3 | + |
| 4 | +from databricks.sdk.service.serving import (ExternalFunctionRequestMethod, |
| 5 | + ExternalFunctionResponse, |
| 6 | + ServingEndpointsAPI) |
2 | 7 |
|
3 | 8 |
|
4 | 9 | class ServingEndpointsExt(ServingEndpointsAPI): |
@@ -50,3 +55,47 @@ def get_langchain_chat_open_ai_client(self, model): |
50 | 55 | openai_api_base=self._api._cfg.host + "/serving-endpoints", |
51 | 56 | api_key="no-token", # Passing in a placeholder to pass validations, this will not be used |
52 | 57 | http_client=self._get_authorized_http_client()) |
| 58 | + |
| 59 | + def http_request(self, |
| 60 | + conn: str, |
| 61 | + method: ExternalFunctionRequestMethod, |
| 62 | + path: str, |
| 63 | + *, |
| 64 | + headers: Optional[Dict[str, str]] = None, |
| 65 | + json: Optional[Dict[str, str]] = None, |
| 66 | + params: Optional[Dict[str, str]] = None) -> ExternalFunctionResponse: |
| 67 | + """Make external services call using the credentials stored in UC Connection. |
| 68 | +
|
| 69 | + :param conn: str |
| 70 | + The connection name to use. This is required to identify the external connection. |
| 71 | + :param method: :class:`ExternalFunctionRequestMethod` |
| 72 | + The HTTP method to use (e.g., 'GET', 'POST'). This is required. |
| 73 | + :param path: str |
| 74 | + The relative path for the API endpoint. This is required. |
| 75 | + :param headers: Dict[str,str] (optional) |
| 76 | + Additional headers for the request. If not provided, only auth headers from connections would be |
| 77 | + passed. |
| 78 | + :param json: Dict[str,str] (optional) |
| 79 | + JSON payload for the request. |
| 80 | + :param params: Dict[str,str] (optional) |
| 81 | + Query parameters for the request. |
| 82 | +
|
| 83 | + :returns: :class:`ExternalFunctionResponse` |
| 84 | + """ |
| 85 | + body = {} |
| 86 | + if conn is not None: |
| 87 | + body["conn"] = conn |
| 88 | + if headers is not None: |
| 89 | + body["headers"] = js.dumps(headers) |
| 90 | + if json is not None: |
| 91 | + body["json"] = js.dumps(json) |
| 92 | + if method is not None: |
| 93 | + body["method"] = method.value |
| 94 | + if params is not None: |
| 95 | + body["params"] = js.dumps(params) |
| 96 | + if path is not None: |
| 97 | + body["path"] = path |
| 98 | + headers = {"Accept": "application/json", "Content-Type": "application/json", } |
| 99 | + |
| 100 | + res = self._api.do("POST", "/external-function", body=body, headers=headers) |
| 101 | + return ExternalFunctionResponse.from_dict(res) |
0 commit comments