dify
This commit is contained in:
21
dify/sdks/python-client/LICENSE
Normal file
21
dify/sdks/python-client/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 LangGenius
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
3
dify/sdks/python-client/MANIFEST.in
Normal file
3
dify/sdks/python-client/MANIFEST.in
Normal file
@@ -0,0 +1,3 @@
|
||||
recursive-include dify_client *.py
|
||||
include README.md
|
||||
include LICENSE
|
||||
409
dify/sdks/python-client/README.md
Normal file
409
dify/sdks/python-client/README.md
Normal file
@@ -0,0 +1,409 @@
|
||||
# dify-client
|
||||
|
||||
A Dify App Service-API Client, using for build a webapp by request Service-API
|
||||
|
||||
## Usage
|
||||
|
||||
First, install `dify-client` python sdk package:
|
||||
|
||||
```
|
||||
pip install dify-client
|
||||
```
|
||||
|
||||
### Synchronous Usage
|
||||
|
||||
Write your code with sdk:
|
||||
|
||||
- completion generate with `blocking` response_mode
|
||||
|
||||
```python
|
||||
from dify_client import CompletionClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize CompletionClient
|
||||
completion_client = CompletionClient(api_key)
|
||||
|
||||
# Create Completion Message using CompletionClient
|
||||
completion_response = completion_client.create_completion_message(inputs={"query": "What's the weather like today?"},
|
||||
response_mode="blocking", user="user_id")
|
||||
completion_response.raise_for_status()
|
||||
|
||||
result = completion_response.json()
|
||||
|
||||
print(result.get('answer'))
|
||||
```
|
||||
|
||||
- completion using vision model, like gpt-4-vision
|
||||
|
||||
```python
|
||||
from dify_client import CompletionClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize CompletionClient
|
||||
completion_client = CompletionClient(api_key)
|
||||
|
||||
files = [{
|
||||
"type": "image",
|
||||
"transfer_method": "remote_url",
|
||||
"url": "your_image_url"
|
||||
}]
|
||||
|
||||
# files = [{
|
||||
# "type": "image",
|
||||
# "transfer_method": "local_file",
|
||||
# "upload_file_id": "your_file_id"
|
||||
# }]
|
||||
|
||||
# Create Completion Message using CompletionClient
|
||||
completion_response = completion_client.create_completion_message(inputs={"query": "Describe the picture."},
|
||||
response_mode="blocking", user="user_id", files=files)
|
||||
completion_response.raise_for_status()
|
||||
|
||||
result = completion_response.json()
|
||||
|
||||
print(result.get('answer'))
|
||||
```
|
||||
|
||||
- chat generate with `streaming` response_mode
|
||||
|
||||
```python
|
||||
import json
|
||||
from dify_client import ChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize ChatClient
|
||||
chat_client = ChatClient(api_key)
|
||||
|
||||
# Create Chat Message using ChatClient
|
||||
chat_response = chat_client.create_chat_message(inputs={}, query="Hello", user="user_id", response_mode="streaming")
|
||||
chat_response.raise_for_status()
|
||||
|
||||
for line in chat_response.iter_lines(decode_unicode=True):
|
||||
line = line.split('data:', 1)[-1]
|
||||
if line.strip():
|
||||
line = json.loads(line.strip())
|
||||
print(line.get('answer'))
|
||||
```
|
||||
|
||||
- chat using vision model, like gpt-4-vision
|
||||
|
||||
```python
|
||||
from dify_client import ChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize ChatClient
|
||||
chat_client = ChatClient(api_key)
|
||||
|
||||
files = [{
|
||||
"type": "image",
|
||||
"transfer_method": "remote_url",
|
||||
"url": "your_image_url"
|
||||
}]
|
||||
|
||||
# files = [{
|
||||
# "type": "image",
|
||||
# "transfer_method": "local_file",
|
||||
# "upload_file_id": "your_file_id"
|
||||
# }]
|
||||
|
||||
# Create Chat Message using ChatClient
|
||||
chat_response = chat_client.create_chat_message(inputs={}, query="Describe the picture.", user="user_id",
|
||||
response_mode="blocking", files=files)
|
||||
chat_response.raise_for_status()
|
||||
|
||||
result = chat_response.json()
|
||||
|
||||
print(result.get("answer"))
|
||||
```
|
||||
|
||||
- upload file when using vision model
|
||||
|
||||
```python
|
||||
from dify_client import DifyClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize Client
|
||||
dify_client = DifyClient(api_key)
|
||||
|
||||
file_path = "your_image_file_path"
|
||||
file_name = "panda.jpeg"
|
||||
mime_type = "image/jpeg"
|
||||
|
||||
with open(file_path, "rb") as file:
|
||||
files = {
|
||||
"file": (file_name, file, mime_type)
|
||||
}
|
||||
response = dify_client.file_upload("user_id", files)
|
||||
|
||||
result = response.json()
|
||||
print(f'upload_file_id: {result.get("id")}')
|
||||
```
|
||||
|
||||
- Others
|
||||
|
||||
```python
|
||||
from dify_client import ChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize Client
|
||||
client = ChatClient(api_key)
|
||||
|
||||
# Get App parameters
|
||||
parameters = client.get_application_parameters(user="user_id")
|
||||
parameters.raise_for_status()
|
||||
|
||||
print('[parameters]')
|
||||
print(parameters.json())
|
||||
|
||||
# Get Conversation List (only for chat)
|
||||
conversations = client.get_conversations(user="user_id")
|
||||
conversations.raise_for_status()
|
||||
|
||||
print('[conversations]')
|
||||
print(conversations.json())
|
||||
|
||||
# Get Message List (only for chat)
|
||||
messages = client.get_conversation_messages(user="user_id", conversation_id="conversation_id")
|
||||
messages.raise_for_status()
|
||||
|
||||
print('[messages]')
|
||||
print(messages.json())
|
||||
|
||||
# Rename Conversation (only for chat)
|
||||
rename_conversation_response = client.rename_conversation(conversation_id="conversation_id",
|
||||
name="new_name", user="user_id")
|
||||
rename_conversation_response.raise_for_status()
|
||||
|
||||
print('[rename result]')
|
||||
print(rename_conversation_response.json())
|
||||
```
|
||||
|
||||
- Using the Workflow Client
|
||||
|
||||
```python
|
||||
import json
|
||||
import requests
|
||||
from dify_client import WorkflowClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Initialize Workflow Client
|
||||
client = WorkflowClient(api_key)
|
||||
|
||||
# Prepare parameters for Workflow Client
|
||||
user_id = "your_user_id"
|
||||
context = "previous user interaction / metadata"
|
||||
user_prompt = "What is the capital of France?"
|
||||
|
||||
inputs = {
|
||||
"context": context,
|
||||
"user_prompt": user_prompt,
|
||||
# Add other input fields expected by your workflow (e.g., additional context, task parameters)
|
||||
|
||||
}
|
||||
|
||||
# Set response mode (default: streaming)
|
||||
response_mode = "blocking"
|
||||
|
||||
# Run the workflow
|
||||
response = client.run(inputs=inputs, response_mode=response_mode, user=user_id)
|
||||
response.raise_for_status()
|
||||
|
||||
# Parse result
|
||||
result = json.loads(response.text)
|
||||
|
||||
answer = result.get("data").get("outputs")
|
||||
|
||||
print(answer["answer"])
|
||||
|
||||
```
|
||||
|
||||
- Dataset Management
|
||||
|
||||
```python
|
||||
from dify_client import KnowledgeBaseClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
dataset_id = "your_dataset_id"
|
||||
|
||||
# Use context manager to ensure proper resource cleanup
|
||||
with KnowledgeBaseClient(api_key, dataset_id) as kb_client:
|
||||
# Get dataset information
|
||||
dataset_info = kb_client.get_dataset()
|
||||
dataset_info.raise_for_status()
|
||||
print(dataset_info.json())
|
||||
|
||||
# Update dataset configuration
|
||||
update_response = kb_client.update_dataset(
|
||||
name="Updated Dataset Name",
|
||||
description="Updated description",
|
||||
indexing_technique="high_quality"
|
||||
)
|
||||
update_response.raise_for_status()
|
||||
print(update_response.json())
|
||||
|
||||
# Batch update document status
|
||||
batch_response = kb_client.batch_update_document_status(
|
||||
action="enable",
|
||||
document_ids=["doc_id_1", "doc_id_2", "doc_id_3"]
|
||||
)
|
||||
batch_response.raise_for_status()
|
||||
print(batch_response.json())
|
||||
```
|
||||
|
||||
- Conversation Variables Management
|
||||
|
||||
```python
|
||||
from dify_client import ChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
# Use context manager to ensure proper resource cleanup
|
||||
with ChatClient(api_key) as chat_client:
|
||||
# Get all conversation variables
|
||||
variables = chat_client.get_conversation_variables(
|
||||
conversation_id="conversation_id",
|
||||
user="user_id"
|
||||
)
|
||||
variables.raise_for_status()
|
||||
print(variables.json())
|
||||
|
||||
# Update a specific conversation variable
|
||||
update_var = chat_client.update_conversation_variable(
|
||||
conversation_id="conversation_id",
|
||||
variable_id="variable_id",
|
||||
value="new_value",
|
||||
user="user_id"
|
||||
)
|
||||
update_var.raise_for_status()
|
||||
print(update_var.json())
|
||||
```
|
||||
|
||||
### Asynchronous Usage
|
||||
|
||||
The SDK provides full async/await support for all API operations using `httpx.AsyncClient`. All async clients mirror their synchronous counterparts but require `await` for method calls.
|
||||
|
||||
- async chat with `blocking` response_mode
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
from dify_client import AsyncChatClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
async def main():
|
||||
# Use async context manager for proper resource cleanup
|
||||
async with AsyncChatClient(api_key) as client:
|
||||
response = await client.create_chat_message(
|
||||
inputs={},
|
||||
query="Hello, how are you?",
|
||||
user="user_id",
|
||||
response_mode="blocking"
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print(result.get('answer'))
|
||||
|
||||
# Run the async function
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
- async completion with `streaming` response_mode
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
import json
|
||||
from dify_client import AsyncCompletionClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
async def main():
|
||||
async with AsyncCompletionClient(api_key) as client:
|
||||
response = await client.create_completion_message(
|
||||
inputs={"query": "What's the weather?"},
|
||||
response_mode="streaming",
|
||||
user="user_id"
|
||||
)
|
||||
response.raise_for_status()
|
||||
|
||||
# Stream the response
|
||||
async for line in response.aiter_lines():
|
||||
if line.startswith('data:'):
|
||||
data = line[5:].strip()
|
||||
if data:
|
||||
chunk = json.loads(data)
|
||||
print(chunk.get('answer', ''), end='', flush=True)
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
- async workflow execution
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
from dify_client import AsyncWorkflowClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
|
||||
async def main():
|
||||
async with AsyncWorkflowClient(api_key) as client:
|
||||
response = await client.run(
|
||||
inputs={"query": "What is machine learning?"},
|
||||
response_mode="blocking",
|
||||
user="user_id"
|
||||
)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
print(result.get("data").get("outputs"))
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
- async dataset management
|
||||
|
||||
```python
|
||||
import asyncio
|
||||
from dify_client import AsyncKnowledgeBaseClient
|
||||
|
||||
api_key = "your_api_key"
|
||||
dataset_id = "your_dataset_id"
|
||||
|
||||
async def main():
|
||||
async with AsyncKnowledgeBaseClient(api_key, dataset_id) as kb_client:
|
||||
# Get dataset information
|
||||
dataset_info = await kb_client.get_dataset()
|
||||
dataset_info.raise_for_status()
|
||||
print(dataset_info.json())
|
||||
|
||||
# List documents
|
||||
docs = await kb_client.list_documents(page=1, page_size=10)
|
||||
docs.raise_for_status()
|
||||
print(docs.json())
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
|
||||
**Benefits of Async Usage:**
|
||||
|
||||
- **Better Performance**: Handle multiple concurrent API requests efficiently
|
||||
- **Non-blocking I/O**: Don't block the event loop during network operations
|
||||
- **Scalability**: Ideal for applications handling many simultaneous requests
|
||||
- **Modern Python**: Leverages Python's native async/await syntax
|
||||
|
||||
**Available Async Clients:**
|
||||
|
||||
- `AsyncDifyClient` - Base async client
|
||||
- `AsyncChatClient` - Async chat operations
|
||||
- `AsyncCompletionClient` - Async completion operations
|
||||
- `AsyncWorkflowClient` - Async workflow operations
|
||||
- `AsyncKnowledgeBaseClient` - Async dataset/knowledge base operations
|
||||
- `AsyncWorkspaceClient` - Async workspace operations
|
||||
|
||||
```
|
||||
```
|
||||
9
dify/sdks/python-client/build.sh
Normal file
9
dify/sdks/python-client/build.sh
Normal file
@@ -0,0 +1,9 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
rm -rf build dist *.egg-info
|
||||
|
||||
pip install setuptools wheel twine
|
||||
python setup.py sdist bdist_wheel
|
||||
twine upload dist/*
|
||||
34
dify/sdks/python-client/dify_client/__init__.py
Normal file
34
dify/sdks/python-client/dify_client/__init__.py
Normal file
@@ -0,0 +1,34 @@
|
||||
from dify_client.client import (
|
||||
ChatClient,
|
||||
CompletionClient,
|
||||
DifyClient,
|
||||
KnowledgeBaseClient,
|
||||
WorkflowClient,
|
||||
WorkspaceClient,
|
||||
)
|
||||
|
||||
from dify_client.async_client import (
|
||||
AsyncChatClient,
|
||||
AsyncCompletionClient,
|
||||
AsyncDifyClient,
|
||||
AsyncKnowledgeBaseClient,
|
||||
AsyncWorkflowClient,
|
||||
AsyncWorkspaceClient,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
# Synchronous clients
|
||||
"ChatClient",
|
||||
"CompletionClient",
|
||||
"DifyClient",
|
||||
"KnowledgeBaseClient",
|
||||
"WorkflowClient",
|
||||
"WorkspaceClient",
|
||||
# Asynchronous clients
|
||||
"AsyncChatClient",
|
||||
"AsyncCompletionClient",
|
||||
"AsyncDifyClient",
|
||||
"AsyncKnowledgeBaseClient",
|
||||
"AsyncWorkflowClient",
|
||||
"AsyncWorkspaceClient",
|
||||
]
|
||||
2074
dify/sdks/python-client/dify_client/async_client.py
Normal file
2074
dify/sdks/python-client/dify_client/async_client.py
Normal file
File diff suppressed because it is too large
Load Diff
228
dify/sdks/python-client/dify_client/base_client.py
Normal file
228
dify/sdks/python-client/dify_client/base_client.py
Normal file
@@ -0,0 +1,228 @@
|
||||
"""Base client with common functionality for both sync and async clients."""
|
||||
|
||||
import json
|
||||
import time
|
||||
import logging
|
||||
from typing import Dict, Callable, Optional
|
||||
|
||||
try:
|
||||
# Python 3.10+
|
||||
from typing import ParamSpec
|
||||
except ImportError:
|
||||
# Python < 3.10
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
from urllib.parse import urljoin
|
||||
|
||||
import httpx
|
||||
|
||||
P = ParamSpec("P")
|
||||
|
||||
from .exceptions import (
|
||||
DifyClientError,
|
||||
APIError,
|
||||
AuthenticationError,
|
||||
RateLimitError,
|
||||
ValidationError,
|
||||
NetworkError,
|
||||
TimeoutError,
|
||||
)
|
||||
|
||||
|
||||
class BaseClientMixin:
|
||||
"""Mixin class providing common functionality for Dify clients."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_key: str,
|
||||
base_url: str = "https://api.dify.ai/v1",
|
||||
timeout: float = 60.0,
|
||||
max_retries: int = 3,
|
||||
retry_delay: float = 1.0,
|
||||
enable_logging: bool = False,
|
||||
):
|
||||
"""Initialize the base client.
|
||||
|
||||
Args:
|
||||
api_key: Your Dify API key
|
||||
base_url: Base URL for the Dify API
|
||||
timeout: Request timeout in seconds
|
||||
max_retries: Maximum number of retry attempts
|
||||
retry_delay: Delay between retries in seconds
|
||||
enable_logging: Enable detailed logging
|
||||
"""
|
||||
if not api_key:
|
||||
raise ValidationError("API key is required")
|
||||
|
||||
self.api_key = api_key
|
||||
self.base_url = base_url.rstrip("/")
|
||||
self.timeout = timeout
|
||||
self.max_retries = max_retries
|
||||
self.retry_delay = retry_delay
|
||||
self.enable_logging = enable_logging
|
||||
|
||||
# Setup logging
|
||||
self.logger = logging.getLogger(f"dify_client.{self.__class__.__name__.lower()}")
|
||||
if enable_logging and not self.logger.handlers:
|
||||
# Create console handler with formatter
|
||||
handler = logging.StreamHandler()
|
||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
handler.setFormatter(formatter)
|
||||
self.logger.addHandler(handler)
|
||||
self.logger.setLevel(logging.INFO)
|
||||
self.enable_logging = True
|
||||
else:
|
||||
self.enable_logging = enable_logging
|
||||
|
||||
def _get_headers(self, content_type: str = "application/json") -> Dict[str, str]:
|
||||
"""Get common request headers."""
|
||||
return {
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": content_type,
|
||||
"User-Agent": "dify-client-python/0.1.12",
|
||||
}
|
||||
|
||||
def _build_url(self, endpoint: str) -> str:
|
||||
"""Build full URL from endpoint."""
|
||||
return urljoin(self.base_url + "/", endpoint.lstrip("/"))
|
||||
|
||||
def _handle_response(self, response: httpx.Response) -> httpx.Response:
|
||||
"""Handle HTTP response and raise appropriate exceptions."""
|
||||
try:
|
||||
if response.status_code == 401:
|
||||
raise AuthenticationError(
|
||||
"Authentication failed. Check your API key.",
|
||||
status_code=response.status_code,
|
||||
response=response.json() if response.content else None,
|
||||
)
|
||||
elif response.status_code == 429:
|
||||
retry_after = response.headers.get("Retry-After")
|
||||
raise RateLimitError(
|
||||
"Rate limit exceeded. Please try again later.",
|
||||
retry_after=int(retry_after) if retry_after else None,
|
||||
)
|
||||
elif response.status_code >= 400:
|
||||
try:
|
||||
error_data = response.json()
|
||||
message = error_data.get("message", f"HTTP {response.status_code}")
|
||||
except:
|
||||
message = f"HTTP {response.status_code}: {response.text}"
|
||||
|
||||
raise APIError(
|
||||
message,
|
||||
status_code=response.status_code,
|
||||
response=response.json() if response.content else None,
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
except json.JSONDecodeError:
|
||||
raise APIError(
|
||||
f"Invalid JSON response: {response.text}",
|
||||
status_code=response.status_code,
|
||||
)
|
||||
|
||||
def _retry_request(
|
||||
self,
|
||||
request_func: Callable[P, httpx.Response],
|
||||
request_context: str | None = None,
|
||||
*args: P.args,
|
||||
**kwargs: P.kwargs,
|
||||
) -> httpx.Response:
|
||||
"""Retry a request with exponential backoff.
|
||||
|
||||
Args:
|
||||
request_func: Function that performs the HTTP request
|
||||
request_context: Context description for logging (e.g., "GET /v1/messages")
|
||||
*args: Positional arguments to pass to request_func
|
||||
**kwargs: Keyword arguments to pass to request_func
|
||||
|
||||
Returns:
|
||||
httpx.Response: Successful response
|
||||
|
||||
Raises:
|
||||
NetworkError: On network failures after retries
|
||||
TimeoutError: On timeout failures after retries
|
||||
APIError: On API errors (4xx/5xx responses)
|
||||
DifyClientError: On unexpected failures
|
||||
"""
|
||||
last_exception = None
|
||||
|
||||
for attempt in range(self.max_retries + 1):
|
||||
try:
|
||||
response = request_func(*args, **kwargs)
|
||||
return response # Let caller handle response processing
|
||||
|
||||
except (httpx.NetworkError, httpx.TimeoutException) as e:
|
||||
last_exception = e
|
||||
context_msg = f" {request_context}" if request_context else ""
|
||||
|
||||
if attempt < self.max_retries:
|
||||
delay = self.retry_delay * (2**attempt) # Exponential backoff
|
||||
self.logger.warning(
|
||||
f"Request failed{context_msg} (attempt {attempt + 1}/{self.max_retries + 1}): {e}. "
|
||||
f"Retrying in {delay:.2f} seconds..."
|
||||
)
|
||||
time.sleep(delay)
|
||||
else:
|
||||
self.logger.error(f"Request failed{context_msg} after {self.max_retries + 1} attempts: {e}")
|
||||
# Convert to custom exceptions
|
||||
if isinstance(e, httpx.TimeoutException):
|
||||
from .exceptions import TimeoutError
|
||||
|
||||
raise TimeoutError(f"Request timed out after {self.max_retries} retries{context_msg}") from e
|
||||
else:
|
||||
from .exceptions import NetworkError
|
||||
|
||||
raise NetworkError(
|
||||
f"Network error after {self.max_retries} retries{context_msg}: {str(e)}"
|
||||
) from e
|
||||
|
||||
if last_exception:
|
||||
raise last_exception
|
||||
raise DifyClientError("Request failed after retries")
|
||||
|
||||
def _validate_params(self, **params) -> None:
|
||||
"""Validate request parameters."""
|
||||
for key, value in params.items():
|
||||
if value is None:
|
||||
continue
|
||||
|
||||
# String validations
|
||||
if isinstance(value, str):
|
||||
if not value.strip():
|
||||
raise ValidationError(f"Parameter '{key}' cannot be empty or whitespace only")
|
||||
if len(value) > 10000:
|
||||
raise ValidationError(f"Parameter '{key}' exceeds maximum length of 10000 characters")
|
||||
|
||||
# List validations
|
||||
elif isinstance(value, list):
|
||||
if len(value) > 1000:
|
||||
raise ValidationError(f"Parameter '{key}' exceeds maximum size of 1000 items")
|
||||
|
||||
# Dictionary validations
|
||||
elif isinstance(value, dict):
|
||||
if len(value) > 100:
|
||||
raise ValidationError(f"Parameter '{key}' exceeds maximum size of 100 items")
|
||||
|
||||
# Type-specific validations
|
||||
if key == "user" and not isinstance(value, str):
|
||||
raise ValidationError(f"Parameter '{key}' must be a string")
|
||||
elif key in ["page", "limit", "page_size"] and not isinstance(value, int):
|
||||
raise ValidationError(f"Parameter '{key}' must be an integer")
|
||||
elif key == "files" and not isinstance(value, (list, dict)):
|
||||
raise ValidationError(f"Parameter '{key}' must be a list or dict")
|
||||
elif key == "rating" and value not in ["like", "dislike"]:
|
||||
raise ValidationError(f"Parameter '{key}' must be 'like' or 'dislike'")
|
||||
|
||||
def _log_request(self, method: str, url: str, **kwargs) -> None:
|
||||
"""Log request details."""
|
||||
self.logger.info(f"Making {method} request to {url}")
|
||||
if kwargs.get("json"):
|
||||
self.logger.debug(f"Request body: {kwargs['json']}")
|
||||
if kwargs.get("params"):
|
||||
self.logger.debug(f"Query params: {kwargs['params']}")
|
||||
|
||||
def _log_response(self, response: httpx.Response) -> None:
|
||||
"""Log response details."""
|
||||
self.logger.info(f"Received response: {response.status_code} ({len(response.content)} bytes)")
|
||||
1267
dify/sdks/python-client/dify_client/client.py
Normal file
1267
dify/sdks/python-client/dify_client/client.py
Normal file
File diff suppressed because it is too large
Load Diff
71
dify/sdks/python-client/dify_client/exceptions.py
Normal file
71
dify/sdks/python-client/dify_client/exceptions.py
Normal file
@@ -0,0 +1,71 @@
|
||||
"""Custom exceptions for the Dify client."""
|
||||
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
|
||||
class DifyClientError(Exception):
|
||||
"""Base exception for all Dify client errors."""
|
||||
|
||||
def __init__(self, message: str, status_code: int | None = None, response: Dict[str, Any] | None = None):
|
||||
super().__init__(message)
|
||||
self.message = message
|
||||
self.status_code = status_code
|
||||
self.response = response
|
||||
|
||||
|
||||
class APIError(DifyClientError):
|
||||
"""Raised when the API returns an error response."""
|
||||
|
||||
def __init__(self, message: str, status_code: int, response: Dict[str, Any] | None = None):
|
||||
super().__init__(message, status_code, response)
|
||||
self.status_code = status_code
|
||||
|
||||
|
||||
class AuthenticationError(DifyClientError):
|
||||
"""Raised when authentication fails."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class RateLimitError(DifyClientError):
|
||||
"""Raised when rate limit is exceeded."""
|
||||
|
||||
def __init__(self, message: str = "Rate limit exceeded", retry_after: int | None = None):
|
||||
super().__init__(message)
|
||||
self.retry_after = retry_after
|
||||
|
||||
|
||||
class ValidationError(DifyClientError):
|
||||
"""Raised when request validation fails."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class NetworkError(DifyClientError):
|
||||
"""Raised when network-related errors occur."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class TimeoutError(DifyClientError):
|
||||
"""Raised when request times out."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class FileUploadError(DifyClientError):
|
||||
"""Raised when file upload fails."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class DatasetError(DifyClientError):
|
||||
"""Raised when dataset operations fail."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
class WorkflowError(DifyClientError):
|
||||
"""Raised when workflow operations fail."""
|
||||
|
||||
pass
|
||||
396
dify/sdks/python-client/dify_client/models.py
Normal file
396
dify/sdks/python-client/dify_client/models.py
Normal file
@@ -0,0 +1,396 @@
|
||||
"""Response models for the Dify client with proper type hints."""
|
||||
|
||||
from typing import Optional, List, Dict, Any, Literal, Union
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
@dataclass
|
||||
class BaseResponse:
|
||||
"""Base response model."""
|
||||
|
||||
success: bool = True
|
||||
message: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ErrorResponse(BaseResponse):
|
||||
"""Error response model."""
|
||||
|
||||
error_code: str | None = None
|
||||
details: Dict[str, Any] | None = None
|
||||
success: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileInfo:
|
||||
"""File information model."""
|
||||
|
||||
id: str
|
||||
name: str
|
||||
size: int
|
||||
mime_type: str
|
||||
url: str | None = None
|
||||
created_at: datetime | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class MessageResponse(BaseResponse):
|
||||
"""Message response model."""
|
||||
|
||||
id: str = ""
|
||||
answer: str = ""
|
||||
conversation_id: str | None = None
|
||||
created_at: int | None = None
|
||||
metadata: Dict[str, Any] | None = None
|
||||
files: List[Dict[str, Any]] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConversationResponse(BaseResponse):
|
||||
"""Conversation response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
inputs: Dict[str, Any] | None = None
|
||||
status: str | None = None
|
||||
created_at: int | None = None
|
||||
updated_at: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DatasetResponse(BaseResponse):
|
||||
"""Dataset response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
description: str | None = None
|
||||
permission: str | None = None
|
||||
indexing_technique: str | None = None
|
||||
embedding_model: str | None = None
|
||||
embedding_model_provider: str | None = None
|
||||
retrieval_model: Dict[str, Any] | None = None
|
||||
document_count: int | None = None
|
||||
word_count: int | None = None
|
||||
app_count: int | None = None
|
||||
created_at: int | None = None
|
||||
updated_at: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DocumentResponse(BaseResponse):
|
||||
"""Document response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
data_source_type: str | None = None
|
||||
data_source_info: Dict[str, Any] | None = None
|
||||
dataset_process_rule_id: str | None = None
|
||||
batch: str | None = None
|
||||
position: int | None = None
|
||||
enabled: bool | None = None
|
||||
disabled_at: float | None = None
|
||||
disabled_by: str | None = None
|
||||
archived: bool | None = None
|
||||
archived_reason: str | None = None
|
||||
archived_at: float | None = None
|
||||
archived_by: str | None = None
|
||||
word_count: int | None = None
|
||||
hit_count: int | None = None
|
||||
doc_form: str | None = None
|
||||
doc_metadata: Dict[str, Any] | None = None
|
||||
created_at: float | None = None
|
||||
updated_at: float | None = None
|
||||
indexing_status: str | None = None
|
||||
completed_at: float | None = None
|
||||
paused_at: float | None = None
|
||||
error: str | None = None
|
||||
stopped_at: float | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DocumentSegmentResponse(BaseResponse):
|
||||
"""Document segment response model."""
|
||||
|
||||
id: str = ""
|
||||
position: int | None = None
|
||||
document_id: str | None = None
|
||||
content: str | None = None
|
||||
answer: str | None = None
|
||||
word_count: int | None = None
|
||||
tokens: int | None = None
|
||||
keywords: List[str] | None = None
|
||||
index_node_id: str | None = None
|
||||
index_node_hash: str | None = None
|
||||
hit_count: int | None = None
|
||||
enabled: bool | None = None
|
||||
disabled_at: float | None = None
|
||||
disabled_by: str | None = None
|
||||
status: str | None = None
|
||||
created_by: str | None = None
|
||||
created_at: float | None = None
|
||||
indexing_at: float | None = None
|
||||
completed_at: float | None = None
|
||||
error: str | None = None
|
||||
stopped_at: float | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class WorkflowRunResponse(BaseResponse):
|
||||
"""Workflow run response model."""
|
||||
|
||||
id: str = ""
|
||||
workflow_id: str | None = None
|
||||
status: Literal["running", "succeeded", "failed", "stopped"] | None = None
|
||||
inputs: Dict[str, Any] | None = None
|
||||
outputs: Dict[str, Any] | None = None
|
||||
error: str | None = None
|
||||
elapsed_time: float | None = None
|
||||
total_tokens: int | None = None
|
||||
total_steps: int | None = None
|
||||
created_at: float | None = None
|
||||
finished_at: float | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ApplicationParametersResponse(BaseResponse):
|
||||
"""Application parameters response model."""
|
||||
|
||||
opening_statement: str | None = None
|
||||
suggested_questions: List[str] | None = None
|
||||
speech_to_text: Dict[str, Any] | None = None
|
||||
text_to_speech: Dict[str, Any] | None = None
|
||||
retriever_resource: Dict[str, Any] | None = None
|
||||
sensitive_word_avoidance: Dict[str, Any] | None = None
|
||||
file_upload: Dict[str, Any] | None = None
|
||||
system_parameters: Dict[str, Any] | None = None
|
||||
user_input_form: List[Dict[str, Any]] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class AnnotationResponse(BaseResponse):
|
||||
"""Annotation response model."""
|
||||
|
||||
id: str = ""
|
||||
question: str = ""
|
||||
answer: str = ""
|
||||
content: str | None = None
|
||||
created_at: float | None = None
|
||||
updated_at: float | None = None
|
||||
created_by: str | None = None
|
||||
updated_by: str | None = None
|
||||
hit_count: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class PaginatedResponse(BaseResponse):
|
||||
"""Paginated response model."""
|
||||
|
||||
data: List[Any] = field(default_factory=list)
|
||||
has_more: bool = False
|
||||
limit: int = 0
|
||||
total: int = 0
|
||||
page: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ConversationVariableResponse(BaseResponse):
|
||||
"""Conversation variable response model."""
|
||||
|
||||
conversation_id: str = ""
|
||||
variables: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileUploadResponse(BaseResponse):
|
||||
"""File upload response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
size: int = 0
|
||||
mime_type: str = ""
|
||||
url: str | None = None
|
||||
created_at: float | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class AudioResponse(BaseResponse):
|
||||
"""Audio generation/response model."""
|
||||
|
||||
audio: str | None = None # Base64 encoded audio data or URL
|
||||
audio_url: str | None = None
|
||||
duration: float | None = None
|
||||
sample_rate: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class SuggestedQuestionsResponse(BaseResponse):
|
||||
"""Suggested questions response model."""
|
||||
|
||||
message_id: str = ""
|
||||
questions: List[str] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppInfoResponse(BaseResponse):
|
||||
"""App info response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
description: str | None = None
|
||||
icon: str | None = None
|
||||
icon_background: str | None = None
|
||||
mode: str | None = None
|
||||
tags: List[str] | None = None
|
||||
enable_site: bool | None = None
|
||||
enable_api: bool | None = None
|
||||
api_token: str | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class WorkspaceModelsResponse(BaseResponse):
|
||||
"""Workspace models response model."""
|
||||
|
||||
models: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class HitTestingResponse(BaseResponse):
|
||||
"""Hit testing response model."""
|
||||
|
||||
query: str = ""
|
||||
records: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class DatasetTagsResponse(BaseResponse):
|
||||
"""Dataset tags response model."""
|
||||
|
||||
tags: List[Dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class WorkflowLogsResponse(BaseResponse):
|
||||
"""Workflow logs response model."""
|
||||
|
||||
logs: List[Dict[str, Any]] = field(default_factory=list)
|
||||
total: int = 0
|
||||
page: int = 0
|
||||
limit: int = 0
|
||||
has_more: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class ModelProviderResponse(BaseResponse):
|
||||
"""Model provider response model."""
|
||||
|
||||
provider_name: str = ""
|
||||
provider_type: str = ""
|
||||
models: List[Dict[str, Any]] = field(default_factory=list)
|
||||
is_enabled: bool = False
|
||||
credentials: Dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class FileInfoResponse(BaseResponse):
|
||||
"""File info response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
size: int = 0
|
||||
mime_type: str = ""
|
||||
url: str | None = None
|
||||
created_at: int | None = None
|
||||
metadata: Dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class WorkflowDraftResponse(BaseResponse):
|
||||
"""Workflow draft response model."""
|
||||
|
||||
id: str = ""
|
||||
app_id: str = ""
|
||||
draft_data: Dict[str, Any] = field(default_factory=dict)
|
||||
version: int = 0
|
||||
created_at: int | None = None
|
||||
updated_at: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ApiTokenResponse(BaseResponse):
|
||||
"""API token response model."""
|
||||
|
||||
id: str = ""
|
||||
name: str = ""
|
||||
token: str = ""
|
||||
description: str | None = None
|
||||
created_at: int | None = None
|
||||
last_used_at: int | None = None
|
||||
is_active: bool = True
|
||||
|
||||
|
||||
@dataclass
|
||||
class JobStatusResponse(BaseResponse):
|
||||
"""Job status response model."""
|
||||
|
||||
job_id: str = ""
|
||||
job_status: str = ""
|
||||
error_msg: str | None = None
|
||||
progress: float | None = None
|
||||
created_at: int | None = None
|
||||
updated_at: int | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DatasetQueryResponse(BaseResponse):
|
||||
"""Dataset query response model."""
|
||||
|
||||
query: str = ""
|
||||
records: List[Dict[str, Any]] = field(default_factory=list)
|
||||
total: int = 0
|
||||
search_time: float | None = None
|
||||
retrieval_model: Dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class DatasetTemplateResponse(BaseResponse):
|
||||
"""Dataset template response model."""
|
||||
|
||||
template_name: str = ""
|
||||
display_name: str = ""
|
||||
description: str = ""
|
||||
category: str = ""
|
||||
icon: str | None = None
|
||||
config_schema: Dict[str, Any] = field(default_factory=dict)
|
||||
|
||||
|
||||
# Type aliases for common response types
|
||||
ResponseType = Union[
|
||||
BaseResponse,
|
||||
ErrorResponse,
|
||||
MessageResponse,
|
||||
ConversationResponse,
|
||||
DatasetResponse,
|
||||
DocumentResponse,
|
||||
DocumentSegmentResponse,
|
||||
WorkflowRunResponse,
|
||||
ApplicationParametersResponse,
|
||||
AnnotationResponse,
|
||||
PaginatedResponse,
|
||||
ConversationVariableResponse,
|
||||
FileUploadResponse,
|
||||
AudioResponse,
|
||||
SuggestedQuestionsResponse,
|
||||
AppInfoResponse,
|
||||
WorkspaceModelsResponse,
|
||||
HitTestingResponse,
|
||||
DatasetTagsResponse,
|
||||
WorkflowLogsResponse,
|
||||
ModelProviderResponse,
|
||||
FileInfoResponse,
|
||||
WorkflowDraftResponse,
|
||||
ApiTokenResponse,
|
||||
JobStatusResponse,
|
||||
DatasetQueryResponse,
|
||||
DatasetTemplateResponse,
|
||||
]
|
||||
264
dify/sdks/python-client/examples/advanced_usage.py
Normal file
264
dify/sdks/python-client/examples/advanced_usage.py
Normal file
@@ -0,0 +1,264 @@
|
||||
"""
|
||||
Advanced usage examples for the Dify Python SDK.
|
||||
|
||||
This example demonstrates:
|
||||
- Error handling and retries
|
||||
- Logging configuration
|
||||
- Context managers
|
||||
- Async usage
|
||||
- File uploads
|
||||
- Dataset management
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
from dify_client import (
|
||||
ChatClient,
|
||||
CompletionClient,
|
||||
AsyncChatClient,
|
||||
KnowledgeBaseClient,
|
||||
DifyClient,
|
||||
)
|
||||
from dify_client.exceptions import (
|
||||
APIError,
|
||||
RateLimitError,
|
||||
AuthenticationError,
|
||||
DifyClientError,
|
||||
)
|
||||
|
||||
|
||||
def setup_logging():
|
||||
"""Setup logging for the SDK."""
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||
|
||||
|
||||
def example_chat_with_error_handling():
|
||||
"""Example of chat with comprehensive error handling."""
|
||||
api_key = "your-api-key-here"
|
||||
|
||||
try:
|
||||
with ChatClient(api_key, enable_logging=True) as client:
|
||||
# Simple chat message
|
||||
response = client.create_chat_message(
|
||||
inputs={}, query="Hello, how are you?", user="user-123", response_mode="blocking"
|
||||
)
|
||||
|
||||
result = response.json()
|
||||
print(f"Response: {result.get('answer')}")
|
||||
|
||||
except AuthenticationError as e:
|
||||
print(f"Authentication failed: {e}")
|
||||
print("Please check your API key")
|
||||
|
||||
except RateLimitError as e:
|
||||
print(f"Rate limit exceeded: {e}")
|
||||
if e.retry_after:
|
||||
print(f"Retry after {e.retry_after} seconds")
|
||||
|
||||
except APIError as e:
|
||||
print(f"API error: {e.message}")
|
||||
print(f"Status code: {e.status_code}")
|
||||
|
||||
except DifyClientError as e:
|
||||
print(f"Dify client error: {e}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Unexpected error: {e}")
|
||||
|
||||
|
||||
def example_completion_with_files():
|
||||
"""Example of completion with file upload."""
|
||||
api_key = "your-api-key-here"
|
||||
|
||||
with CompletionClient(api_key) as client:
|
||||
# Upload an image file first
|
||||
file_path = "path/to/your/image.jpg"
|
||||
|
||||
try:
|
||||
with open(file_path, "rb") as f:
|
||||
files = {"file": (Path(file_path).name, f, "image/jpeg")}
|
||||
upload_response = client.file_upload("user-123", files)
|
||||
upload_response.raise_for_status()
|
||||
|
||||
file_id = upload_response.json().get("id")
|
||||
print(f"File uploaded with ID: {file_id}")
|
||||
|
||||
# Use the uploaded file in completion
|
||||
files_list = [{"type": "image", "transfer_method": "local_file", "upload_file_id": file_id}]
|
||||
|
||||
completion_response = client.create_completion_message(
|
||||
inputs={"query": "Describe this image"}, response_mode="blocking", user="user-123", files=files_list
|
||||
)
|
||||
|
||||
result = completion_response.json()
|
||||
print(f"Completion result: {result.get('answer')}")
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"File not found: {file_path}")
|
||||
except Exception as e:
|
||||
print(f"Error during file upload/completion: {e}")
|
||||
|
||||
|
||||
def example_dataset_management():
|
||||
"""Example of dataset management operations."""
|
||||
api_key = "your-api-key-here"
|
||||
|
||||
with KnowledgeBaseClient(api_key) as kb_client:
|
||||
try:
|
||||
# Create a new dataset
|
||||
create_response = kb_client.create_dataset(name="My Test Dataset")
|
||||
create_response.raise_for_status()
|
||||
|
||||
dataset_id = create_response.json().get("id")
|
||||
print(f"Created dataset with ID: {dataset_id}")
|
||||
|
||||
# Create a client with the dataset ID
|
||||
dataset_client = KnowledgeBaseClient(api_key, dataset_id=dataset_id)
|
||||
|
||||
# Add a document by text
|
||||
doc_response = dataset_client.create_document_by_text(
|
||||
name="Test Document", text="This is a test document for the knowledge base."
|
||||
)
|
||||
doc_response.raise_for_status()
|
||||
|
||||
document_id = doc_response.json().get("document", {}).get("id")
|
||||
print(f"Created document with ID: {document_id}")
|
||||
|
||||
# List documents
|
||||
list_response = dataset_client.list_documents()
|
||||
list_response.raise_for_status()
|
||||
|
||||
documents = list_response.json().get("data", [])
|
||||
print(f"Dataset contains {len(documents)} documents")
|
||||
|
||||
# Update dataset configuration
|
||||
update_response = dataset_client.update_dataset(
|
||||
name="Updated Dataset Name", description="Updated description", indexing_technique="high_quality"
|
||||
)
|
||||
update_response.raise_for_status()
|
||||
|
||||
print("Dataset updated successfully")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Dataset management error: {e}")
|
||||
|
||||
|
||||
async def example_async_chat():
|
||||
"""Example of async chat usage."""
|
||||
api_key = "your-api-key-here"
|
||||
|
||||
try:
|
||||
async with AsyncChatClient(api_key) as client:
|
||||
# Create chat message
|
||||
response = await client.create_chat_message(
|
||||
inputs={}, query="What's the weather like?", user="user-456", response_mode="blocking"
|
||||
)
|
||||
|
||||
result = response.json()
|
||||
print(f"Async response: {result.get('answer')}")
|
||||
|
||||
# Get conversations
|
||||
conversations = await client.get_conversations("user-456")
|
||||
conversations.raise_for_status()
|
||||
|
||||
conv_data = conversations.json()
|
||||
print(f"Found {len(conv_data.get('data', []))} conversations")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Async chat error: {e}")
|
||||
|
||||
|
||||
def example_streaming_response():
|
||||
"""Example of handling streaming responses."""
|
||||
api_key = "your-api-key-here"
|
||||
|
||||
with ChatClient(api_key) as client:
|
||||
try:
|
||||
response = client.create_chat_message(
|
||||
inputs={}, query="Tell me a story", user="user-789", response_mode="streaming"
|
||||
)
|
||||
|
||||
print("Streaming response:")
|
||||
for line in response.iter_lines(decode_unicode=True):
|
||||
if line.startswith("data:"):
|
||||
data = line[5:].strip()
|
||||
if data:
|
||||
import json
|
||||
|
||||
try:
|
||||
chunk = json.loads(data)
|
||||
answer = chunk.get("answer", "")
|
||||
if answer:
|
||||
print(answer, end="", flush=True)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
print() # New line after streaming
|
||||
|
||||
except Exception as e:
|
||||
print(f"Streaming error: {e}")
|
||||
|
||||
|
||||
def example_application_info():
|
||||
"""Example of getting application information."""
|
||||
api_key = "your-api-key-here"
|
||||
|
||||
with DifyClient(api_key) as client:
|
||||
try:
|
||||
# Get app info
|
||||
info_response = client.get_app_info()
|
||||
info_response.raise_for_status()
|
||||
|
||||
app_info = info_response.json()
|
||||
print(f"App name: {app_info.get('name')}")
|
||||
print(f"App mode: {app_info.get('mode')}")
|
||||
print(f"App tags: {app_info.get('tags', [])}")
|
||||
|
||||
# Get app parameters
|
||||
params_response = client.get_application_parameters("user-123")
|
||||
params_response.raise_for_status()
|
||||
|
||||
params = params_response.json()
|
||||
print(f"Opening statement: {params.get('opening_statement')}")
|
||||
print(f"Suggested questions: {params.get('suggested_questions', [])}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"App info error: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
"""Run all examples."""
|
||||
setup_logging()
|
||||
|
||||
print("=== Dify Python SDK Advanced Usage Examples ===\n")
|
||||
|
||||
print("1. Chat with Error Handling:")
|
||||
example_chat_with_error_handling()
|
||||
print()
|
||||
|
||||
print("2. Completion with Files:")
|
||||
example_completion_with_files()
|
||||
print()
|
||||
|
||||
print("3. Dataset Management:")
|
||||
example_dataset_management()
|
||||
print()
|
||||
|
||||
print("4. Async Chat:")
|
||||
asyncio.run(example_async_chat())
|
||||
print()
|
||||
|
||||
print("5. Streaming Response:")
|
||||
example_streaming_response()
|
||||
print()
|
||||
|
||||
print("6. Application Info:")
|
||||
example_application_info()
|
||||
print()
|
||||
|
||||
print("All examples completed!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
43
dify/sdks/python-client/pyproject.toml
Normal file
43
dify/sdks/python-client/pyproject.toml
Normal file
@@ -0,0 +1,43 @@
|
||||
[project]
|
||||
name = "dify-client"
|
||||
version = "0.1.12"
|
||||
description = "A package for interacting with the Dify Service-API"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.10"
|
||||
dependencies = [
|
||||
"httpx[http2]>=0.27.0",
|
||||
"aiofiles>=23.0.0",
|
||||
]
|
||||
authors = [
|
||||
{name = "Dify", email = "hello@dify.ai"}
|
||||
]
|
||||
license = {text = "MIT"}
|
||||
keywords = ["dify", "nlp", "ai", "language-processing"]
|
||||
classifiers = [
|
||||
"Programming Language :: Python :: 3",
|
||||
"License :: OSI Approved :: MIT License",
|
||||
"Operating System :: OS Independent",
|
||||
]
|
||||
|
||||
[project.urls]
|
||||
Homepage = "https://github.com/langgenius/dify"
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = [
|
||||
"pytest>=7.0.0",
|
||||
"pytest-asyncio>=0.21.0",
|
||||
]
|
||||
|
||||
[build-system]
|
||||
requires = ["hatchling"]
|
||||
build-backend = "hatchling.build"
|
||||
|
||||
[tool.hatch.build.targets.wheel]
|
||||
packages = ["dify_client"]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["tests"]
|
||||
python_files = ["test_*.py"]
|
||||
python_classes = ["Test*"]
|
||||
python_functions = ["test_*"]
|
||||
asyncio_mode = "auto"
|
||||
0
dify/sdks/python-client/tests/__init__.py
Normal file
0
dify/sdks/python-client/tests/__init__.py
Normal file
250
dify/sdks/python-client/tests/test_async_client.py
Normal file
250
dify/sdks/python-client/tests/test_async_client.py
Normal file
@@ -0,0 +1,250 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test suite for async client implementation in the Python SDK.
|
||||
|
||||
This test validates the async/await functionality using httpx.AsyncClient
|
||||
and ensures API parity with sync clients.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch, AsyncMock
|
||||
|
||||
from dify_client.async_client import (
|
||||
AsyncDifyClient,
|
||||
AsyncChatClient,
|
||||
AsyncCompletionClient,
|
||||
AsyncWorkflowClient,
|
||||
AsyncWorkspaceClient,
|
||||
AsyncKnowledgeBaseClient,
|
||||
)
|
||||
|
||||
|
||||
class TestAsyncAPIParity(unittest.TestCase):
|
||||
"""Test that async clients have API parity with sync clients."""
|
||||
|
||||
def test_dify_client_api_parity(self):
|
||||
"""Test AsyncDifyClient has same methods as DifyClient."""
|
||||
from dify_client import DifyClient
|
||||
|
||||
sync_methods = {name for name in dir(DifyClient) if not name.startswith("_")}
|
||||
async_methods = {name for name in dir(AsyncDifyClient) if not name.startswith("_")}
|
||||
|
||||
# aclose is async-specific, close is sync-specific
|
||||
sync_methods.discard("close")
|
||||
async_methods.discard("aclose")
|
||||
|
||||
# Verify parity
|
||||
self.assertEqual(sync_methods, async_methods, "API parity mismatch for DifyClient")
|
||||
|
||||
def test_chat_client_api_parity(self):
|
||||
"""Test AsyncChatClient has same methods as ChatClient."""
|
||||
from dify_client import ChatClient
|
||||
|
||||
sync_methods = {name for name in dir(ChatClient) if not name.startswith("_")}
|
||||
async_methods = {name for name in dir(AsyncChatClient) if not name.startswith("_")}
|
||||
|
||||
sync_methods.discard("close")
|
||||
async_methods.discard("aclose")
|
||||
|
||||
self.assertEqual(sync_methods, async_methods, "API parity mismatch for ChatClient")
|
||||
|
||||
def test_completion_client_api_parity(self):
|
||||
"""Test AsyncCompletionClient has same methods as CompletionClient."""
|
||||
from dify_client import CompletionClient
|
||||
|
||||
sync_methods = {name for name in dir(CompletionClient) if not name.startswith("_")}
|
||||
async_methods = {name for name in dir(AsyncCompletionClient) if not name.startswith("_")}
|
||||
|
||||
sync_methods.discard("close")
|
||||
async_methods.discard("aclose")
|
||||
|
||||
self.assertEqual(sync_methods, async_methods, "API parity mismatch for CompletionClient")
|
||||
|
||||
def test_workflow_client_api_parity(self):
|
||||
"""Test AsyncWorkflowClient has same methods as WorkflowClient."""
|
||||
from dify_client import WorkflowClient
|
||||
|
||||
sync_methods = {name for name in dir(WorkflowClient) if not name.startswith("_")}
|
||||
async_methods = {name for name in dir(AsyncWorkflowClient) if not name.startswith("_")}
|
||||
|
||||
sync_methods.discard("close")
|
||||
async_methods.discard("aclose")
|
||||
|
||||
self.assertEqual(sync_methods, async_methods, "API parity mismatch for WorkflowClient")
|
||||
|
||||
def test_workspace_client_api_parity(self):
|
||||
"""Test AsyncWorkspaceClient has same methods as WorkspaceClient."""
|
||||
from dify_client import WorkspaceClient
|
||||
|
||||
sync_methods = {name for name in dir(WorkspaceClient) if not name.startswith("_")}
|
||||
async_methods = {name for name in dir(AsyncWorkspaceClient) if not name.startswith("_")}
|
||||
|
||||
sync_methods.discard("close")
|
||||
async_methods.discard("aclose")
|
||||
|
||||
self.assertEqual(sync_methods, async_methods, "API parity mismatch for WorkspaceClient")
|
||||
|
||||
def test_knowledge_base_client_api_parity(self):
|
||||
"""Test AsyncKnowledgeBaseClient has same methods as KnowledgeBaseClient."""
|
||||
from dify_client import KnowledgeBaseClient
|
||||
|
||||
sync_methods = {name for name in dir(KnowledgeBaseClient) if not name.startswith("_")}
|
||||
async_methods = {name for name in dir(AsyncKnowledgeBaseClient) if not name.startswith("_")}
|
||||
|
||||
sync_methods.discard("close")
|
||||
async_methods.discard("aclose")
|
||||
|
||||
self.assertEqual(sync_methods, async_methods, "API parity mismatch for KnowledgeBaseClient")
|
||||
|
||||
|
||||
class TestAsyncClientMocked(unittest.IsolatedAsyncioTestCase):
|
||||
"""Test async client with mocked httpx.AsyncClient."""
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_client_initialization(self, mock_httpx_async_client):
|
||||
"""Test async client initializes with httpx.AsyncClient."""
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
client = AsyncDifyClient("test-key", "https://api.dify.ai/v1")
|
||||
|
||||
# Verify httpx.AsyncClient was called
|
||||
mock_httpx_async_client.assert_called_once()
|
||||
self.assertEqual(client.api_key, "test-key")
|
||||
|
||||
await client.aclose()
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_context_manager(self, mock_httpx_async_client):
|
||||
"""Test async context manager works."""
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncDifyClient("test-key") as client:
|
||||
self.assertEqual(client.api_key, "test-key")
|
||||
|
||||
# Verify aclose was called
|
||||
mock_client_instance.aclose.assert_called_once()
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_send_request(self, mock_httpx_async_client):
|
||||
"""Test async _send_request method."""
|
||||
mock_response = AsyncMock()
|
||||
mock_response.json = AsyncMock(return_value={"result": "success"})
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_instance.request = AsyncMock(return_value=mock_response)
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncDifyClient("test-key") as client:
|
||||
response = await client._send_request("GET", "/test")
|
||||
|
||||
# Verify request was called
|
||||
mock_client_instance.request.assert_called_once()
|
||||
call_args = mock_client_instance.request.call_args
|
||||
|
||||
# Verify parameters
|
||||
self.assertEqual(call_args[0][0], "GET")
|
||||
self.assertEqual(call_args[0][1], "/test")
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_chat_client(self, mock_httpx_async_client):
|
||||
"""Test AsyncChatClient functionality."""
|
||||
mock_response = AsyncMock()
|
||||
mock_response.text = '{"answer": "Hello!"}'
|
||||
mock_response.json = AsyncMock(return_value={"answer": "Hello!"})
|
||||
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_instance.request = AsyncMock(return_value=mock_response)
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncChatClient("test-key") as client:
|
||||
response = await client.create_chat_message({}, "Hi", "user123")
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_completion_client(self, mock_httpx_async_client):
|
||||
"""Test AsyncCompletionClient functionality."""
|
||||
mock_response = AsyncMock()
|
||||
mock_response.text = '{"answer": "Response"}'
|
||||
mock_response.json = AsyncMock(return_value={"answer": "Response"})
|
||||
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_instance.request = AsyncMock(return_value=mock_response)
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncCompletionClient("test-key") as client:
|
||||
response = await client.create_completion_message({"query": "test"}, "blocking", "user123")
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_workflow_client(self, mock_httpx_async_client):
|
||||
"""Test AsyncWorkflowClient functionality."""
|
||||
mock_response = AsyncMock()
|
||||
mock_response.json = AsyncMock(return_value={"result": "success"})
|
||||
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_instance.request = AsyncMock(return_value=mock_response)
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncWorkflowClient("test-key") as client:
|
||||
response = await client.run({"input": "test"}, "blocking", "user123")
|
||||
data = await response.json()
|
||||
self.assertEqual(data["result"], "success")
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_workspace_client(self, mock_httpx_async_client):
|
||||
"""Test AsyncWorkspaceClient functionality."""
|
||||
mock_response = AsyncMock()
|
||||
mock_response.json = AsyncMock(return_value={"data": []})
|
||||
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_instance.request = AsyncMock(return_value=mock_response)
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncWorkspaceClient("test-key") as client:
|
||||
response = await client.get_available_models("llm")
|
||||
data = await response.json()
|
||||
self.assertIn("data", data)
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_async_knowledge_base_client(self, mock_httpx_async_client):
|
||||
"""Test AsyncKnowledgeBaseClient functionality."""
|
||||
mock_response = AsyncMock()
|
||||
mock_response.json = AsyncMock(return_value={"data": [], "total": 0})
|
||||
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_client_instance.request = AsyncMock(return_value=mock_response)
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
async with AsyncKnowledgeBaseClient("test-key") as client:
|
||||
response = await client.list_datasets()
|
||||
data = await response.json()
|
||||
self.assertIn("data", data)
|
||||
|
||||
@patch("dify_client.async_client.httpx.AsyncClient")
|
||||
async def test_all_async_client_classes(self, mock_httpx_async_client):
|
||||
"""Test all async client classes work with httpx.AsyncClient."""
|
||||
mock_client_instance = AsyncMock()
|
||||
mock_httpx_async_client.return_value = mock_client_instance
|
||||
|
||||
clients = [
|
||||
AsyncDifyClient("key"),
|
||||
AsyncChatClient("key"),
|
||||
AsyncCompletionClient("key"),
|
||||
AsyncWorkflowClient("key"),
|
||||
AsyncWorkspaceClient("key"),
|
||||
AsyncKnowledgeBaseClient("key"),
|
||||
]
|
||||
|
||||
# Verify httpx.AsyncClient was called for each
|
||||
self.assertEqual(mock_httpx_async_client.call_count, 6)
|
||||
|
||||
# Clean up
|
||||
for client in clients:
|
||||
await client.aclose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
489
dify/sdks/python-client/tests/test_client.py
Normal file
489
dify/sdks/python-client/tests/test_client.py
Normal file
@@ -0,0 +1,489 @@
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch, mock_open
|
||||
|
||||
from dify_client.client import (
|
||||
ChatClient,
|
||||
CompletionClient,
|
||||
DifyClient,
|
||||
KnowledgeBaseClient,
|
||||
)
|
||||
|
||||
API_KEY = os.environ.get("API_KEY")
|
||||
APP_ID = os.environ.get("APP_ID")
|
||||
API_BASE_URL = os.environ.get("API_BASE_URL", "https://api.dify.ai/v1")
|
||||
FILE_PATH_BASE = os.path.dirname(__file__)
|
||||
|
||||
|
||||
class TestKnowledgeBaseClient(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.api_key = "test-api-key"
|
||||
self.base_url = "https://api.dify.ai/v1"
|
||||
self.knowledge_base_client = KnowledgeBaseClient(self.api_key, base_url=self.base_url)
|
||||
self.README_FILE_PATH = os.path.abspath(os.path.join(FILE_PATH_BASE, "../README.md"))
|
||||
self.dataset_id = "test-dataset-id"
|
||||
self.document_id = "test-document-id"
|
||||
self.segment_id = "test-segment-id"
|
||||
self.batch_id = "test-batch-id"
|
||||
|
||||
def _get_dataset_kb_client(self):
|
||||
return KnowledgeBaseClient(self.api_key, base_url=self.base_url, dataset_id=self.dataset_id)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_001_create_dataset(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"id": self.dataset_id, "name": "test_dataset"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Re-create client with mocked httpx
|
||||
self.knowledge_base_client = KnowledgeBaseClient(self.api_key, base_url=self.base_url)
|
||||
|
||||
response = self.knowledge_base_client.create_dataset(name="test_dataset")
|
||||
data = response.json()
|
||||
self.assertIn("id", data)
|
||||
self.assertEqual("test_dataset", data["name"])
|
||||
|
||||
# the following tests require to be executed in order because they use
|
||||
# the dataset/document/segment ids from the previous test
|
||||
self._test_002_list_datasets()
|
||||
self._test_003_create_document_by_text()
|
||||
self._test_004_update_document_by_text()
|
||||
self._test_006_update_document_by_file()
|
||||
self._test_007_list_documents()
|
||||
self._test_008_delete_document()
|
||||
self._test_009_create_document_by_file()
|
||||
self._test_010_add_segments()
|
||||
self._test_011_query_segments()
|
||||
self._test_012_update_document_segment()
|
||||
self._test_013_delete_document_segment()
|
||||
self._test_014_delete_dataset()
|
||||
|
||||
def _test_002_list_datasets(self):
|
||||
# Mock the response - using the already mocked client from test_001_create_dataset
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": [], "total": 0}
|
||||
mock_response.status_code = 200
|
||||
self.knowledge_base_client._client.request.return_value = mock_response
|
||||
|
||||
response = self.knowledge_base_client.list_datasets()
|
||||
data = response.json()
|
||||
self.assertIn("data", data)
|
||||
self.assertIn("total", data)
|
||||
|
||||
def _test_003_create_document_by_text(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"document": {"id": self.document_id}, "batch": self.batch_id}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.create_document_by_text("test_document", "test_text")
|
||||
data = response.json()
|
||||
self.assertIn("document", data)
|
||||
|
||||
def _test_004_update_document_by_text(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"document": {"id": self.document_id}, "batch": self.batch_id}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.update_document_by_text(self.document_id, "test_document_updated", "test_text_updated")
|
||||
data = response.json()
|
||||
self.assertIn("document", data)
|
||||
self.assertIn("batch", data)
|
||||
|
||||
def _test_006_update_document_by_file(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"document": {"id": self.document_id}, "batch": self.batch_id}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.update_document_by_file(self.document_id, self.README_FILE_PATH)
|
||||
data = response.json()
|
||||
self.assertIn("document", data)
|
||||
self.assertIn("batch", data)
|
||||
|
||||
def _test_007_list_documents(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": []}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.list_documents()
|
||||
data = response.json()
|
||||
self.assertIn("data", data)
|
||||
|
||||
def _test_008_delete_document(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.delete_document(self.document_id)
|
||||
data = response.json()
|
||||
self.assertIn("result", data)
|
||||
self.assertEqual("success", data["result"])
|
||||
|
||||
def _test_009_create_document_by_file(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"document": {"id": self.document_id}, "batch": self.batch_id}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.create_document_by_file(self.README_FILE_PATH)
|
||||
data = response.json()
|
||||
self.assertIn("document", data)
|
||||
|
||||
def _test_010_add_segments(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": [{"id": self.segment_id, "content": "test text segment 1"}]}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.add_segments(self.document_id, [{"content": "test text segment 1"}])
|
||||
data = response.json()
|
||||
self.assertIn("data", data)
|
||||
self.assertGreater(len(data["data"]), 0)
|
||||
|
||||
def _test_011_query_segments(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": [{"id": self.segment_id, "content": "test text segment 1"}]}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.query_segments(self.document_id)
|
||||
data = response.json()
|
||||
self.assertIn("data", data)
|
||||
self.assertGreater(len(data["data"]), 0)
|
||||
|
||||
def _test_012_update_document_segment(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": {"id": self.segment_id, "content": "test text segment 1 updated"}}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.update_document_segment(
|
||||
self.document_id,
|
||||
self.segment_id,
|
||||
{"content": "test text segment 1 updated"},
|
||||
)
|
||||
data = response.json()
|
||||
self.assertIn("data", data)
|
||||
self.assertEqual("test text segment 1 updated", data["data"]["content"])
|
||||
|
||||
def _test_013_delete_document_segment(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.delete_document_segment(self.document_id, self.segment_id)
|
||||
data = response.json()
|
||||
self.assertIn("result", data)
|
||||
self.assertEqual("success", data["result"])
|
||||
|
||||
def _test_014_delete_dataset(self):
|
||||
client = self._get_dataset_kb_client()
|
||||
# Mock the response
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 204
|
||||
client._client.request.return_value = mock_response
|
||||
|
||||
response = client.delete_dataset()
|
||||
self.assertEqual(204, response.status_code)
|
||||
|
||||
|
||||
class TestChatClient(unittest.TestCase):
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def setUp(self, mock_httpx_client):
|
||||
self.api_key = "test-api-key"
|
||||
self.chat_client = ChatClient(self.api_key)
|
||||
|
||||
# Set up default mock response for the client
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "Hello! This is a test response."}'
|
||||
mock_response.json.return_value = {"answer": "Hello! This is a test response."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_chat_message(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "Hello! This is a test response."}'
|
||||
mock_response.json.return_value = {"answer": "Hello! This is a test response."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
chat_client = ChatClient(self.api_key)
|
||||
response = chat_client.create_chat_message({}, "Hello, World!", "test_user")
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_chat_message_with_vision_model_by_remote_url(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "I can see this is a test image description."}'
|
||||
mock_response.json.return_value = {"answer": "I can see this is a test image description."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
chat_client = ChatClient(self.api_key)
|
||||
files = [{"type": "image", "transfer_method": "remote_url", "url": "https://example.com/test-image.jpg"}]
|
||||
response = chat_client.create_chat_message({}, "Describe the picture.", "test_user", files=files)
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_chat_message_with_vision_model_by_local_file(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "I can see this is a test uploaded image."}'
|
||||
mock_response.json.return_value = {"answer": "I can see this is a test uploaded image."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
chat_client = ChatClient(self.api_key)
|
||||
files = [
|
||||
{
|
||||
"type": "image",
|
||||
"transfer_method": "local_file",
|
||||
"upload_file_id": "test-file-id",
|
||||
}
|
||||
]
|
||||
response = chat_client.create_chat_message({}, "Describe the picture.", "test_user", files=files)
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_get_conversation_messages(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "Here are the conversation messages."}'
|
||||
mock_response.json.return_value = {"answer": "Here are the conversation messages."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
chat_client = ChatClient(self.api_key)
|
||||
response = chat_client.get_conversation_messages("test_user", "test-conversation-id")
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_get_conversations(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"data": [{"id": "conv1", "name": "Test Conversation"}]}'
|
||||
mock_response.json.return_value = {"data": [{"id": "conv1", "name": "Test Conversation"}]}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
chat_client = ChatClient(self.api_key)
|
||||
response = chat_client.get_conversations("test_user")
|
||||
self.assertIn("data", response.text)
|
||||
|
||||
|
||||
class TestCompletionClient(unittest.TestCase):
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def setUp(self, mock_httpx_client):
|
||||
self.api_key = "test-api-key"
|
||||
self.completion_client = CompletionClient(self.api_key)
|
||||
|
||||
# Set up default mock response for the client
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "This is a test completion response."}'
|
||||
mock_response.json.return_value = {"answer": "This is a test completion response."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_completion_message(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "The weather today is sunny with a temperature of 75°F."}'
|
||||
mock_response.json.return_value = {"answer": "The weather today is sunny with a temperature of 75°F."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
completion_client = CompletionClient(self.api_key)
|
||||
response = completion_client.create_completion_message(
|
||||
{"query": "What's the weather like today?"}, "blocking", "test_user"
|
||||
)
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_completion_message_with_vision_model_by_remote_url(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "This is a test image description from completion API."}'
|
||||
mock_response.json.return_value = {"answer": "This is a test image description from completion API."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
completion_client = CompletionClient(self.api_key)
|
||||
files = [{"type": "image", "transfer_method": "remote_url", "url": "https://example.com/test-image.jpg"}]
|
||||
response = completion_client.create_completion_message(
|
||||
{"query": "Describe the picture."}, "blocking", "test_user", files
|
||||
)
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_completion_message_with_vision_model_by_local_file(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "This is a test uploaded image description from completion API."}'
|
||||
mock_response.json.return_value = {"answer": "This is a test uploaded image description from completion API."}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
completion_client = CompletionClient(self.api_key)
|
||||
files = [
|
||||
{
|
||||
"type": "image",
|
||||
"transfer_method": "local_file",
|
||||
"upload_file_id": "test-file-id",
|
||||
}
|
||||
]
|
||||
response = completion_client.create_completion_message(
|
||||
{"query": "Describe the picture."}, "blocking", "test_user", files
|
||||
)
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
|
||||
class TestDifyClient(unittest.TestCase):
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def setUp(self, mock_httpx_client):
|
||||
self.api_key = "test-api-key"
|
||||
self.dify_client = DifyClient(self.api_key)
|
||||
|
||||
# Set up default mock response for the client
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"result": "success"}'
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_message_feedback(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"success": true}'
|
||||
mock_response.json.return_value = {"success": True}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
dify_client = DifyClient(self.api_key)
|
||||
response = dify_client.message_feedback("test-message-id", "like", "test_user")
|
||||
self.assertIn("success", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_get_application_parameters(self, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"user_input_form": [{"field": "text", "label": "Input"}]}'
|
||||
mock_response.json.return_value = {"user_input_form": [{"field": "text", "label": "Input"}]}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
dify_client = DifyClient(self.api_key)
|
||||
response = dify_client.get_application_parameters("test_user")
|
||||
self.assertIn("user_input_form", response.text)
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
@patch("builtins.open", new_callable=mock_open, read_data=b"fake image data")
|
||||
def test_file_upload(self, mock_file_open, mock_httpx_client):
|
||||
# Mock the HTTP response
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"name": "panda.jpeg", "id": "test-file-id"}'
|
||||
mock_response.json.return_value = {"name": "panda.jpeg", "id": "test-file-id"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# Create client with mocked httpx
|
||||
dify_client = DifyClient(self.api_key)
|
||||
file_path = "/path/to/test/panda.jpeg"
|
||||
file_name = "panda.jpeg"
|
||||
mime_type = "image/jpeg"
|
||||
|
||||
with open(file_path, "rb") as file:
|
||||
files = {"file": (file_name, file, mime_type)}
|
||||
response = dify_client.file_upload("test_user", files)
|
||||
self.assertIn("name", response.text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
79
dify/sdks/python-client/tests/test_exceptions.py
Normal file
79
dify/sdks/python-client/tests/test_exceptions.py
Normal file
@@ -0,0 +1,79 @@
|
||||
"""Tests for custom exceptions."""
|
||||
|
||||
import unittest
|
||||
from dify_client.exceptions import (
|
||||
DifyClientError,
|
||||
APIError,
|
||||
AuthenticationError,
|
||||
RateLimitError,
|
||||
ValidationError,
|
||||
NetworkError,
|
||||
TimeoutError,
|
||||
FileUploadError,
|
||||
DatasetError,
|
||||
WorkflowError,
|
||||
)
|
||||
|
||||
|
||||
class TestExceptions(unittest.TestCase):
|
||||
"""Test custom exception classes."""
|
||||
|
||||
def test_base_exception(self):
|
||||
"""Test base DifyClientError."""
|
||||
error = DifyClientError("Test message", 500, {"error": "details"})
|
||||
self.assertEqual(str(error), "Test message")
|
||||
self.assertEqual(error.status_code, 500)
|
||||
self.assertEqual(error.response, {"error": "details"})
|
||||
|
||||
def test_api_error(self):
|
||||
"""Test APIError."""
|
||||
error = APIError("API failed", 400)
|
||||
self.assertEqual(error.status_code, 400)
|
||||
self.assertEqual(error.message, "API failed")
|
||||
|
||||
def test_authentication_error(self):
|
||||
"""Test AuthenticationError."""
|
||||
error = AuthenticationError("Invalid API key")
|
||||
self.assertEqual(str(error), "Invalid API key")
|
||||
|
||||
def test_rate_limit_error(self):
|
||||
"""Test RateLimitError."""
|
||||
error = RateLimitError("Rate limited", retry_after=60)
|
||||
self.assertEqual(error.retry_after, 60)
|
||||
|
||||
error_default = RateLimitError()
|
||||
self.assertEqual(error_default.retry_after, None)
|
||||
|
||||
def test_validation_error(self):
|
||||
"""Test ValidationError."""
|
||||
error = ValidationError("Invalid parameter")
|
||||
self.assertEqual(str(error), "Invalid parameter")
|
||||
|
||||
def test_network_error(self):
|
||||
"""Test NetworkError."""
|
||||
error = NetworkError("Connection failed")
|
||||
self.assertEqual(str(error), "Connection failed")
|
||||
|
||||
def test_timeout_error(self):
|
||||
"""Test TimeoutError."""
|
||||
error = TimeoutError("Request timed out")
|
||||
self.assertEqual(str(error), "Request timed out")
|
||||
|
||||
def test_file_upload_error(self):
|
||||
"""Test FileUploadError."""
|
||||
error = FileUploadError("Upload failed")
|
||||
self.assertEqual(str(error), "Upload failed")
|
||||
|
||||
def test_dataset_error(self):
|
||||
"""Test DatasetError."""
|
||||
error = DatasetError("Dataset operation failed")
|
||||
self.assertEqual(str(error), "Dataset operation failed")
|
||||
|
||||
def test_workflow_error(self):
|
||||
"""Test WorkflowError."""
|
||||
error = WorkflowError("Workflow failed")
|
||||
self.assertEqual(str(error), "Workflow failed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
333
dify/sdks/python-client/tests/test_httpx_migration.py
Normal file
333
dify/sdks/python-client/tests/test_httpx_migration.py
Normal file
@@ -0,0 +1,333 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test suite for httpx migration in the Python SDK.
|
||||
|
||||
This test validates that the migration from requests to httpx maintains
|
||||
backward compatibility and proper resource management.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from dify_client import (
|
||||
DifyClient,
|
||||
ChatClient,
|
||||
CompletionClient,
|
||||
WorkflowClient,
|
||||
WorkspaceClient,
|
||||
KnowledgeBaseClient,
|
||||
)
|
||||
|
||||
|
||||
class TestHttpxMigrationMocked(unittest.TestCase):
|
||||
"""Test cases for httpx migration with mocked requests."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up test fixtures."""
|
||||
self.api_key = "test-api-key"
|
||||
self.base_url = "https://api.dify.ai/v1"
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_client_initialization(self, mock_httpx_client):
|
||||
"""Test that client initializes with httpx.Client."""
|
||||
mock_client_instance = Mock()
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
client = DifyClient(self.api_key, self.base_url)
|
||||
|
||||
# Verify httpx.Client was called with correct parameters
|
||||
mock_httpx_client.assert_called_once()
|
||||
call_kwargs = mock_httpx_client.call_args[1]
|
||||
self.assertEqual(call_kwargs["base_url"], self.base_url)
|
||||
|
||||
# Verify client properties
|
||||
self.assertEqual(client.api_key, self.api_key)
|
||||
self.assertEqual(client.base_url, self.base_url)
|
||||
|
||||
client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_context_manager_support(self, mock_httpx_client):
|
||||
"""Test that client works as context manager."""
|
||||
mock_client_instance = Mock()
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with DifyClient(self.api_key, self.base_url) as client:
|
||||
self.assertEqual(client.api_key, self.api_key)
|
||||
|
||||
# Verify close was called
|
||||
mock_client_instance.close.assert_called_once()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_manual_close(self, mock_httpx_client):
|
||||
"""Test manual close() method."""
|
||||
mock_client_instance = Mock()
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
client = DifyClient(self.api_key, self.base_url)
|
||||
client.close()
|
||||
|
||||
# Verify close was called
|
||||
mock_client_instance.close.assert_called_once()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_send_request_httpx_compatibility(self, mock_httpx_client):
|
||||
"""Test _send_request uses httpx.Client.request properly."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
client = DifyClient(self.api_key, self.base_url)
|
||||
response = client._send_request("GET", "/test-endpoint")
|
||||
|
||||
# Verify httpx.Client.request was called correctly
|
||||
mock_client_instance.request.assert_called_once()
|
||||
call_args = mock_client_instance.request.call_args
|
||||
|
||||
# Verify method and endpoint
|
||||
self.assertEqual(call_args[0][0], "GET")
|
||||
self.assertEqual(call_args[0][1], "/test-endpoint")
|
||||
|
||||
# Verify headers contain authorization
|
||||
headers = call_args[1]["headers"]
|
||||
self.assertEqual(headers["Authorization"], f"Bearer {self.api_key}")
|
||||
self.assertEqual(headers["Content-Type"], "application/json")
|
||||
|
||||
client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_response_compatibility(self, mock_httpx_client):
|
||||
"""Test httpx.Response is compatible with requests.Response API."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"key": "value"}
|
||||
mock_response.text = '{"key": "value"}'
|
||||
mock_response.content = b'{"key": "value"}'
|
||||
mock_response.status_code = 200
|
||||
mock_response.headers = {"Content-Type": "application/json"}
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
client = DifyClient(self.api_key, self.base_url)
|
||||
response = client._send_request("GET", "/test")
|
||||
|
||||
# Verify all common response methods work
|
||||
self.assertEqual(response.json(), {"key": "value"})
|
||||
self.assertEqual(response.text, '{"key": "value"}')
|
||||
self.assertEqual(response.content, b'{"key": "value"}')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(response.headers["Content-Type"], "application/json")
|
||||
|
||||
client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_all_client_classes_use_httpx(self, mock_httpx_client):
|
||||
"""Test that all client classes properly use httpx."""
|
||||
mock_client_instance = Mock()
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
clients = [
|
||||
DifyClient(self.api_key, self.base_url),
|
||||
ChatClient(self.api_key, self.base_url),
|
||||
CompletionClient(self.api_key, self.base_url),
|
||||
WorkflowClient(self.api_key, self.base_url),
|
||||
WorkspaceClient(self.api_key, self.base_url),
|
||||
KnowledgeBaseClient(self.api_key, self.base_url),
|
||||
]
|
||||
|
||||
# Verify httpx.Client was called for each client
|
||||
self.assertEqual(mock_httpx_client.call_count, 6)
|
||||
|
||||
# Clean up
|
||||
for client in clients:
|
||||
client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_json_parameter_handling(self, mock_httpx_client):
|
||||
"""Test that json parameter is passed correctly."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200 # Add status_code attribute
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
client = DifyClient(self.api_key, self.base_url)
|
||||
test_data = {"key": "value", "number": 123}
|
||||
|
||||
client._send_request("POST", "/test", json=test_data)
|
||||
|
||||
# Verify json parameter was passed
|
||||
call_args = mock_client_instance.request.call_args
|
||||
self.assertEqual(call_args[1]["json"], test_data)
|
||||
|
||||
client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_params_parameter_handling(self, mock_httpx_client):
|
||||
"""Test that params parameter is passed correctly."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200 # Add status_code attribute
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
client = DifyClient(self.api_key, self.base_url)
|
||||
test_params = {"page": 1, "limit": 20}
|
||||
|
||||
client._send_request("GET", "/test", params=test_params)
|
||||
|
||||
# Verify params parameter was passed
|
||||
call_args = mock_client_instance.request.call_args
|
||||
self.assertEqual(call_args[1]["params"], test_params)
|
||||
|
||||
client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_inheritance_chain(self, mock_httpx_client):
|
||||
"""Test that inheritance chain is maintained."""
|
||||
mock_client_instance = Mock()
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
# ChatClient inherits from DifyClient
|
||||
chat_client = ChatClient(self.api_key, self.base_url)
|
||||
self.assertIsInstance(chat_client, DifyClient)
|
||||
|
||||
# CompletionClient inherits from DifyClient
|
||||
completion_client = CompletionClient(self.api_key, self.base_url)
|
||||
self.assertIsInstance(completion_client, DifyClient)
|
||||
|
||||
# WorkflowClient inherits from DifyClient
|
||||
workflow_client = WorkflowClient(self.api_key, self.base_url)
|
||||
self.assertIsInstance(workflow_client, DifyClient)
|
||||
|
||||
# Clean up
|
||||
chat_client.close()
|
||||
completion_client.close()
|
||||
workflow_client.close()
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_nested_context_managers(self, mock_httpx_client):
|
||||
"""Test nested context managers work correctly."""
|
||||
mock_client_instance = Mock()
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with DifyClient(self.api_key, self.base_url) as client1:
|
||||
with ChatClient(self.api_key, self.base_url) as client2:
|
||||
self.assertEqual(client1.api_key, self.api_key)
|
||||
self.assertEqual(client2.api_key, self.api_key)
|
||||
|
||||
# Both close methods should have been called
|
||||
self.assertEqual(mock_client_instance.close.call_count, 2)
|
||||
|
||||
|
||||
class TestChatClientHttpx(unittest.TestCase):
|
||||
"""Test ChatClient specific httpx integration."""
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_chat_message_httpx(self, mock_httpx_client):
|
||||
"""Test create_chat_message works with httpx."""
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "Hello!"}'
|
||||
mock_response.json.return_value = {"answer": "Hello!"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with ChatClient("test-key") as client:
|
||||
response = client.create_chat_message({}, "Hi", "user123")
|
||||
self.assertIn("answer", response.text)
|
||||
self.assertEqual(response.json()["answer"], "Hello!")
|
||||
|
||||
|
||||
class TestCompletionClientHttpx(unittest.TestCase):
|
||||
"""Test CompletionClient specific httpx integration."""
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_create_completion_message_httpx(self, mock_httpx_client):
|
||||
"""Test create_completion_message works with httpx."""
|
||||
mock_response = Mock()
|
||||
mock_response.text = '{"answer": "Response"}'
|
||||
mock_response.json.return_value = {"answer": "Response"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with CompletionClient("test-key") as client:
|
||||
response = client.create_completion_message({"query": "test"}, "blocking", "user123")
|
||||
self.assertIn("answer", response.text)
|
||||
|
||||
|
||||
class TestKnowledgeBaseClientHttpx(unittest.TestCase):
|
||||
"""Test KnowledgeBaseClient specific httpx integration."""
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_list_datasets_httpx(self, mock_httpx_client):
|
||||
"""Test list_datasets works with httpx."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": [], "total": 0}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with KnowledgeBaseClient("test-key") as client:
|
||||
response = client.list_datasets()
|
||||
data = response.json()
|
||||
self.assertIn("data", data)
|
||||
self.assertIn("total", data)
|
||||
|
||||
|
||||
class TestWorkflowClientHttpx(unittest.TestCase):
|
||||
"""Test WorkflowClient specific httpx integration."""
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_run_workflow_httpx(self, mock_httpx_client):
|
||||
"""Test run workflow works with httpx."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"result": "success"}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with WorkflowClient("test-key") as client:
|
||||
response = client.run({"input": "test"}, "blocking", "user123")
|
||||
self.assertEqual(response.json()["result"], "success")
|
||||
|
||||
|
||||
class TestWorkspaceClientHttpx(unittest.TestCase):
|
||||
"""Test WorkspaceClient specific httpx integration."""
|
||||
|
||||
@patch("dify_client.client.httpx.Client")
|
||||
def test_get_available_models_httpx(self, mock_httpx_client):
|
||||
"""Test get_available_models works with httpx."""
|
||||
mock_response = Mock()
|
||||
mock_response.json.return_value = {"data": []}
|
||||
mock_response.status_code = 200
|
||||
|
||||
mock_client_instance = Mock()
|
||||
mock_client_instance.request.return_value = mock_response
|
||||
mock_httpx_client.return_value = mock_client_instance
|
||||
|
||||
with WorkspaceClient("test-key") as client:
|
||||
response = client.get_available_models("llm")
|
||||
self.assertIn("data", response.json())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
539
dify/sdks/python-client/tests/test_integration.py
Normal file
539
dify/sdks/python-client/tests/test_integration.py
Normal file
@@ -0,0 +1,539 @@
|
||||
"""Integration tests with proper mocking."""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
import json
|
||||
import httpx
|
||||
from dify_client import (
|
||||
DifyClient,
|
||||
ChatClient,
|
||||
CompletionClient,
|
||||
WorkflowClient,
|
||||
KnowledgeBaseClient,
|
||||
WorkspaceClient,
|
||||
)
|
||||
from dify_client.exceptions import (
|
||||
APIError,
|
||||
AuthenticationError,
|
||||
RateLimitError,
|
||||
ValidationError,
|
||||
)
|
||||
|
||||
|
||||
class TestDifyClientIntegration(unittest.TestCase):
|
||||
"""Integration tests for DifyClient with mocked HTTP responses."""
|
||||
|
||||
def setUp(self):
|
||||
self.api_key = "test_api_key"
|
||||
self.base_url = "https://api.dify.ai/v1"
|
||||
self.client = DifyClient(api_key=self.api_key, base_url=self.base_url, enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_get_app_info_integration(self, mock_request):
|
||||
"""Test get_app_info integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "app_123",
|
||||
"name": "Test App",
|
||||
"description": "A test application",
|
||||
"mode": "chat",
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.get_app_info()
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["id"], "app_123")
|
||||
self.assertEqual(data["name"], "Test App")
|
||||
mock_request.assert_called_once_with(
|
||||
"GET",
|
||||
"/info",
|
||||
json=None,
|
||||
params=None,
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_get_application_parameters_integration(self, mock_request):
|
||||
"""Test get_application_parameters integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"opening_statement": "Hello! How can I help you?",
|
||||
"suggested_questions": ["What is AI?", "How does this work?"],
|
||||
"speech_to_text": {"enabled": True},
|
||||
"text_to_speech": {"enabled": False},
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.get_application_parameters("user_123")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["opening_statement"], "Hello! How can I help you?")
|
||||
self.assertEqual(len(data["suggested_questions"]), 2)
|
||||
mock_request.assert_called_once_with(
|
||||
"GET",
|
||||
"/parameters",
|
||||
json=None,
|
||||
params={"user": "user_123"},
|
||||
headers={
|
||||
"Authorization": f"Bearer {self.api_key}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_file_upload_integration(self, mock_request):
|
||||
"""Test file_upload integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "file_123",
|
||||
"name": "test.txt",
|
||||
"size": 1024,
|
||||
"mime_type": "text/plain",
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
files = {"file": ("test.txt", "test content", "text/plain")}
|
||||
response = self.client.file_upload("user_123", files)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["id"], "file_123")
|
||||
self.assertEqual(data["name"], "test.txt")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_message_feedback_integration(self, mock_request):
|
||||
"""Test message_feedback integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {"success": True}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.message_feedback("msg_123", "like", "user_123")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(data["success"])
|
||||
mock_request.assert_called_once_with(
|
||||
"POST",
|
||||
"/messages/msg_123/feedbacks",
|
||||
json={"rating": "like", "user": "user_123"},
|
||||
params=None,
|
||||
headers={
|
||||
"Authorization": "Bearer test_api_key",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class TestChatClientIntegration(unittest.TestCase):
|
||||
"""Integration tests for ChatClient."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = ChatClient("test_api_key", enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_create_chat_message_blocking(self, mock_request):
|
||||
"""Test create_chat_message with blocking response."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "msg_123",
|
||||
"answer": "Hello! How can I help you today?",
|
||||
"conversation_id": "conv_123",
|
||||
"created_at": 1234567890,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.create_chat_message(
|
||||
inputs={"query": "Hello"},
|
||||
query="Hello, AI!",
|
||||
user="user_123",
|
||||
response_mode="blocking",
|
||||
)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["answer"], "Hello! How can I help you today?")
|
||||
self.assertEqual(data["conversation_id"], "conv_123")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_create_chat_message_streaming(self, mock_request):
|
||||
"""Test create_chat_message with streaming response."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.iter_lines.return_value = [
|
||||
b'data: {"answer": "Hello"}',
|
||||
b'data: {"answer": " world"}',
|
||||
b'data: {"answer": "!"}',
|
||||
]
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.create_chat_message(inputs={}, query="Hello", user="user_123", response_mode="streaming")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
lines = list(response.iter_lines())
|
||||
self.assertEqual(len(lines), 3)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_get_conversations_integration(self, mock_request):
|
||||
"""Test get_conversations integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"data": [
|
||||
{"id": "conv_1", "name": "Conversation 1"},
|
||||
{"id": "conv_2", "name": "Conversation 2"},
|
||||
],
|
||||
"has_more": False,
|
||||
"limit": 20,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.get_conversations("user_123", limit=20)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(data["data"]), 2)
|
||||
self.assertEqual(data["data"][0]["name"], "Conversation 1")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_get_conversation_messages_integration(self, mock_request):
|
||||
"""Test get_conversation_messages integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"data": [
|
||||
{"id": "msg_1", "role": "user", "content": "Hello"},
|
||||
{"id": "msg_2", "role": "assistant", "content": "Hi there!"},
|
||||
]
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.get_conversation_messages("user_123", conversation_id="conv_123")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(data["data"]), 2)
|
||||
self.assertEqual(data["data"][0]["role"], "user")
|
||||
|
||||
|
||||
class TestCompletionClientIntegration(unittest.TestCase):
|
||||
"""Integration tests for CompletionClient."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = CompletionClient("test_api_key", enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_create_completion_message_blocking(self, mock_request):
|
||||
"""Test create_completion_message with blocking response."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "comp_123",
|
||||
"answer": "This is a completion response.",
|
||||
"created_at": 1234567890,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.create_completion_message(
|
||||
inputs={"prompt": "Complete this sentence"},
|
||||
response_mode="blocking",
|
||||
user="user_123",
|
||||
)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["answer"], "This is a completion response.")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_create_completion_message_with_files(self, mock_request):
|
||||
"""Test create_completion_message with files."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "comp_124",
|
||||
"answer": "I can see the image shows...",
|
||||
"files": [{"id": "file_1", "type": "image"}],
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
files = {
|
||||
"file": {
|
||||
"type": "image",
|
||||
"transfer_method": "remote_url",
|
||||
"url": "https://example.com/image.jpg",
|
||||
}
|
||||
}
|
||||
response = self.client.create_completion_message(
|
||||
inputs={"prompt": "Describe this image"},
|
||||
response_mode="blocking",
|
||||
user="user_123",
|
||||
files=files,
|
||||
)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn("image", data["answer"])
|
||||
self.assertEqual(len(data["files"]), 1)
|
||||
|
||||
|
||||
class TestWorkflowClientIntegration(unittest.TestCase):
|
||||
"""Integration tests for WorkflowClient."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = WorkflowClient("test_api_key", enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_run_workflow_blocking(self, mock_request):
|
||||
"""Test run workflow with blocking response."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "run_123",
|
||||
"workflow_id": "workflow_123",
|
||||
"status": "succeeded",
|
||||
"inputs": {"query": "Test input"},
|
||||
"outputs": {"result": "Test output"},
|
||||
"elapsed_time": 2.5,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.run(inputs={"query": "Test input"}, response_mode="blocking", user="user_123")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["status"], "succeeded")
|
||||
self.assertEqual(data["outputs"]["result"], "Test output")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_get_workflow_logs(self, mock_request):
|
||||
"""Test get_workflow_logs integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"logs": [
|
||||
{"id": "log_1", "status": "succeeded", "created_at": 1234567890},
|
||||
{"id": "log_2", "status": "failed", "created_at": 1234567891},
|
||||
],
|
||||
"total": 2,
|
||||
"page": 1,
|
||||
"limit": 20,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.get_workflow_logs(page=1, limit=20)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(data["logs"]), 2)
|
||||
self.assertEqual(data["logs"][0]["status"], "succeeded")
|
||||
|
||||
|
||||
class TestKnowledgeBaseClientIntegration(unittest.TestCase):
|
||||
"""Integration tests for KnowledgeBaseClient."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = KnowledgeBaseClient("test_api_key")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_create_dataset(self, mock_request):
|
||||
"""Test create_dataset integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"id": "dataset_123",
|
||||
"name": "Test Dataset",
|
||||
"description": "A test dataset",
|
||||
"created_at": 1234567890,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.create_dataset(name="Test Dataset")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["name"], "Test Dataset")
|
||||
self.assertEqual(data["id"], "dataset_123")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_list_datasets(self, mock_request):
|
||||
"""Test list_datasets integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"data": [
|
||||
{"id": "dataset_1", "name": "Dataset 1"},
|
||||
{"id": "dataset_2", "name": "Dataset 2"},
|
||||
],
|
||||
"has_more": False,
|
||||
"limit": 20,
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.list_datasets(page=1, page_size=20)
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(data["data"]), 2)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_create_document_by_text(self, mock_request):
|
||||
"""Test create_document_by_text integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"document": {
|
||||
"id": "doc_123",
|
||||
"name": "Test Document",
|
||||
"word_count": 100,
|
||||
"status": "indexing",
|
||||
}
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
# Mock dataset_id
|
||||
self.client.dataset_id = "dataset_123"
|
||||
|
||||
response = self.client.create_document_by_text(name="Test Document", text="This is test document content.")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(data["document"]["name"], "Test Document")
|
||||
self.assertEqual(data["document"]["word_count"], 100)
|
||||
|
||||
|
||||
class TestWorkspaceClientIntegration(unittest.TestCase):
|
||||
"""Integration tests for WorkspaceClient."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = WorkspaceClient("test_api_key", enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_get_available_models(self, mock_request):
|
||||
"""Test get_available_models integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {
|
||||
"models": [
|
||||
{"id": "gpt-4", "name": "GPT-4", "provider": "openai"},
|
||||
{"id": "claude-3", "name": "Claude 3", "provider": "anthropic"},
|
||||
]
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client.get_available_models("llm")
|
||||
data = response.json()
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(data["models"]), 2)
|
||||
self.assertEqual(data["models"][0]["id"], "gpt-4")
|
||||
|
||||
|
||||
class TestErrorScenariosIntegration(unittest.TestCase):
|
||||
"""Integration tests for error scenarios."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = DifyClient("test_api_key", enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_authentication_error_integration(self, mock_request):
|
||||
"""Test authentication error in integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 401
|
||||
mock_response.json.return_value = {"message": "Invalid API key"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(AuthenticationError) as context:
|
||||
self.client.get_app_info()
|
||||
|
||||
self.assertEqual(str(context.exception), "Invalid API key")
|
||||
self.assertEqual(context.exception.status_code, 401)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_rate_limit_error_integration(self, mock_request):
|
||||
"""Test rate limit error in integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 429
|
||||
mock_response.json.return_value = {"message": "Rate limit exceeded"}
|
||||
mock_response.headers = {"Retry-After": "60"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(RateLimitError) as context:
|
||||
self.client.get_app_info()
|
||||
|
||||
self.assertEqual(str(context.exception), "Rate limit exceeded")
|
||||
self.assertEqual(context.exception.retry_after, "60")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_server_error_with_retry_integration(self, mock_request):
|
||||
"""Test server error with retry in integration."""
|
||||
# API errors don't retry by design - only network/timeout errors retry
|
||||
mock_response_500 = Mock()
|
||||
mock_response_500.status_code = 500
|
||||
mock_response_500.json.return_value = {"message": "Internal server error"}
|
||||
|
||||
mock_request.return_value = mock_response_500
|
||||
|
||||
with patch("time.sleep"): # Skip actual sleep
|
||||
with self.assertRaises(APIError) as context:
|
||||
self.client.get_app_info()
|
||||
|
||||
self.assertEqual(str(context.exception), "Internal server error")
|
||||
self.assertEqual(mock_request.call_count, 1)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_validation_error_integration(self, mock_request):
|
||||
"""Test validation error in integration."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 422
|
||||
mock_response.json.return_value = {
|
||||
"message": "Validation failed",
|
||||
"details": {"field": "query", "error": "required"},
|
||||
}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
self.client.get_app_info()
|
||||
|
||||
self.assertEqual(str(context.exception), "Validation failed")
|
||||
self.assertEqual(context.exception.status_code, 422)
|
||||
|
||||
|
||||
class TestContextManagerIntegration(unittest.TestCase):
|
||||
"""Integration tests for context manager usage."""
|
||||
|
||||
@patch("httpx.Client.close")
|
||||
@patch("httpx.Client.request")
|
||||
def test_context_manager_usage(self, mock_request, mock_close):
|
||||
"""Test context manager properly closes connections."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.json.return_value = {"id": "app_123", "name": "Test App"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with DifyClient("test_api_key") as client:
|
||||
response = client.get_app_info()
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# Verify close was called
|
||||
mock_close.assert_called_once()
|
||||
|
||||
@patch("httpx.Client.close")
|
||||
def test_manual_close(self, mock_close):
|
||||
"""Test manual close method."""
|
||||
client = DifyClient("test_api_key")
|
||||
client.close()
|
||||
mock_close.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
640
dify/sdks/python-client/tests/test_models.py
Normal file
640
dify/sdks/python-client/tests/test_models.py
Normal file
@@ -0,0 +1,640 @@
|
||||
"""Unit tests for response models."""
|
||||
|
||||
import unittest
|
||||
import json
|
||||
from datetime import datetime
|
||||
from dify_client.models import (
|
||||
BaseResponse,
|
||||
ErrorResponse,
|
||||
FileInfo,
|
||||
MessageResponse,
|
||||
ConversationResponse,
|
||||
DatasetResponse,
|
||||
DocumentResponse,
|
||||
DocumentSegmentResponse,
|
||||
WorkflowRunResponse,
|
||||
ApplicationParametersResponse,
|
||||
AnnotationResponse,
|
||||
PaginatedResponse,
|
||||
ConversationVariableResponse,
|
||||
FileUploadResponse,
|
||||
AudioResponse,
|
||||
SuggestedQuestionsResponse,
|
||||
AppInfoResponse,
|
||||
WorkspaceModelsResponse,
|
||||
HitTestingResponse,
|
||||
DatasetTagsResponse,
|
||||
WorkflowLogsResponse,
|
||||
ModelProviderResponse,
|
||||
FileInfoResponse,
|
||||
WorkflowDraftResponse,
|
||||
ApiTokenResponse,
|
||||
JobStatusResponse,
|
||||
DatasetQueryResponse,
|
||||
DatasetTemplateResponse,
|
||||
)
|
||||
|
||||
|
||||
class TestResponseModels(unittest.TestCase):
|
||||
"""Test cases for response model classes."""
|
||||
|
||||
def test_base_response(self):
|
||||
"""Test BaseResponse model."""
|
||||
response = BaseResponse(success=True, message="Operation successful")
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.message, "Operation successful")
|
||||
|
||||
def test_base_response_defaults(self):
|
||||
"""Test BaseResponse with default values."""
|
||||
response = BaseResponse(success=True)
|
||||
self.assertTrue(response.success)
|
||||
self.assertIsNone(response.message)
|
||||
|
||||
def test_error_response(self):
|
||||
"""Test ErrorResponse model."""
|
||||
response = ErrorResponse(
|
||||
success=False,
|
||||
message="Error occurred",
|
||||
error_code="VALIDATION_ERROR",
|
||||
details={"field": "invalid_value"},
|
||||
)
|
||||
self.assertFalse(response.success)
|
||||
self.assertEqual(response.message, "Error occurred")
|
||||
self.assertEqual(response.error_code, "VALIDATION_ERROR")
|
||||
self.assertEqual(response.details["field"], "invalid_value")
|
||||
|
||||
def test_file_info(self):
|
||||
"""Test FileInfo model."""
|
||||
now = datetime.now()
|
||||
file_info = FileInfo(
|
||||
id="file_123",
|
||||
name="test.txt",
|
||||
size=1024,
|
||||
mime_type="text/plain",
|
||||
url="https://example.com/file.txt",
|
||||
created_at=now,
|
||||
)
|
||||
self.assertEqual(file_info.id, "file_123")
|
||||
self.assertEqual(file_info.name, "test.txt")
|
||||
self.assertEqual(file_info.size, 1024)
|
||||
self.assertEqual(file_info.mime_type, "text/plain")
|
||||
self.assertEqual(file_info.url, "https://example.com/file.txt")
|
||||
self.assertEqual(file_info.created_at, now)
|
||||
|
||||
def test_message_response(self):
|
||||
"""Test MessageResponse model."""
|
||||
response = MessageResponse(
|
||||
success=True,
|
||||
id="msg_123",
|
||||
answer="Hello, world!",
|
||||
conversation_id="conv_123",
|
||||
created_at=1234567890,
|
||||
metadata={"model": "gpt-4"},
|
||||
files=[{"id": "file_1", "type": "image"}],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "msg_123")
|
||||
self.assertEqual(response.answer, "Hello, world!")
|
||||
self.assertEqual(response.conversation_id, "conv_123")
|
||||
self.assertEqual(response.created_at, 1234567890)
|
||||
self.assertEqual(response.metadata["model"], "gpt-4")
|
||||
self.assertEqual(response.files[0]["id"], "file_1")
|
||||
|
||||
def test_conversation_response(self):
|
||||
"""Test ConversationResponse model."""
|
||||
response = ConversationResponse(
|
||||
success=True,
|
||||
id="conv_123",
|
||||
name="Test Conversation",
|
||||
inputs={"query": "Hello"},
|
||||
status="active",
|
||||
created_at=1234567890,
|
||||
updated_at=1234567891,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "conv_123")
|
||||
self.assertEqual(response.name, "Test Conversation")
|
||||
self.assertEqual(response.inputs["query"], "Hello")
|
||||
self.assertEqual(response.status, "active")
|
||||
self.assertEqual(response.created_at, 1234567890)
|
||||
self.assertEqual(response.updated_at, 1234567891)
|
||||
|
||||
def test_dataset_response(self):
|
||||
"""Test DatasetResponse model."""
|
||||
response = DatasetResponse(
|
||||
success=True,
|
||||
id="dataset_123",
|
||||
name="Test Dataset",
|
||||
description="A test dataset",
|
||||
permission="read",
|
||||
indexing_technique="high_quality",
|
||||
embedding_model="text-embedding-ada-002",
|
||||
embedding_model_provider="openai",
|
||||
retrieval_model={"search_type": "semantic"},
|
||||
document_count=10,
|
||||
word_count=5000,
|
||||
app_count=2,
|
||||
created_at=1234567890,
|
||||
updated_at=1234567891,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "dataset_123")
|
||||
self.assertEqual(response.name, "Test Dataset")
|
||||
self.assertEqual(response.description, "A test dataset")
|
||||
self.assertEqual(response.permission, "read")
|
||||
self.assertEqual(response.indexing_technique, "high_quality")
|
||||
self.assertEqual(response.embedding_model, "text-embedding-ada-002")
|
||||
self.assertEqual(response.embedding_model_provider, "openai")
|
||||
self.assertEqual(response.retrieval_model["search_type"], "semantic")
|
||||
self.assertEqual(response.document_count, 10)
|
||||
self.assertEqual(response.word_count, 5000)
|
||||
self.assertEqual(response.app_count, 2)
|
||||
|
||||
def test_document_response(self):
|
||||
"""Test DocumentResponse model."""
|
||||
response = DocumentResponse(
|
||||
success=True,
|
||||
id="doc_123",
|
||||
name="test_document.txt",
|
||||
data_source_type="upload_file",
|
||||
position=1,
|
||||
enabled=True,
|
||||
word_count=1000,
|
||||
hit_count=5,
|
||||
doc_form="text_model",
|
||||
created_at=1234567890.0,
|
||||
indexing_status="completed",
|
||||
completed_at=1234567891.0,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "doc_123")
|
||||
self.assertEqual(response.name, "test_document.txt")
|
||||
self.assertEqual(response.data_source_type, "upload_file")
|
||||
self.assertEqual(response.position, 1)
|
||||
self.assertTrue(response.enabled)
|
||||
self.assertEqual(response.word_count, 1000)
|
||||
self.assertEqual(response.hit_count, 5)
|
||||
self.assertEqual(response.doc_form, "text_model")
|
||||
self.assertEqual(response.created_at, 1234567890.0)
|
||||
self.assertEqual(response.indexing_status, "completed")
|
||||
self.assertEqual(response.completed_at, 1234567891.0)
|
||||
|
||||
def test_document_segment_response(self):
|
||||
"""Test DocumentSegmentResponse model."""
|
||||
response = DocumentSegmentResponse(
|
||||
success=True,
|
||||
id="seg_123",
|
||||
position=1,
|
||||
document_id="doc_123",
|
||||
content="This is a test segment.",
|
||||
answer="Test answer",
|
||||
word_count=5,
|
||||
tokens=10,
|
||||
keywords=["test", "segment"],
|
||||
hit_count=2,
|
||||
enabled=True,
|
||||
status="completed",
|
||||
created_at=1234567890.0,
|
||||
completed_at=1234567891.0,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "seg_123")
|
||||
self.assertEqual(response.position, 1)
|
||||
self.assertEqual(response.document_id, "doc_123")
|
||||
self.assertEqual(response.content, "This is a test segment.")
|
||||
self.assertEqual(response.answer, "Test answer")
|
||||
self.assertEqual(response.word_count, 5)
|
||||
self.assertEqual(response.tokens, 10)
|
||||
self.assertEqual(response.keywords, ["test", "segment"])
|
||||
self.assertEqual(response.hit_count, 2)
|
||||
self.assertTrue(response.enabled)
|
||||
self.assertEqual(response.status, "completed")
|
||||
self.assertEqual(response.created_at, 1234567890.0)
|
||||
self.assertEqual(response.completed_at, 1234567891.0)
|
||||
|
||||
def test_workflow_run_response(self):
|
||||
"""Test WorkflowRunResponse model."""
|
||||
response = WorkflowRunResponse(
|
||||
success=True,
|
||||
id="run_123",
|
||||
workflow_id="workflow_123",
|
||||
status="succeeded",
|
||||
inputs={"query": "test"},
|
||||
outputs={"answer": "result"},
|
||||
elapsed_time=5.5,
|
||||
total_tokens=100,
|
||||
total_steps=3,
|
||||
created_at=1234567890.0,
|
||||
finished_at=1234567895.5,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "run_123")
|
||||
self.assertEqual(response.workflow_id, "workflow_123")
|
||||
self.assertEqual(response.status, "succeeded")
|
||||
self.assertEqual(response.inputs["query"], "test")
|
||||
self.assertEqual(response.outputs["answer"], "result")
|
||||
self.assertEqual(response.elapsed_time, 5.5)
|
||||
self.assertEqual(response.total_tokens, 100)
|
||||
self.assertEqual(response.total_steps, 3)
|
||||
self.assertEqual(response.created_at, 1234567890.0)
|
||||
self.assertEqual(response.finished_at, 1234567895.5)
|
||||
|
||||
def test_application_parameters_response(self):
|
||||
"""Test ApplicationParametersResponse model."""
|
||||
response = ApplicationParametersResponse(
|
||||
success=True,
|
||||
opening_statement="Hello! How can I help you?",
|
||||
suggested_questions=["What is AI?", "How does this work?"],
|
||||
speech_to_text={"enabled": True},
|
||||
text_to_speech={"enabled": False, "voice": "alloy"},
|
||||
retriever_resource={"enabled": True},
|
||||
sensitive_word_avoidance={"enabled": False},
|
||||
file_upload={"enabled": True, "file_size_limit": 10485760},
|
||||
system_parameters={"max_tokens": 1000},
|
||||
user_input_form=[{"type": "text", "label": "Query"}],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.opening_statement, "Hello! How can I help you?")
|
||||
self.assertEqual(response.suggested_questions, ["What is AI?", "How does this work?"])
|
||||
self.assertTrue(response.speech_to_text["enabled"])
|
||||
self.assertFalse(response.text_to_speech["enabled"])
|
||||
self.assertEqual(response.text_to_speech["voice"], "alloy")
|
||||
self.assertTrue(response.retriever_resource["enabled"])
|
||||
self.assertFalse(response.sensitive_word_avoidance["enabled"])
|
||||
self.assertTrue(response.file_upload["enabled"])
|
||||
self.assertEqual(response.file_upload["file_size_limit"], 10485760)
|
||||
self.assertEqual(response.system_parameters["max_tokens"], 1000)
|
||||
self.assertEqual(response.user_input_form[0]["type"], "text")
|
||||
|
||||
def test_annotation_response(self):
|
||||
"""Test AnnotationResponse model."""
|
||||
response = AnnotationResponse(
|
||||
success=True,
|
||||
id="annotation_123",
|
||||
question="What is the capital of France?",
|
||||
answer="Paris",
|
||||
content="Additional context",
|
||||
created_at=1234567890.0,
|
||||
updated_at=1234567891.0,
|
||||
created_by="user_123",
|
||||
updated_by="user_123",
|
||||
hit_count=5,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "annotation_123")
|
||||
self.assertEqual(response.question, "What is the capital of France?")
|
||||
self.assertEqual(response.answer, "Paris")
|
||||
self.assertEqual(response.content, "Additional context")
|
||||
self.assertEqual(response.created_at, 1234567890.0)
|
||||
self.assertEqual(response.updated_at, 1234567891.0)
|
||||
self.assertEqual(response.created_by, "user_123")
|
||||
self.assertEqual(response.updated_by, "user_123")
|
||||
self.assertEqual(response.hit_count, 5)
|
||||
|
||||
def test_paginated_response(self):
|
||||
"""Test PaginatedResponse model."""
|
||||
response = PaginatedResponse(
|
||||
success=True,
|
||||
data=[{"id": 1}, {"id": 2}, {"id": 3}],
|
||||
has_more=True,
|
||||
limit=10,
|
||||
total=100,
|
||||
page=1,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(len(response.data), 3)
|
||||
self.assertEqual(response.data[0]["id"], 1)
|
||||
self.assertTrue(response.has_more)
|
||||
self.assertEqual(response.limit, 10)
|
||||
self.assertEqual(response.total, 100)
|
||||
self.assertEqual(response.page, 1)
|
||||
|
||||
def test_conversation_variable_response(self):
|
||||
"""Test ConversationVariableResponse model."""
|
||||
response = ConversationVariableResponse(
|
||||
success=True,
|
||||
conversation_id="conv_123",
|
||||
variables=[
|
||||
{"id": "var_1", "name": "user_name", "value": "John"},
|
||||
{"id": "var_2", "name": "preferences", "value": {"theme": "dark"}},
|
||||
],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.conversation_id, "conv_123")
|
||||
self.assertEqual(len(response.variables), 2)
|
||||
self.assertEqual(response.variables[0]["name"], "user_name")
|
||||
self.assertEqual(response.variables[0]["value"], "John")
|
||||
self.assertEqual(response.variables[1]["name"], "preferences")
|
||||
self.assertEqual(response.variables[1]["value"]["theme"], "dark")
|
||||
|
||||
def test_file_upload_response(self):
|
||||
"""Test FileUploadResponse model."""
|
||||
response = FileUploadResponse(
|
||||
success=True,
|
||||
id="file_123",
|
||||
name="test.txt",
|
||||
size=1024,
|
||||
mime_type="text/plain",
|
||||
url="https://example.com/files/test.txt",
|
||||
created_at=1234567890.0,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "file_123")
|
||||
self.assertEqual(response.name, "test.txt")
|
||||
self.assertEqual(response.size, 1024)
|
||||
self.assertEqual(response.mime_type, "text/plain")
|
||||
self.assertEqual(response.url, "https://example.com/files/test.txt")
|
||||
self.assertEqual(response.created_at, 1234567890.0)
|
||||
|
||||
def test_audio_response(self):
|
||||
"""Test AudioResponse model."""
|
||||
response = AudioResponse(
|
||||
success=True,
|
||||
audio="base64_encoded_audio_data",
|
||||
audio_url="https://example.com/audio.mp3",
|
||||
duration=10.5,
|
||||
sample_rate=44100,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.audio, "base64_encoded_audio_data")
|
||||
self.assertEqual(response.audio_url, "https://example.com/audio.mp3")
|
||||
self.assertEqual(response.duration, 10.5)
|
||||
self.assertEqual(response.sample_rate, 44100)
|
||||
|
||||
def test_suggested_questions_response(self):
|
||||
"""Test SuggestedQuestionsResponse model."""
|
||||
response = SuggestedQuestionsResponse(
|
||||
success=True,
|
||||
message_id="msg_123",
|
||||
questions=[
|
||||
"What is machine learning?",
|
||||
"How does AI work?",
|
||||
"Can you explain neural networks?",
|
||||
],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.message_id, "msg_123")
|
||||
self.assertEqual(len(response.questions), 3)
|
||||
self.assertEqual(response.questions[0], "What is machine learning?")
|
||||
|
||||
def test_app_info_response(self):
|
||||
"""Test AppInfoResponse model."""
|
||||
response = AppInfoResponse(
|
||||
success=True,
|
||||
id="app_123",
|
||||
name="Test App",
|
||||
description="A test application",
|
||||
icon="🤖",
|
||||
icon_background="#FF6B6B",
|
||||
mode="chat",
|
||||
tags=["AI", "Chat", "Test"],
|
||||
enable_site=True,
|
||||
enable_api=True,
|
||||
api_token="app_token_123",
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "app_123")
|
||||
self.assertEqual(response.name, "Test App")
|
||||
self.assertEqual(response.description, "A test application")
|
||||
self.assertEqual(response.icon, "🤖")
|
||||
self.assertEqual(response.icon_background, "#FF6B6B")
|
||||
self.assertEqual(response.mode, "chat")
|
||||
self.assertEqual(response.tags, ["AI", "Chat", "Test"])
|
||||
self.assertTrue(response.enable_site)
|
||||
self.assertTrue(response.enable_api)
|
||||
self.assertEqual(response.api_token, "app_token_123")
|
||||
|
||||
def test_workspace_models_response(self):
|
||||
"""Test WorkspaceModelsResponse model."""
|
||||
response = WorkspaceModelsResponse(
|
||||
success=True,
|
||||
models=[
|
||||
{"id": "gpt-4", "name": "GPT-4", "provider": "openai"},
|
||||
{"id": "claude-3", "name": "Claude 3", "provider": "anthropic"},
|
||||
],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(len(response.models), 2)
|
||||
self.assertEqual(response.models[0]["id"], "gpt-4")
|
||||
self.assertEqual(response.models[0]["name"], "GPT-4")
|
||||
self.assertEqual(response.models[0]["provider"], "openai")
|
||||
|
||||
def test_hit_testing_response(self):
|
||||
"""Test HitTestingResponse model."""
|
||||
response = HitTestingResponse(
|
||||
success=True,
|
||||
query="What is machine learning?",
|
||||
records=[
|
||||
{"content": "Machine learning is a subset of AI...", "score": 0.95},
|
||||
{"content": "ML algorithms learn from data...", "score": 0.87},
|
||||
],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.query, "What is machine learning?")
|
||||
self.assertEqual(len(response.records), 2)
|
||||
self.assertEqual(response.records[0]["score"], 0.95)
|
||||
|
||||
def test_dataset_tags_response(self):
|
||||
"""Test DatasetTagsResponse model."""
|
||||
response = DatasetTagsResponse(
|
||||
success=True,
|
||||
tags=[
|
||||
{"id": "tag_1", "name": "Technology", "color": "#FF0000"},
|
||||
{"id": "tag_2", "name": "Science", "color": "#00FF00"},
|
||||
],
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(len(response.tags), 2)
|
||||
self.assertEqual(response.tags[0]["name"], "Technology")
|
||||
self.assertEqual(response.tags[0]["color"], "#FF0000")
|
||||
|
||||
def test_workflow_logs_response(self):
|
||||
"""Test WorkflowLogsResponse model."""
|
||||
response = WorkflowLogsResponse(
|
||||
success=True,
|
||||
logs=[
|
||||
{"id": "log_1", "status": "succeeded", "created_at": 1234567890},
|
||||
{"id": "log_2", "status": "failed", "created_at": 1234567891},
|
||||
],
|
||||
total=50,
|
||||
page=1,
|
||||
limit=10,
|
||||
has_more=True,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(len(response.logs), 2)
|
||||
self.assertEqual(response.logs[0]["status"], "succeeded")
|
||||
self.assertEqual(response.total, 50)
|
||||
self.assertEqual(response.page, 1)
|
||||
self.assertEqual(response.limit, 10)
|
||||
self.assertTrue(response.has_more)
|
||||
|
||||
def test_model_serialization(self):
|
||||
"""Test that models can be serialized to JSON."""
|
||||
response = MessageResponse(
|
||||
success=True,
|
||||
id="msg_123",
|
||||
answer="Hello, world!",
|
||||
conversation_id="conv_123",
|
||||
)
|
||||
|
||||
# Convert to dict and then to JSON
|
||||
response_dict = {
|
||||
"success": response.success,
|
||||
"id": response.id,
|
||||
"answer": response.answer,
|
||||
"conversation_id": response.conversation_id,
|
||||
}
|
||||
|
||||
json_str = json.dumps(response_dict)
|
||||
parsed = json.loads(json_str)
|
||||
|
||||
self.assertTrue(parsed["success"])
|
||||
self.assertEqual(parsed["id"], "msg_123")
|
||||
self.assertEqual(parsed["answer"], "Hello, world!")
|
||||
self.assertEqual(parsed["conversation_id"], "conv_123")
|
||||
|
||||
# Tests for new response models
|
||||
def test_model_provider_response(self):
|
||||
"""Test ModelProviderResponse model."""
|
||||
response = ModelProviderResponse(
|
||||
success=True,
|
||||
provider_name="openai",
|
||||
provider_type="llm",
|
||||
models=[
|
||||
{"id": "gpt-4", "name": "GPT-4", "max_tokens": 8192},
|
||||
{"id": "gpt-3.5-turbo", "name": "GPT-3.5 Turbo", "max_tokens": 4096},
|
||||
],
|
||||
is_enabled=True,
|
||||
credentials={"api_key": "sk-..."},
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.provider_name, "openai")
|
||||
self.assertEqual(response.provider_type, "llm")
|
||||
self.assertEqual(len(response.models), 2)
|
||||
self.assertEqual(response.models[0]["id"], "gpt-4")
|
||||
self.assertTrue(response.is_enabled)
|
||||
self.assertEqual(response.credentials["api_key"], "sk-...")
|
||||
|
||||
def test_file_info_response(self):
|
||||
"""Test FileInfoResponse model."""
|
||||
response = FileInfoResponse(
|
||||
success=True,
|
||||
id="file_123",
|
||||
name="document.pdf",
|
||||
size=2048576,
|
||||
mime_type="application/pdf",
|
||||
url="https://example.com/files/document.pdf",
|
||||
created_at=1234567890,
|
||||
metadata={"pages": 10, "author": "John Doe"},
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "file_123")
|
||||
self.assertEqual(response.name, "document.pdf")
|
||||
self.assertEqual(response.size, 2048576)
|
||||
self.assertEqual(response.mime_type, "application/pdf")
|
||||
self.assertEqual(response.url, "https://example.com/files/document.pdf")
|
||||
self.assertEqual(response.created_at, 1234567890)
|
||||
self.assertEqual(response.metadata["pages"], 10)
|
||||
|
||||
def test_workflow_draft_response(self):
|
||||
"""Test WorkflowDraftResponse model."""
|
||||
response = WorkflowDraftResponse(
|
||||
success=True,
|
||||
id="draft_123",
|
||||
app_id="app_456",
|
||||
draft_data={"nodes": [], "edges": [], "config": {"name": "Test Workflow"}},
|
||||
version=1,
|
||||
created_at=1234567890,
|
||||
updated_at=1234567891,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "draft_123")
|
||||
self.assertEqual(response.app_id, "app_456")
|
||||
self.assertEqual(response.draft_data["config"]["name"], "Test Workflow")
|
||||
self.assertEqual(response.version, 1)
|
||||
self.assertEqual(response.created_at, 1234567890)
|
||||
self.assertEqual(response.updated_at, 1234567891)
|
||||
|
||||
def test_api_token_response(self):
|
||||
"""Test ApiTokenResponse model."""
|
||||
response = ApiTokenResponse(
|
||||
success=True,
|
||||
id="token_123",
|
||||
name="Production Token",
|
||||
token="app-xxxxxxxxxxxx",
|
||||
description="Token for production environment",
|
||||
created_at=1234567890,
|
||||
last_used_at=1234567891,
|
||||
is_active=True,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.id, "token_123")
|
||||
self.assertEqual(response.name, "Production Token")
|
||||
self.assertEqual(response.token, "app-xxxxxxxxxxxx")
|
||||
self.assertEqual(response.description, "Token for production environment")
|
||||
self.assertEqual(response.created_at, 1234567890)
|
||||
self.assertEqual(response.last_used_at, 1234567891)
|
||||
self.assertTrue(response.is_active)
|
||||
|
||||
def test_job_status_response(self):
|
||||
"""Test JobStatusResponse model."""
|
||||
response = JobStatusResponse(
|
||||
success=True,
|
||||
job_id="job_123",
|
||||
job_status="running",
|
||||
error_msg=None,
|
||||
progress=0.75,
|
||||
created_at=1234567890,
|
||||
updated_at=1234567891,
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.job_id, "job_123")
|
||||
self.assertEqual(response.job_status, "running")
|
||||
self.assertIsNone(response.error_msg)
|
||||
self.assertEqual(response.progress, 0.75)
|
||||
self.assertEqual(response.created_at, 1234567890)
|
||||
self.assertEqual(response.updated_at, 1234567891)
|
||||
|
||||
def test_dataset_query_response(self):
|
||||
"""Test DatasetQueryResponse model."""
|
||||
response = DatasetQueryResponse(
|
||||
success=True,
|
||||
query="What is machine learning?",
|
||||
records=[
|
||||
{"content": "Machine learning is...", "score": 0.95},
|
||||
{"content": "ML algorithms...", "score": 0.87},
|
||||
],
|
||||
total=2,
|
||||
search_time=0.123,
|
||||
retrieval_model={"method": "semantic_search", "top_k": 3},
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.query, "What is machine learning?")
|
||||
self.assertEqual(len(response.records), 2)
|
||||
self.assertEqual(response.total, 2)
|
||||
self.assertEqual(response.search_time, 0.123)
|
||||
self.assertEqual(response.retrieval_model["method"], "semantic_search")
|
||||
|
||||
def test_dataset_template_response(self):
|
||||
"""Test DatasetTemplateResponse model."""
|
||||
response = DatasetTemplateResponse(
|
||||
success=True,
|
||||
template_name="customer_support",
|
||||
display_name="Customer Support",
|
||||
description="Template for customer support knowledge base",
|
||||
category="support",
|
||||
icon="🎧",
|
||||
config_schema={"fields": [{"name": "category", "type": "string"}]},
|
||||
)
|
||||
self.assertTrue(response.success)
|
||||
self.assertEqual(response.template_name, "customer_support")
|
||||
self.assertEqual(response.display_name, "Customer Support")
|
||||
self.assertEqual(response.description, "Template for customer support knowledge base")
|
||||
self.assertEqual(response.category, "support")
|
||||
self.assertEqual(response.icon, "🎧")
|
||||
self.assertEqual(response.config_schema["fields"][0]["name"], "category")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
313
dify/sdks/python-client/tests/test_retry_and_error_handling.py
Normal file
313
dify/sdks/python-client/tests/test_retry_and_error_handling.py
Normal file
@@ -0,0 +1,313 @@
|
||||
"""Unit tests for retry mechanism and error handling."""
|
||||
|
||||
import unittest
|
||||
from unittest.mock import Mock, patch, MagicMock
|
||||
import httpx
|
||||
from dify_client.client import DifyClient
|
||||
from dify_client.exceptions import (
|
||||
APIError,
|
||||
AuthenticationError,
|
||||
RateLimitError,
|
||||
ValidationError,
|
||||
NetworkError,
|
||||
TimeoutError,
|
||||
FileUploadError,
|
||||
)
|
||||
|
||||
|
||||
class TestRetryMechanism(unittest.TestCase):
|
||||
"""Test cases for retry mechanism."""
|
||||
|
||||
def setUp(self):
|
||||
self.api_key = "test_api_key"
|
||||
self.base_url = "https://api.dify.ai/v1"
|
||||
self.client = DifyClient(
|
||||
api_key=self.api_key,
|
||||
base_url=self.base_url,
|
||||
max_retries=3,
|
||||
retry_delay=0.1, # Short delay for tests
|
||||
enable_logging=False,
|
||||
)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_successful_request_no_retry(self, mock_request):
|
||||
"""Test that successful requests don't trigger retries."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.content = b'{"success": true}'
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
response = self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(response, mock_response)
|
||||
self.assertEqual(mock_request.call_count, 1)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
@patch("time.sleep")
|
||||
def test_retry_on_network_error(self, mock_sleep, mock_request):
|
||||
"""Test retry on network errors."""
|
||||
# First two calls raise network error, third succeeds
|
||||
mock_request.side_effect = [
|
||||
httpx.NetworkError("Connection failed"),
|
||||
httpx.NetworkError("Connection failed"),
|
||||
Mock(status_code=200, content=b'{"success": true}'),
|
||||
]
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.content = b'{"success": true}'
|
||||
|
||||
response = self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(mock_request.call_count, 3)
|
||||
self.assertEqual(mock_sleep.call_count, 2)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
@patch("time.sleep")
|
||||
def test_retry_on_timeout_error(self, mock_sleep, mock_request):
|
||||
"""Test retry on timeout errors."""
|
||||
mock_request.side_effect = [
|
||||
httpx.TimeoutException("Request timed out"),
|
||||
httpx.TimeoutException("Request timed out"),
|
||||
Mock(status_code=200, content=b'{"success": true}'),
|
||||
]
|
||||
|
||||
response = self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(mock_request.call_count, 3)
|
||||
self.assertEqual(mock_sleep.call_count, 2)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
@patch("time.sleep")
|
||||
def test_max_retries_exceeded(self, mock_sleep, mock_request):
|
||||
"""Test behavior when max retries are exceeded."""
|
||||
mock_request.side_effect = httpx.NetworkError("Persistent network error")
|
||||
|
||||
with self.assertRaises(NetworkError):
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(mock_request.call_count, 4) # 1 initial + 3 retries
|
||||
self.assertEqual(mock_sleep.call_count, 3)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_no_retry_on_client_error(self, mock_request):
|
||||
"""Test that client errors (4xx) don't trigger retries."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 401
|
||||
mock_response.json.return_value = {"message": "Unauthorized"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(AuthenticationError):
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(mock_request.call_count, 1)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_retry_on_server_error(self, mock_request):
|
||||
"""Test that server errors (5xx) don't retry - they raise APIError immediately."""
|
||||
mock_response_500 = Mock()
|
||||
mock_response_500.status_code = 500
|
||||
mock_response_500.json.return_value = {"message": "Internal server error"}
|
||||
|
||||
mock_request.return_value = mock_response_500
|
||||
|
||||
with self.assertRaises(APIError) as context:
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(str(context.exception), "Internal server error")
|
||||
self.assertEqual(context.exception.status_code, 500)
|
||||
# Should not retry server errors
|
||||
self.assertEqual(mock_request.call_count, 1)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_exponential_backoff(self, mock_request):
|
||||
"""Test exponential backoff timing."""
|
||||
mock_request.side_effect = [
|
||||
httpx.NetworkError("Connection failed"),
|
||||
httpx.NetworkError("Connection failed"),
|
||||
httpx.NetworkError("Connection failed"),
|
||||
httpx.NetworkError("Connection failed"), # All attempts fail
|
||||
]
|
||||
|
||||
with patch("time.sleep") as mock_sleep:
|
||||
with self.assertRaises(NetworkError):
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
# Check exponential backoff: 0.1, 0.2, 0.4
|
||||
expected_calls = [0.1, 0.2, 0.4]
|
||||
actual_calls = [call[0][0] for call in mock_sleep.call_args_list]
|
||||
self.assertEqual(actual_calls, expected_calls)
|
||||
|
||||
|
||||
class TestErrorHandling(unittest.TestCase):
|
||||
"""Test cases for error handling."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = DifyClient(api_key="test_api_key", enable_logging=False)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_authentication_error(self, mock_request):
|
||||
"""Test AuthenticationError handling."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 401
|
||||
mock_response.json.return_value = {"message": "Invalid API key"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(AuthenticationError) as context:
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(str(context.exception), "Invalid API key")
|
||||
self.assertEqual(context.exception.status_code, 401)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_rate_limit_error(self, mock_request):
|
||||
"""Test RateLimitError handling."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 429
|
||||
mock_response.json.return_value = {"message": "Rate limit exceeded"}
|
||||
mock_response.headers = {"Retry-After": "60"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(RateLimitError) as context:
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(str(context.exception), "Rate limit exceeded")
|
||||
self.assertEqual(context.exception.retry_after, "60")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_validation_error(self, mock_request):
|
||||
"""Test ValidationError handling."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 422
|
||||
mock_response.json.return_value = {"message": "Invalid parameters"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(ValidationError) as context:
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(str(context.exception), "Invalid parameters")
|
||||
self.assertEqual(context.exception.status_code, 422)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_api_error(self, mock_request):
|
||||
"""Test general APIError handling."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 500
|
||||
mock_response.json.return_value = {"message": "Internal server error"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(APIError) as context:
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(str(context.exception), "Internal server error")
|
||||
self.assertEqual(context.exception.status_code, 500)
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_error_response_without_json(self, mock_request):
|
||||
"""Test error handling when response doesn't contain valid JSON."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 500
|
||||
mock_response.content = b"Internal Server Error"
|
||||
mock_response.json.side_effect = ValueError("No JSON object could be decoded")
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(APIError) as context:
|
||||
self.client._send_request("GET", "/test")
|
||||
|
||||
self.assertEqual(str(context.exception), "HTTP 500")
|
||||
|
||||
@patch("httpx.Client.request")
|
||||
def test_file_upload_error(self, mock_request):
|
||||
"""Test FileUploadError handling."""
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 400
|
||||
mock_response.json.return_value = {"message": "File upload failed"}
|
||||
mock_request.return_value = mock_response
|
||||
|
||||
with self.assertRaises(FileUploadError) as context:
|
||||
self.client._send_request_with_files("POST", "/upload", {}, {})
|
||||
|
||||
self.assertEqual(str(context.exception), "File upload failed")
|
||||
self.assertEqual(context.exception.status_code, 400)
|
||||
|
||||
|
||||
class TestParameterValidation(unittest.TestCase):
|
||||
"""Test cases for parameter validation."""
|
||||
|
||||
def setUp(self):
|
||||
self.client = DifyClient(api_key="test_api_key", enable_logging=False)
|
||||
|
||||
def test_empty_string_validation(self):
|
||||
"""Test validation of empty strings."""
|
||||
with self.assertRaises(ValidationError):
|
||||
self.client._validate_params(empty_string="")
|
||||
|
||||
def test_whitespace_only_string_validation(self):
|
||||
"""Test validation of whitespace-only strings."""
|
||||
with self.assertRaises(ValidationError):
|
||||
self.client._validate_params(whitespace_string=" ")
|
||||
|
||||
def test_long_string_validation(self):
|
||||
"""Test validation of overly long strings."""
|
||||
long_string = "a" * 10001 # Exceeds 10000 character limit
|
||||
with self.assertRaises(ValidationError):
|
||||
self.client._validate_params(long_string=long_string)
|
||||
|
||||
def test_large_list_validation(self):
|
||||
"""Test validation of overly large lists."""
|
||||
large_list = list(range(1001)) # Exceeds 1000 item limit
|
||||
with self.assertRaises(ValidationError):
|
||||
self.client._validate_params(large_list=large_list)
|
||||
|
||||
def test_large_dict_validation(self):
|
||||
"""Test validation of overly large dictionaries."""
|
||||
large_dict = {f"key_{i}": i for i in range(101)} # Exceeds 100 item limit
|
||||
with self.assertRaises(ValidationError):
|
||||
self.client._validate_params(large_dict=large_dict)
|
||||
|
||||
def test_valid_parameters_pass(self):
|
||||
"""Test that valid parameters pass validation."""
|
||||
# Should not raise any exception
|
||||
self.client._validate_params(
|
||||
valid_string="Hello, World!",
|
||||
valid_list=[1, 2, 3],
|
||||
valid_dict={"key": "value"},
|
||||
none_value=None,
|
||||
)
|
||||
|
||||
def test_message_feedback_validation(self):
|
||||
"""Test validation in message_feedback method."""
|
||||
with self.assertRaises(ValidationError):
|
||||
self.client.message_feedback("msg_id", "invalid_rating", "user")
|
||||
|
||||
def test_completion_message_validation(self):
|
||||
"""Test validation in create_completion_message method."""
|
||||
from dify_client.client import CompletionClient
|
||||
|
||||
client = CompletionClient("test_api_key")
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
client.create_completion_message(
|
||||
inputs="not_a_dict", # Should be a dict
|
||||
response_mode="invalid_mode", # Should be 'blocking' or 'streaming'
|
||||
user="test_user",
|
||||
)
|
||||
|
||||
def test_chat_message_validation(self):
|
||||
"""Test validation in create_chat_message method."""
|
||||
from dify_client.client import ChatClient
|
||||
|
||||
client = ChatClient("test_api_key")
|
||||
|
||||
with self.assertRaises(ValidationError):
|
||||
client.create_chat_message(
|
||||
inputs="not_a_dict", # Should be a dict
|
||||
query="", # Should not be empty
|
||||
user="test_user",
|
||||
response_mode="invalid_mode", # Should be 'blocking' or 'streaming'
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
307
dify/sdks/python-client/uv.lock
generated
Normal file
307
dify/sdks/python-client/uv.lock
generated
Normal file
@@ -0,0 +1,307 @@
|
||||
version = 1
|
||||
revision = 3
|
||||
requires-python = ">=3.10"
|
||||
|
||||
[[package]]
|
||||
name = "aiofiles"
|
||||
version = "25.1.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/41/c3/534eac40372d8ee36ef40df62ec129bee4fdb5ad9706e58a29be53b2c970/aiofiles-25.1.0.tar.gz", hash = "sha256:a8d728f0a29de45dc521f18f07297428d56992a742f0cd2701ba86e44d23d5b2", size = 46354, upload-time = "2025-10-09T20:51:04.358Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/bc/8a/340a1555ae33d7354dbca4faa54948d76d89a27ceef032c8c3bc661d003e/aiofiles-25.1.0-py3-none-any.whl", hash = "sha256:abe311e527c862958650f9438e859c1fa7568a141b22abcd015e120e86a85695", size = 14668, upload-time = "2025-10-09T20:51:03.174Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyio"
|
||||
version = "4.11.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "exceptiongroup", marker = "python_full_version < '3.11'" },
|
||||
{ name = "idna" },
|
||||
{ name = "sniffio" },
|
||||
{ name = "typing-extensions", marker = "python_full_version < '3.13'" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/c6/78/7d432127c41b50bccba979505f272c16cbcadcc33645d5fa3a738110ae75/anyio-4.11.0.tar.gz", hash = "sha256:82a8d0b81e318cc5ce71a5f1f8b5c4e63619620b63141ef8c995fa0db95a57c4", size = 219094, upload-time = "2025-09-23T09:19:12.58Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/15/b3/9b1a8074496371342ec1e796a96f99c82c945a339cd81a8e73de28b4cf9e/anyio-4.11.0-py3-none-any.whl", hash = "sha256:0287e96f4d26d4149305414d4e3bc32f0dcd0862365a4bddea19d7a1ec38c4fc", size = 109097, upload-time = "2025-09-23T09:19:10.601Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "backports-asyncio-runner"
|
||||
version = "1.2.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/8e/ff/70dca7d7cb1cbc0edb2c6cc0c38b65cba36cccc491eca64cabd5fe7f8670/backports_asyncio_runner-1.2.0.tar.gz", hash = "sha256:a5aa7b2b7d8f8bfcaa2b57313f70792df84e32a2a746f585213373f900b42162", size = 69893, upload-time = "2025-07-02T02:27:15.685Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/a0/59/76ab57e3fe74484f48a53f8e337171b4a2349e506eabe136d7e01d059086/backports_asyncio_runner-1.2.0-py3-none-any.whl", hash = "sha256:0da0a936a8aeb554eccb426dc55af3ba63bcdc69fa1a600b5bb305413a4477b5", size = 12313, upload-time = "2025-07-02T02:27:14.263Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "certifi"
|
||||
version = "2025.10.5"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/4c/5b/b6ce21586237c77ce67d01dc5507039d444b630dd76611bbca2d8e5dcd91/certifi-2025.10.5.tar.gz", hash = "sha256:47c09d31ccf2acf0be3f701ea53595ee7e0b8fa08801c6624be771df09ae7b43", size = 164519, upload-time = "2025-10-05T04:12:15.808Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/e4/37/af0d2ef3967ac0d6113837b44a4f0bfe1328c2b9763bd5b1744520e5cfed/certifi-2025.10.5-py3-none-any.whl", hash = "sha256:0f212c2744a9bb6de0c56639a6f68afe01ecd92d91f14ae897c4fe7bbeeef0de", size = 163286, upload-time = "2025-10-05T04:12:14.03Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorama"
|
||||
version = "0.4.6"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dify-client"
|
||||
version = "0.1.12"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "aiofiles" },
|
||||
{ name = "httpx", extra = ["http2"] },
|
||||
]
|
||||
|
||||
[package.optional-dependencies]
|
||||
dev = [
|
||||
{ name = "pytest" },
|
||||
{ name = "pytest-asyncio" },
|
||||
]
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "aiofiles", specifier = ">=23.0.0" },
|
||||
{ name = "httpx", extras = ["http2"], specifier = ">=0.27.0" },
|
||||
{ name = "pytest", marker = "extra == 'dev'", specifier = ">=7.0.0" },
|
||||
{ name = "pytest-asyncio", marker = "extra == 'dev'", specifier = ">=0.21.0" },
|
||||
]
|
||||
provides-extras = ["dev"]
|
||||
|
||||
[[package]]
|
||||
name = "exceptiongroup"
|
||||
version = "1.3.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "typing-extensions", marker = "python_full_version < '3.13'" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/0b/9f/a65090624ecf468cdca03533906e7c69ed7588582240cfe7cc9e770b50eb/exceptiongroup-1.3.0.tar.gz", hash = "sha256:b241f5885f560bc56a59ee63ca4c6a8bfa46ae4ad651af316d4e81817bb9fd88", size = 29749, upload-time = "2025-05-10T17:42:51.123Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/36/f4/c6e662dade71f56cd2f3735141b265c3c79293c109549c1e6933b0651ffc/exceptiongroup-1.3.0-py3-none-any.whl", hash = "sha256:4d111e6e0c13d0644cad6ddaa7ed0261a0b36971f6d23e7ec9b4b9097da78a10", size = 16674, upload-time = "2025-05-10T17:42:49.33Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "h11"
|
||||
version = "0.16.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/01/ee/02a2c011bdab74c6fb3c75474d40b3052059d95df7e73351460c8588d963/h11-0.16.0.tar.gz", hash = "sha256:4e35b956cf45792e4caa5885e69fba00bdbc6ffafbfa020300e549b208ee5ff1", size = 101250, upload-time = "2025-04-24T03:35:25.427Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/04/4b/29cac41a4d98d144bf5f6d33995617b185d14b22401f75ca86f384e87ff1/h11-0.16.0-py3-none-any.whl", hash = "sha256:63cf8bbe7522de3bf65932fda1d9c2772064ffb3dae62d55932da54b31cb6c86", size = 37515, upload-time = "2025-04-24T03:35:24.344Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "h2"
|
||||
version = "4.3.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "hpack" },
|
||||
{ name = "hyperframe" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/1d/17/afa56379f94ad0fe8defd37d6eb3f89a25404ffc71d4d848893d270325fc/h2-4.3.0.tar.gz", hash = "sha256:6c59efe4323fa18b47a632221a1888bd7fde6249819beda254aeca909f221bf1", size = 2152026, upload-time = "2025-08-23T18:12:19.778Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/69/b2/119f6e6dcbd96f9069ce9a2665e0146588dc9f88f29549711853645e736a/h2-4.3.0-py3-none-any.whl", hash = "sha256:c438f029a25f7945c69e0ccf0fb951dc3f73a5f6412981daee861431b70e2bdd", size = 61779, upload-time = "2025-08-23T18:12:17.779Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hpack"
|
||||
version = "4.1.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/2c/48/71de9ed269fdae9c8057e5a4c0aa7402e8bb16f2c6e90b3aa53327b113f8/hpack-4.1.0.tar.gz", hash = "sha256:ec5eca154f7056aa06f196a557655c5b009b382873ac8d1e66e79e87535f1dca", size = 51276, upload-time = "2025-01-22T21:44:58.347Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/07/c6/80c95b1b2b94682a72cbdbfb85b81ae2daffa4291fbfa1b1464502ede10d/hpack-4.1.0-py3-none-any.whl", hash = "sha256:157ac792668d995c657d93111f46b4535ed114f0c9c8d672271bbec7eae1b496", size = 34357, upload-time = "2025-01-22T21:44:56.92Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "httpcore"
|
||||
version = "1.0.9"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "certifi" },
|
||||
{ name = "h11" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/06/94/82699a10bca87a5556c9c59b5963f2d039dbd239f25bc2a63907a05a14cb/httpcore-1.0.9.tar.gz", hash = "sha256:6e34463af53fd2ab5d807f399a9b45ea31c3dfa2276f15a2c3f00afff6e176e8", size = 85484, upload-time = "2025-04-24T22:06:22.219Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/7e/f5/f66802a942d491edb555dd61e3a9961140fd64c90bce1eafd741609d334d/httpcore-1.0.9-py3-none-any.whl", hash = "sha256:2d400746a40668fc9dec9810239072b40b4484b640a8c38fd654a024c7a1bf55", size = 78784, upload-time = "2025-04-24T22:06:20.566Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "httpx"
|
||||
version = "0.28.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "anyio" },
|
||||
{ name = "certifi" },
|
||||
{ name = "httpcore" },
|
||||
{ name = "idna" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/b1/df/48c586a5fe32a0f01324ee087459e112ebb7224f646c0b5023f5e79e9956/httpx-0.28.1.tar.gz", hash = "sha256:75e98c5f16b0f35b567856f597f06ff2270a374470a5c2392242528e3e3e42fc", size = 141406, upload-time = "2024-12-06T15:37:23.222Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/2a/39/e50c7c3a983047577ee07d2a9e53faf5a69493943ec3f6a384bdc792deb2/httpx-0.28.1-py3-none-any.whl", hash = "sha256:d909fcccc110f8c7faf814ca82a9a4d816bc5a6dbfea25d6591d6985b8ba59ad", size = 73517, upload-time = "2024-12-06T15:37:21.509Z" },
|
||||
]
|
||||
|
||||
[package.optional-dependencies]
|
||||
http2 = [
|
||||
{ name = "h2" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hyperframe"
|
||||
version = "6.1.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/02/e7/94f8232d4a74cc99514c13a9f995811485a6903d48e5d952771ef6322e30/hyperframe-6.1.0.tar.gz", hash = "sha256:f630908a00854a7adeabd6382b43923a4c4cd4b821fcb527e6ab9e15382a3b08", size = 26566, upload-time = "2025-01-22T21:41:49.302Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/48/30/47d0bf6072f7252e6521f3447ccfa40b421b6824517f82854703d0f5a98b/hyperframe-6.1.0-py3-none-any.whl", hash = "sha256:b03380493a519fce58ea5af42e4a42317bf9bd425596f7a0835ffce80f1a42e5", size = 13007, upload-time = "2025-01-22T21:41:47.295Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "3.10"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/f1/70/7703c29685631f5a7590aa73f1f1d3fa9a380e654b86af429e0934a32f7d/idna-3.10.tar.gz", hash = "sha256:12f65c9b470abda6dc35cf8e63cc574b1c52b11df2c86030af0ac09b01b13ea9", size = 190490, upload-time = "2024-09-15T18:07:39.745Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/76/c6/c88e154df9c4e1a2a66ccf0005a88dfb2650c1dffb6f5ce603dfbd452ce3/idna-3.10-py3-none-any.whl", hash = "sha256:946d195a0d259cbba61165e88e65941f16e9b36ea6ddb97f00452bae8b1287d3", size = 70442, upload-time = "2024-09-15T18:07:37.964Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iniconfig"
|
||||
version = "2.1.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/f2/97/ebf4da567aa6827c909642694d71c9fcf53e5b504f2d96afea02718862f3/iniconfig-2.1.0.tar.gz", hash = "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7", size = 4793, upload-time = "2025-03-19T20:09:59.721Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/2c/e1/e6716421ea10d38022b952c159d5161ca1193197fb744506875fbb87ea7b/iniconfig-2.1.0-py3-none-any.whl", hash = "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760", size = 6050, upload-time = "2025-03-19T20:10:01.071Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "packaging"
|
||||
version = "25.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/a1/d4/1fc4078c65507b51b96ca8f8c3ba19e6a61c8253c72794544580a7b6c24d/packaging-25.0.tar.gz", hash = "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f", size = 165727, upload-time = "2025-04-19T11:48:59.673Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/20/12/38679034af332785aac8774540895e234f4d07f7545804097de4b666afd8/packaging-25.0-py3-none-any.whl", hash = "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", size = 66469, upload-time = "2025-04-19T11:48:57.875Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pluggy"
|
||||
version = "1.6.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412, upload-time = "2025-05-15T12:30:07.975Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pygments"
|
||||
version = "2.19.2"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/b0/77/a5b8c569bf593b0140bde72ea885a803b82086995367bf2037de0159d924/pygments-2.19.2.tar.gz", hash = "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887", size = 4968631, upload-time = "2025-06-21T13:39:12.283Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/c7/21/705964c7812476f378728bdf590ca4b771ec72385c533964653c68e86bdc/pygments-2.19.2-py3-none-any.whl", hash = "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b", size = 1225217, upload-time = "2025-06-21T13:39:07.939Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pytest"
|
||||
version = "8.4.2"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "colorama", marker = "sys_platform == 'win32'" },
|
||||
{ name = "exceptiongroup", marker = "python_full_version < '3.11'" },
|
||||
{ name = "iniconfig" },
|
||||
{ name = "packaging" },
|
||||
{ name = "pluggy" },
|
||||
{ name = "pygments" },
|
||||
{ name = "tomli", marker = "python_full_version < '3.11'" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/a3/5c/00a0e072241553e1a7496d638deababa67c5058571567b92a7eaa258397c/pytest-8.4.2.tar.gz", hash = "sha256:86c0d0b93306b961d58d62a4db4879f27fe25513d4b969df351abdddb3c30e01", size = 1519618, upload-time = "2025-09-04T14:34:22.711Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/a8/a4/20da314d277121d6534b3a980b29035dcd51e6744bd79075a6ce8fa4eb8d/pytest-8.4.2-py3-none-any.whl", hash = "sha256:872f880de3fc3a5bdc88a11b39c9710c3497a547cfa9320bc3c5e62fbf272e79", size = 365750, upload-time = "2025-09-04T14:34:20.226Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pytest-asyncio"
|
||||
version = "1.2.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "backports-asyncio-runner", marker = "python_full_version < '3.11'" },
|
||||
{ name = "pytest" },
|
||||
{ name = "typing-extensions", marker = "python_full_version < '3.13'" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/42/86/9e3c5f48f7b7b638b216e4b9e645f54d199d7abbbab7a64a13b4e12ba10f/pytest_asyncio-1.2.0.tar.gz", hash = "sha256:c609a64a2a8768462d0c99811ddb8bd2583c33fd33cf7f21af1c142e824ffb57", size = 50119, upload-time = "2025-09-12T07:33:53.816Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/04/93/2fa34714b7a4ae72f2f8dad66ba17dd9a2c793220719e736dda28b7aec27/pytest_asyncio-1.2.0-py3-none-any.whl", hash = "sha256:8e17ae5e46d8e7efe51ab6494dd2010f4ca8dae51652aa3c8d55acf50bfb2e99", size = 15095, upload-time = "2025-09-12T07:33:52.639Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sniffio"
|
||||
version = "1.3.1"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/a2/87/a6771e1546d97e7e041b6ae58d80074f81b7d5121207425c964ddf5cfdbd/sniffio-1.3.1.tar.gz", hash = "sha256:f4324edc670a0f49750a81b895f35c3adb843cca46f0530f79fc1babb23789dc", size = 20372, upload-time = "2024-02-25T23:20:04.057Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/e9/44/75a9c9421471a6c4805dbf2356f7c181a29c1879239abab1ea2cc8f38b40/sniffio-1.3.1-py3-none-any.whl", hash = "sha256:2f6da418d1f1e0fddd844478f41680e794e6051915791a034ff65e5f100525a2", size = 10235, upload-time = "2024-02-25T23:20:01.196Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tomli"
|
||||
version = "2.3.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/52/ed/3f73f72945444548f33eba9a87fc7a6e969915e7b1acc8260b30e1f76a2f/tomli-2.3.0.tar.gz", hash = "sha256:64be704a875d2a59753d80ee8a533c3fe183e3f06807ff7dc2232938ccb01549", size = 17392, upload-time = "2025-10-08T22:01:47.119Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/b3/2e/299f62b401438d5fe1624119c723f5d877acc86a4c2492da405626665f12/tomli-2.3.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:88bd15eb972f3664f5ed4b57c1634a97153b4bac4479dcb6a495f41921eb7f45", size = 153236, upload-time = "2025-10-08T22:01:00.137Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/86/7f/d8fffe6a7aefdb61bced88fcb5e280cfd71e08939da5894161bd71bea022/tomli-2.3.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:883b1c0d6398a6a9d29b508c331fa56adbcdff647f6ace4dfca0f50e90dfd0ba", size = 148084, upload-time = "2025-10-08T22:01:01.63Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/47/5c/24935fb6a2ee63e86d80e4d3b58b222dafaf438c416752c8b58537c8b89a/tomli-2.3.0-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:d1381caf13ab9f300e30dd8feadb3de072aeb86f1d34a8569453ff32a7dea4bf", size = 234832, upload-time = "2025-10-08T22:01:02.543Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/89/da/75dfd804fc11e6612846758a23f13271b76d577e299592b4371a4ca4cd09/tomli-2.3.0-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:a0e285d2649b78c0d9027570d4da3425bdb49830a6156121360b3f8511ea3441", size = 242052, upload-time = "2025-10-08T22:01:03.836Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/70/8c/f48ac899f7b3ca7eb13af73bacbc93aec37f9c954df3c08ad96991c8c373/tomli-2.3.0-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:0a154a9ae14bfcf5d8917a59b51ffd5a3ac1fd149b71b47a3a104ca4edcfa845", size = 239555, upload-time = "2025-10-08T22:01:04.834Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/ba/28/72f8afd73f1d0e7829bfc093f4cb98ce0a40ffc0cc997009ee1ed94ba705/tomli-2.3.0-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:74bf8464ff93e413514fefd2be591c3b0b23231a77f901db1eb30d6f712fc42c", size = 245128, upload-time = "2025-10-08T22:01:05.84Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/b6/eb/a7679c8ac85208706d27436e8d421dfa39d4c914dcf5fa8083a9305f58d9/tomli-2.3.0-cp311-cp311-win32.whl", hash = "sha256:00b5f5d95bbfc7d12f91ad8c593a1659b6387b43f054104cda404be6bda62456", size = 96445, upload-time = "2025-10-08T22:01:06.896Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/0a/fe/3d3420c4cb1ad9cb462fb52967080575f15898da97e21cb6f1361d505383/tomli-2.3.0-cp311-cp311-win_amd64.whl", hash = "sha256:4dc4ce8483a5d429ab602f111a93a6ab1ed425eae3122032db7e9acf449451be", size = 107165, upload-time = "2025-10-08T22:01:08.107Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/ff/b7/40f36368fcabc518bb11c8f06379a0fd631985046c038aca08c6d6a43c6e/tomli-2.3.0-cp312-cp312-macosx_10_13_x86_64.whl", hash = "sha256:d7d86942e56ded512a594786a5ba0a5e521d02529b3826e7761a05138341a2ac", size = 154891, upload-time = "2025-10-08T22:01:09.082Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f9/3f/d9dd692199e3b3aab2e4e4dd948abd0f790d9ded8cd10cbaae276a898434/tomli-2.3.0-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:73ee0b47d4dad1c5e996e3cd33b8a76a50167ae5f96a2607cbe8cc773506ab22", size = 148796, upload-time = "2025-10-08T22:01:10.266Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/60/83/59bff4996c2cf9f9387a0f5a3394629c7efa5ef16142076a23a90f1955fa/tomli-2.3.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:792262b94d5d0a466afb5bc63c7daa9d75520110971ee269152083270998316f", size = 242121, upload-time = "2025-10-08T22:01:11.332Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/45/e5/7c5119ff39de8693d6baab6c0b6dcb556d192c165596e9fc231ea1052041/tomli-2.3.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4f195fe57ecceac95a66a75ac24d9d5fbc98ef0962e09b2eddec5d39375aae52", size = 250070, upload-time = "2025-10-08T22:01:12.498Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/45/12/ad5126d3a278f27e6701abde51d342aa78d06e27ce2bb596a01f7709a5a2/tomli-2.3.0-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:e31d432427dcbf4d86958c184b9bfd1e96b5b71f8eb17e6d02531f434fd335b8", size = 245859, upload-time = "2025-10-08T22:01:13.551Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/fb/a1/4d6865da6a71c603cfe6ad0e6556c73c76548557a8d658f9e3b142df245f/tomli-2.3.0-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:7b0882799624980785240ab732537fcfc372601015c00f7fc367c55308c186f6", size = 250296, upload-time = "2025-10-08T22:01:14.614Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/a0/b7/a7a7042715d55c9ba6e8b196d65d2cb662578b4d8cd17d882d45322b0d78/tomli-2.3.0-cp312-cp312-win32.whl", hash = "sha256:ff72b71b5d10d22ecb084d345fc26f42b5143c5533db5e2eaba7d2d335358876", size = 97124, upload-time = "2025-10-08T22:01:15.629Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/06/1e/f22f100db15a68b520664eb3328fb0ae4e90530887928558112c8d1f4515/tomli-2.3.0-cp312-cp312-win_amd64.whl", hash = "sha256:1cb4ed918939151a03f33d4242ccd0aa5f11b3547d0cf30f7c74a408a5b99878", size = 107698, upload-time = "2025-10-08T22:01:16.51Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/89/48/06ee6eabe4fdd9ecd48bf488f4ac783844fd777f547b8d1b61c11939974e/tomli-2.3.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:5192f562738228945d7b13d4930baffda67b69425a7f0da96d360b0a3888136b", size = 154819, upload-time = "2025-10-08T22:01:17.964Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f1/01/88793757d54d8937015c75dcdfb673c65471945f6be98e6a0410fba167ed/tomli-2.3.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:be71c93a63d738597996be9528f4abe628d1adf5e6eb11607bc8fe1a510b5dae", size = 148766, upload-time = "2025-10-08T22:01:18.959Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/42/17/5e2c956f0144b812e7e107f94f1cc54af734eb17b5191c0bbfb72de5e93e/tomli-2.3.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c4665508bcbac83a31ff8ab08f424b665200c0e1e645d2bd9ab3d3e557b6185b", size = 240771, upload-time = "2025-10-08T22:01:20.106Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/d5/f4/0fbd014909748706c01d16824eadb0307115f9562a15cbb012cd9b3512c5/tomli-2.3.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:4021923f97266babc6ccab9f5068642a0095faa0a51a246a6a02fccbb3514eaf", size = 248586, upload-time = "2025-10-08T22:01:21.164Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/30/77/fed85e114bde5e81ecf9bc5da0cc69f2914b38f4708c80ae67d0c10180c5/tomli-2.3.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:a4ea38c40145a357d513bffad0ed869f13c1773716cf71ccaa83b0fa0cc4e42f", size = 244792, upload-time = "2025-10-08T22:01:22.417Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/55/92/afed3d497f7c186dc71e6ee6d4fcb0acfa5f7d0a1a2878f8beae379ae0cc/tomli-2.3.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:ad805ea85eda330dbad64c7ea7a4556259665bdf9d2672f5dccc740eb9d3ca05", size = 248909, upload-time = "2025-10-08T22:01:23.859Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/f8/84/ef50c51b5a9472e7265ce1ffc7f24cd4023d289e109f669bdb1553f6a7c2/tomli-2.3.0-cp313-cp313-win32.whl", hash = "sha256:97d5eec30149fd3294270e889b4234023f2c69747e555a27bd708828353ab606", size = 96946, upload-time = "2025-10-08T22:01:24.893Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/b2/b7/718cd1da0884f281f95ccfa3a6cc572d30053cba64603f79d431d3c9b61b/tomli-2.3.0-cp313-cp313-win_amd64.whl", hash = "sha256:0c95ca56fbe89e065c6ead5b593ee64b84a26fca063b5d71a1122bf26e533999", size = 107705, upload-time = "2025-10-08T22:01:26.153Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/19/94/aeafa14a52e16163008060506fcb6aa1949d13548d13752171a755c65611/tomli-2.3.0-cp314-cp314-macosx_10_13_x86_64.whl", hash = "sha256:cebc6fe843e0733ee827a282aca4999b596241195f43b4cc371d64fc6639da9e", size = 154244, upload-time = "2025-10-08T22:01:27.06Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/db/e4/1e58409aa78eefa47ccd19779fc6f36787edbe7d4cd330eeeedb33a4515b/tomli-2.3.0-cp314-cp314-macosx_11_0_arm64.whl", hash = "sha256:4c2ef0244c75aba9355561272009d934953817c49f47d768070c3c94355c2aa3", size = 148637, upload-time = "2025-10-08T22:01:28.059Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/26/b6/d1eccb62f665e44359226811064596dd6a366ea1f985839c566cd61525ae/tomli-2.3.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:c22a8bf253bacc0cf11f35ad9808b6cb75ada2631c2d97c971122583b129afbc", size = 241925, upload-time = "2025-10-08T22:01:29.066Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/70/91/7cdab9a03e6d3d2bb11beae108da5bdc1c34bdeb06e21163482544ddcc90/tomli-2.3.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:0eea8cc5c5e9f89c9b90c4896a8deefc74f518db5927d0e0e8d4a80953d774d0", size = 249045, upload-time = "2025-10-08T22:01:31.98Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/15/1b/8c26874ed1f6e4f1fcfeb868db8a794cbe9f227299402db58cfcc858766c/tomli-2.3.0-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:b74a0e59ec5d15127acdabd75ea17726ac4c5178ae51b85bfe39c4f8a278e879", size = 245835, upload-time = "2025-10-08T22:01:32.989Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/fd/42/8e3c6a9a4b1a1360c1a2a39f0b972cef2cc9ebd56025168c4137192a9321/tomli-2.3.0-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:b5870b50c9db823c595983571d1296a6ff3e1b88f734a4c8f6fc6188397de005", size = 253109, upload-time = "2025-10-08T22:01:34.052Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/22/0c/b4da635000a71b5f80130937eeac12e686eefb376b8dee113b4a582bba42/tomli-2.3.0-cp314-cp314-win32.whl", hash = "sha256:feb0dacc61170ed7ab602d3d972a58f14ee3ee60494292d384649a3dc38ef463", size = 97930, upload-time = "2025-10-08T22:01:35.082Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/b9/74/cb1abc870a418ae99cd5c9547d6bce30701a954e0e721821df483ef7223c/tomli-2.3.0-cp314-cp314-win_amd64.whl", hash = "sha256:b273fcbd7fc64dc3600c098e39136522650c49bca95df2d11cf3b626422392c8", size = 107964, upload-time = "2025-10-08T22:01:36.057Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/54/78/5c46fff6432a712af9f792944f4fcd7067d8823157949f4e40c56b8b3c83/tomli-2.3.0-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:940d56ee0410fa17ee1f12b817b37a4d4e4dc4d27340863cc67236c74f582e77", size = 163065, upload-time = "2025-10-08T22:01:37.27Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/39/67/f85d9bd23182f45eca8939cd2bc7050e1f90c41f4a2ecbbd5963a1d1c486/tomli-2.3.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:f85209946d1fe94416debbb88d00eb92ce9cd5266775424ff81bc959e001acaf", size = 159088, upload-time = "2025-10-08T22:01:38.235Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/26/5a/4b546a0405b9cc0659b399f12b6adb750757baf04250b148d3c5059fc4eb/tomli-2.3.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:a56212bdcce682e56b0aaf79e869ba5d15a6163f88d5451cbde388d48b13f530", size = 268193, upload-time = "2025-10-08T22:01:39.712Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/42/4f/2c12a72ae22cf7b59a7fe75b3465b7aba40ea9145d026ba41cb382075b0e/tomli-2.3.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:c5f3ffd1e098dfc032d4d3af5c0ac64f6d286d98bc148698356847b80fa4de1b", size = 275488, upload-time = "2025-10-08T22:01:40.773Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/92/04/a038d65dbe160c3aa5a624e93ad98111090f6804027d474ba9c37c8ae186/tomli-2.3.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:5e01decd096b1530d97d5d85cb4dff4af2d8347bd35686654a004f8dea20fc67", size = 272669, upload-time = "2025-10-08T22:01:41.824Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/be/2f/8b7c60a9d1612a7cbc39ffcca4f21a73bf368a80fc25bccf8253e2563267/tomli-2.3.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:8a35dd0e643bb2610f156cca8db95d213a90015c11fee76c946aa62b7ae7e02f", size = 279709, upload-time = "2025-10-08T22:01:43.177Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/7e/46/cc36c679f09f27ded940281c38607716c86cf8ba4a518d524e349c8b4874/tomli-2.3.0-cp314-cp314t-win32.whl", hash = "sha256:a1f7f282fe248311650081faafa5f4732bdbfef5d45fe3f2e702fbc6f2d496e0", size = 107563, upload-time = "2025-10-08T22:01:44.233Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/84/ff/426ca8683cf7b753614480484f6437f568fd2fda2edbdf57a2d3d8b27a0b/tomli-2.3.0-cp314-cp314t-win_amd64.whl", hash = "sha256:70a251f8d4ba2d9ac2542eecf008b3c8a9fc5c3f9f02c56a9d7952612be2fdba", size = 119756, upload-time = "2025-10-08T22:01:45.234Z" },
|
||||
{ url = "https://files.pythonhosted.org/packages/77/b8/0135fadc89e73be292b473cb820b4f5a08197779206b33191e801feeae40/tomli-2.3.0-py3-none-any.whl", hash = "sha256:e95b1af3c5b07d9e643909b5abbec77cd9f1217e6d0bca72b0234736b9fb1f1b", size = 14408, upload-time = "2025-10-08T22:01:46.04Z" },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typing-extensions"
|
||||
version = "4.15.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/72/94/1a15dd82efb362ac84269196e94cf00f187f7ed21c242792a923cdb1c61f/typing_extensions-4.15.0.tar.gz", hash = "sha256:0cea48d173cc12fa28ecabc3b837ea3cf6f38c6d1136f85cbaaf598984861466", size = 109391, upload-time = "2025-08-25T13:49:26.313Z" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/18/67/36e9267722cc04a6b9f15c7f3441c2363321a3ea07da7ae0c0707beb2a9c/typing_extensions-4.15.0-py3-none-any.whl", hash = "sha256:f0fa19c6845758ab08074a0cfa8b7aecb71c999ca73d62883bc25cc018c4e548", size = 44614, upload-time = "2025-08-25T13:49:24.86Z" },
|
||||
]
|
||||
Reference in New Issue
Block a user