mirror of
https://github.com/wassname/openrouter-python-sdk-retry-errors.git
synced 2026-07-30 12:20:57 +08:00
Initial commit: OpenRouter Python SDK
Auto-generated Python SDK for OpenRouter API, providing comprehensive client library with: - Chat completions and streaming support - Embeddings API - Model and provider information - API key management - Analytics and usage tracking - OAuth authentication flows - Full type hints and error handling
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
||||
|
||||
from .sdkhooks import *
|
||||
from .types import *
|
||||
from .registration import *
|
||||
@@ -0,0 +1,13 @@
|
||||
from .types import Hooks
|
||||
|
||||
|
||||
# This file is only ever generated once on the first generation and then is free to be modified.
|
||||
# Any hooks you wish to add should be registered in the init_hooks function. Feel free to define them
|
||||
# in this file or in separate files in the hooks folder.
|
||||
|
||||
|
||||
def init_hooks(hooks: Hooks):
|
||||
# pylint: disable=unused-argument
|
||||
"""Add hooks by calling hooks.register{sdk_init/before_request/after_success/after_error}Hook
|
||||
with an instance of a hook that implements that specific Hook interface
|
||||
Hooks are registered per SDK instance, and are valid for the lifetime of the SDK instance"""
|
||||
@@ -0,0 +1,76 @@
|
||||
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
||||
|
||||
import httpx
|
||||
from .types import (
|
||||
SDKInitHook,
|
||||
BeforeRequestContext,
|
||||
BeforeRequestHook,
|
||||
AfterSuccessContext,
|
||||
AfterSuccessHook,
|
||||
AfterErrorContext,
|
||||
AfterErrorHook,
|
||||
Hooks,
|
||||
)
|
||||
from .registration import init_hooks
|
||||
from typing import List, Optional, Tuple
|
||||
from openrouter.sdkconfiguration import SDKConfiguration
|
||||
|
||||
|
||||
class SDKHooks(Hooks):
|
||||
def __init__(self) -> None:
|
||||
self.sdk_init_hooks: List[SDKInitHook] = []
|
||||
self.before_request_hooks: List[BeforeRequestHook] = []
|
||||
self.after_success_hooks: List[AfterSuccessHook] = []
|
||||
self.after_error_hooks: List[AfterErrorHook] = []
|
||||
init_hooks(self)
|
||||
|
||||
def register_sdk_init_hook(self, hook: SDKInitHook) -> None:
|
||||
self.sdk_init_hooks.append(hook)
|
||||
|
||||
def register_before_request_hook(self, hook: BeforeRequestHook) -> None:
|
||||
self.before_request_hooks.append(hook)
|
||||
|
||||
def register_after_success_hook(self, hook: AfterSuccessHook) -> None:
|
||||
self.after_success_hooks.append(hook)
|
||||
|
||||
def register_after_error_hook(self, hook: AfterErrorHook) -> None:
|
||||
self.after_error_hooks.append(hook)
|
||||
|
||||
def sdk_init(self, config: SDKConfiguration) -> SDKConfiguration:
|
||||
for hook in self.sdk_init_hooks:
|
||||
config = hook.sdk_init(config)
|
||||
return config
|
||||
|
||||
def before_request(
|
||||
self, hook_ctx: BeforeRequestContext, request: httpx.Request
|
||||
) -> httpx.Request:
|
||||
for hook in self.before_request_hooks:
|
||||
out = hook.before_request(hook_ctx, request)
|
||||
if isinstance(out, Exception):
|
||||
raise out
|
||||
request = out
|
||||
|
||||
return request
|
||||
|
||||
def after_success(
|
||||
self, hook_ctx: AfterSuccessContext, response: httpx.Response
|
||||
) -> httpx.Response:
|
||||
for hook in self.after_success_hooks:
|
||||
out = hook.after_success(hook_ctx, response)
|
||||
if isinstance(out, Exception):
|
||||
raise out
|
||||
response = out
|
||||
return response
|
||||
|
||||
def after_error(
|
||||
self,
|
||||
hook_ctx: AfterErrorContext,
|
||||
response: Optional[httpx.Response],
|
||||
error: Optional[Exception],
|
||||
) -> Tuple[Optional[httpx.Response], Optional[Exception]]:
|
||||
for hook in self.after_error_hooks:
|
||||
result = hook.after_error(hook_ctx, response, error)
|
||||
if isinstance(result, Exception):
|
||||
raise result
|
||||
response, error = result
|
||||
return response, error
|
||||
@@ -0,0 +1,112 @@
|
||||
"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
import httpx
|
||||
from openrouter.sdkconfiguration import SDKConfiguration
|
||||
from typing import Any, Callable, List, Optional, Tuple, Union
|
||||
|
||||
|
||||
class HookContext:
|
||||
config: SDKConfiguration
|
||||
base_url: str
|
||||
operation_id: str
|
||||
oauth2_scopes: Optional[List[str]] = None
|
||||
security_source: Optional[Union[Any, Callable[[], Any]]] = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: SDKConfiguration,
|
||||
base_url: str,
|
||||
operation_id: str,
|
||||
oauth2_scopes: Optional[List[str]],
|
||||
security_source: Optional[Union[Any, Callable[[], Any]]],
|
||||
):
|
||||
self.config = config
|
||||
self.base_url = base_url
|
||||
self.operation_id = operation_id
|
||||
self.oauth2_scopes = oauth2_scopes
|
||||
self.security_source = security_source
|
||||
|
||||
|
||||
class BeforeRequestContext(HookContext):
|
||||
def __init__(self, hook_ctx: HookContext):
|
||||
super().__init__(
|
||||
hook_ctx.config,
|
||||
hook_ctx.base_url,
|
||||
hook_ctx.operation_id,
|
||||
hook_ctx.oauth2_scopes,
|
||||
hook_ctx.security_source,
|
||||
)
|
||||
|
||||
|
||||
class AfterSuccessContext(HookContext):
|
||||
def __init__(self, hook_ctx: HookContext):
|
||||
super().__init__(
|
||||
hook_ctx.config,
|
||||
hook_ctx.base_url,
|
||||
hook_ctx.operation_id,
|
||||
hook_ctx.oauth2_scopes,
|
||||
hook_ctx.security_source,
|
||||
)
|
||||
|
||||
|
||||
class AfterErrorContext(HookContext):
|
||||
def __init__(self, hook_ctx: HookContext):
|
||||
super().__init__(
|
||||
hook_ctx.config,
|
||||
hook_ctx.base_url,
|
||||
hook_ctx.operation_id,
|
||||
hook_ctx.oauth2_scopes,
|
||||
hook_ctx.security_source,
|
||||
)
|
||||
|
||||
|
||||
class SDKInitHook(ABC):
|
||||
@abstractmethod
|
||||
def sdk_init(self, config: SDKConfiguration) -> SDKConfiguration:
|
||||
pass
|
||||
|
||||
|
||||
class BeforeRequestHook(ABC):
|
||||
@abstractmethod
|
||||
def before_request(
|
||||
self, hook_ctx: BeforeRequestContext, request: httpx.Request
|
||||
) -> Union[httpx.Request, Exception]:
|
||||
pass
|
||||
|
||||
|
||||
class AfterSuccessHook(ABC):
|
||||
@abstractmethod
|
||||
def after_success(
|
||||
self, hook_ctx: AfterSuccessContext, response: httpx.Response
|
||||
) -> Union[httpx.Response, Exception]:
|
||||
pass
|
||||
|
||||
|
||||
class AfterErrorHook(ABC):
|
||||
@abstractmethod
|
||||
def after_error(
|
||||
self,
|
||||
hook_ctx: AfterErrorContext,
|
||||
response: Optional[httpx.Response],
|
||||
error: Optional[Exception],
|
||||
) -> Union[Tuple[Optional[httpx.Response], Optional[Exception]], Exception]:
|
||||
pass
|
||||
|
||||
|
||||
class Hooks(ABC):
|
||||
@abstractmethod
|
||||
def register_sdk_init_hook(self, hook: SDKInitHook):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def register_before_request_hook(self, hook: BeforeRequestHook):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def register_after_success_hook(self, hook: AfterSuccessHook):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def register_after_error_hook(self, hook: AfterErrorHook):
|
||||
pass
|
||||
Reference in New Issue
Block a user