Skip to content

Commit

Permalink
[OPIK-558] Adapt Fern for TypeScript SDK (#972)
Browse files Browse the repository at this point in the history
* Add support for global headers in the Fern client and different configuration for TS SDK

* Regenerate auto-generated code based on the new config
  • Loading branch information
ferc authored Dec 31, 2024
1 parent 7dc5889 commit 8f29976
Show file tree
Hide file tree
Showing 1,539 changed files with 18,588 additions and 24,863 deletions.
4 changes: 2 additions & 2 deletions sdks/code_generation/fern/fern.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"organization": "Opik",
"version": "0.45.0"
"organization": "Opik",
"version": "0.46.17"
}
4 changes: 4 additions & 0 deletions sdks/code_generation/fern/generators.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
api:
path: openapi/openapi.yaml
overrides: openapi/overrides-global-headers.yaml
default-group: local
groups:
local:
Expand All @@ -14,3 +15,6 @@ groups:
output:
location: local-file-system
path: ../../typescript/src/opik/rest_api
config:
outputSourceFiles: true
includeCredentialsOnCrossOriginRequests: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
x-fern-global-headers:
- header: Authorization
name: apiKey
optional: true
- header: Comet-Workspace
name: workspaceName
optional: true
10 changes: 8 additions & 2 deletions sdks/python/src/opik/rest_api/chat_completions/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ def create_chat_completions(
--------
from Opik import OpikApi
client = OpikApi()
client = OpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
client.chat_completions.create_chat_completions()
"""
_response = self._client_wrapper.httpx_client.request(
Expand Down Expand Up @@ -268,7 +271,10 @@ async def create_chat_completions(
from Opik import AsyncOpikApi
client = AsyncOpikApi()
client = AsyncOpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
async def main() -> None:
Expand Down
10 changes: 8 additions & 2 deletions sdks/python/src/opik/rest_api/check/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ def access(
--------
from Opik import OpikApi
client = OpikApi()
client = OpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
client.check.access(
request={"key": "value"},
)
Expand Down Expand Up @@ -115,7 +118,10 @@ async def access(
from Opik import AsyncOpikApi
client = AsyncOpikApi()
client = AsyncOpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
async def main() -> None:
Expand Down
42 changes: 36 additions & 6 deletions sdks/python/src/opik/rest_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class OpikApi:
api_key : typing.Optional[str]
workspace_name : typing.Optional[str]
timeout : typing.Optional[float]
The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.
Expand All @@ -64,14 +66,19 @@ class OpikApi:
--------
from Opik import OpikApi
client = OpikApi()
client = OpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
"""

def __init__(
self,
*,
base_url: typing.Optional[str] = None,
environment: OpikApiEnvironment = OpikApiEnvironment.DEFAULT,
api_key: typing.Optional[str] = None,
workspace_name: typing.Optional[str] = None,
timeout: typing.Optional[float] = None,
follow_redirects: typing.Optional[bool] = True,
httpx_client: typing.Optional[httpx.Client] = None,
Expand All @@ -81,6 +88,8 @@ def __init__(
)
self._client_wrapper = SyncClientWrapper(
base_url=_get_base_url(base_url=base_url, environment=environment),
api_key=api_key,
workspace_name=workspace_name,
httpx_client=httpx_client
if httpx_client is not None
else httpx.Client(
Expand Down Expand Up @@ -126,7 +135,10 @@ def is_alive(
--------
from Opik import OpikApi
client = OpikApi()
client = OpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
client.is_alive()
"""
_response = self._client_wrapper.httpx_client.request(
Expand Down Expand Up @@ -166,7 +178,10 @@ def version(
--------
from Opik import OpikApi
client = OpikApi()
client = OpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
client.version()
"""
_response = self._client_wrapper.httpx_client.request(
Expand Down Expand Up @@ -207,6 +222,8 @@ class AsyncOpikApi:
api_key : typing.Optional[str]
workspace_name : typing.Optional[str]
timeout : typing.Optional[float]
The timeout to be used, in seconds, for requests. By default the timeout is 60 seconds, unless a custom httpx client is used, in which case this default is not enforced.
Expand All @@ -220,14 +237,19 @@ class AsyncOpikApi:
--------
from Opik import AsyncOpikApi
client = AsyncOpikApi()
client = AsyncOpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
"""

def __init__(
self,
*,
base_url: typing.Optional[str] = None,
environment: OpikApiEnvironment = OpikApiEnvironment.DEFAULT,
api_key: typing.Optional[str] = None,
workspace_name: typing.Optional[str] = None,
timeout: typing.Optional[float] = None,
follow_redirects: typing.Optional[bool] = True,
httpx_client: typing.Optional[httpx.AsyncClient] = None,
Expand All @@ -237,6 +259,8 @@ def __init__(
)
self._client_wrapper = AsyncClientWrapper(
base_url=_get_base_url(base_url=base_url, environment=environment),
api_key=api_key,
workspace_name=workspace_name,
httpx_client=httpx_client
if httpx_client is not None
else httpx.AsyncClient(
Expand Down Expand Up @@ -284,7 +308,10 @@ async def is_alive(
from Opik import AsyncOpikApi
client = AsyncOpikApi()
client = AsyncOpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
async def main() -> None:
Expand Down Expand Up @@ -332,7 +359,10 @@ async def version(
from Opik import AsyncOpikApi
client = AsyncOpikApi()
client = AsyncOpikApi(
api_key="YOUR_API_KEY",
workspace_name="YOUR_WORKSPACE_NAME",
)
async def main() -> None:
Expand Down
33 changes: 30 additions & 3 deletions sdks/python/src/opik/rest_api/core/client_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@


class BaseClientWrapper:
def __init__(self, *, base_url: str, timeout: typing.Optional[float] = None):
def __init__(
self,
*,
api_key: typing.Optional[str] = None,
workspace_name: typing.Optional[str] = None,
base_url: str,
timeout: typing.Optional[float] = None,
):
self._api_key = api_key
self._workspace_name = workspace_name
self._base_url = base_url
self._timeout = timeout

def get_headers(self) -> typing.Dict[str, str]:
headers: typing.Dict[str, str] = {
"X-Fern-Language": "Python",
}
if self._api_key is not None:
headers["Authorization"] = self._api_key
if self._workspace_name is not None:
headers["Comet-Workspace"] = self._workspace_name
return headers

def get_base_url(self) -> str:
Expand All @@ -28,11 +41,18 @@ class SyncClientWrapper(BaseClientWrapper):
def __init__(
self,
*,
api_key: typing.Optional[str] = None,
workspace_name: typing.Optional[str] = None,
base_url: str,
timeout: typing.Optional[float] = None,
httpx_client: httpx.Client,
):
super().__init__(base_url=base_url, timeout=timeout)
super().__init__(
api_key=api_key,
workspace_name=workspace_name,
base_url=base_url,
timeout=timeout,
)
self.httpx_client = HttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
Expand All @@ -45,11 +65,18 @@ class AsyncClientWrapper(BaseClientWrapper):
def __init__(
self,
*,
api_key: typing.Optional[str] = None,
workspace_name: typing.Optional[str] = None,
base_url: str,
timeout: typing.Optional[float] = None,
httpx_client: httpx.AsyncClient,
):
super().__init__(base_url=base_url, timeout=timeout)
super().__init__(
api_key=api_key,
workspace_name=workspace_name,
base_url=base_url,
timeout=timeout,
)
self.httpx_client = AsyncHttpClient(
httpx_client=httpx_client,
base_headers=self.get_headers,
Expand Down
Loading

0 comments on commit 8f29976

Please sign in to comment.