chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,71 @@
|
||||
import json
|
||||
from http import HTTPStatus
|
||||
from typing import Never
|
||||
|
||||
import httpx
|
||||
|
||||
from core.helper import ssrf_proxy
|
||||
from services.auth.errors import (
|
||||
DataSourceApiKeyAuthCredentialValidationError,
|
||||
DataSourceApiKeyAuthProviderUnavailableError,
|
||||
InvalidDataSourceApiKeyAuthCredentialsError,
|
||||
)
|
||||
from services.data_source.entities.api_key_auth import DataSourceApiKeyAuthCredentials
|
||||
|
||||
# Explicit bounded timeout for credential-validation requests so a slow or
|
||||
# hanging Firecrawl endpoint cannot block the worker indefinitely.
|
||||
_CREDENTIAL_TIMEOUT = httpx.Timeout(10.0)
|
||||
|
||||
|
||||
class FirecrawlAuth:
|
||||
def __init__(self, credentials: DataSourceApiKeyAuthCredentials):
|
||||
if credentials.auth_type != "bearer":
|
||||
raise InvalidDataSourceApiKeyAuthCredentialsError("Invalid auth type, Firecrawl auth type must be Bearer")
|
||||
self.api_key = credentials.api_key
|
||||
self.base_url = credentials.options.get("base_url", "https://api.firecrawl.dev")
|
||||
|
||||
if not self.api_key:
|
||||
raise InvalidDataSourceApiKeyAuthCredentialsError("No API key provided")
|
||||
|
||||
def validate_credentials(self) -> bool:
|
||||
headers = self._prepare_headers()
|
||||
options = {
|
||||
"url": "https://example.com",
|
||||
"includePaths": [],
|
||||
"excludePaths": [],
|
||||
"limit": 1,
|
||||
"scrapeOptions": {"onlyMainContent": True},
|
||||
}
|
||||
response = self._post_request(self._build_url("v1/crawl"), options, headers)
|
||||
if response.status_code == 200:
|
||||
return True
|
||||
else:
|
||||
self._handle_error(response)
|
||||
|
||||
def _prepare_headers(self):
|
||||
return {"Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}"}
|
||||
|
||||
def _build_url(self, path: str) -> str:
|
||||
# ensure exactly one slash between base and path, regardless of user-provided base_url
|
||||
return f"{self.base_url.rstrip('/')}/{path.lstrip('/')}"
|
||||
|
||||
def _post_request(self, url, data, headers):
|
||||
return ssrf_proxy.post(url, headers=headers, json=data, timeout=_CREDENTIAL_TIMEOUT, max_retries=0)
|
||||
|
||||
def _handle_error(self, response) -> Never:
|
||||
if (
|
||||
response.status_code == HTTPStatus.TOO_MANY_REQUESTS
|
||||
or response.status_code >= HTTPStatus.INTERNAL_SERVER_ERROR
|
||||
):
|
||||
raise DataSourceApiKeyAuthProviderUnavailableError("firecrawl", response.status_code)
|
||||
|
||||
try:
|
||||
payload = response.json()
|
||||
except json.JSONDecodeError:
|
||||
payload = {}
|
||||
if not isinstance(payload, dict):
|
||||
payload = {}
|
||||
error_message = payload.get("error") or payload.get("message") or (response.text or "Unknown error occurred")
|
||||
raise DataSourceApiKeyAuthCredentialValidationError(
|
||||
f"Failed to authorize. Status code: {response.status_code}. Error: {error_message}"
|
||||
)
|
||||
Reference in new issue
Block a user