Files
dify-plugin-sdks/python/dify_plugin/invocations/file.py
Maries c6f83a63e1 feat[0.4.2]: Tool OAuth (#179)
* chore: fix ruff issue

* feat(oauth): implement OAuth

* feat(invoke-message): refactor message handling and introduce InvokeMessage class

* feat(plugin-oauth): add credential_id and credential_type to tool parameters

* feat(plugin-oauth): add credential_id and credential_type to tool parameters

* chore: update dify_plugin version to 0.5.0b4 and clean up github.yaml

* chore: update plugin version to 0.1.2 in manifest.yaml

* feat(session): session context and tool backwards invocation credential support

* feat(oauth): session context and tool backwards invocation credential support

* feat: update README and requirements for OAuth support in version 0.4.2

* feat: add .gitignore to exclude IDE files and secret keys

* chore: apply ruff

* feat: bump version to 0.4.2b1

* feat: update GitHub plugin configuration for OAuth support and improve credential handling

* feat: update .gitignore to exclude dify plugin files and public keys

* feat: fix credential validation for GitHub API and bump version to 0.2.1

* feat: update GitHub plugin to support multiple access tokens and bump version to 0.2.5

* chore: apply ruff

* feat: add ToolProviderOAuthError for improved OAuth error handling in GitHub plugin

* chore: apply ruff

* chore: bump version to 0.4.2

* chore: update examples sdk version to 0.4.2

* fix: thread deadlock in PluginRunner when running tests without gevent monkey patching

* feat: add support for refreshing OAuth credentials in Plugin and GitHub provider

* feat: refactor OAuth credential handling to return structured OAuthCredentials object

* apply ruff

* feat: refactor OAuth credential handling to use ToolOAuthCredentials for improved structure

* feat: reorganize imports in __init__.py for improved clarity and structure

* feat: add Microsoft To Do plugin for refresh token example

* chore: apply ruff

* fix: update author in GitHub configuration and clean up Microsoft To Do schema

* chore: bump version to 0.4.2b2 in pyproject.toml

* feat: update Microsoft To Do plugin to handle OAuth token encoding and version bump

* feat:remove inelegant example

* chore: update dify_plugin version to 0.4.2

* chore: bump version to 0.4.2 in pyproject.toml

---------

Co-authored-by: Yeuoly <admin@srmxy.cn>
2025-07-23 13:49:01 +08:00

81 lines
2.3 KiB
Python

from enum import Enum
import requests
from pydantic import BaseModel, model_validator
from dify_plugin.core.entities.invocation import InvokeType
from dify_plugin.core.runtime import BackwardsInvocation
class UploadFileResponse(BaseModel):
class Type(str, Enum):
DOCUMENT = "document"
IMAGE = "image"
VIDEO = "video"
AUDIO = "audio"
@classmethod
def from_mime_type(cls, mime_type: str):
if mime_type.startswith("image/"):
return cls.IMAGE
if mime_type.startswith("video/"):
return cls.VIDEO
if mime_type.startswith("audio/"):
return cls.AUDIO
return cls.DOCUMENT
id: str
name: str
size: int
extension: str
mime_type: str
type: Type | None = None
preview_url: str | None = None
@model_validator(mode="before")
@classmethod
def validate_type(cls, d):
if "type" not in d:
d["type"] = cls.Type.from_mime_type(d.get("mime_type", ""))
return d
def to_app_parameter(self) -> dict:
return {
"upload_file_id": self.id,
"transfer_method": "local_file",
"type": self.Type.from_mime_type(self.mime_type).value,
}
class File(BackwardsInvocation[dict]):
def upload(self, filename: str, content: bytes, mimetype: str) -> UploadFileResponse:
"""
Upload a file
:param filename: file name
:param content: file content
:param mimetype: file mime type
:return: file id
"""
for response in self._backwards_invoke(
InvokeType.UploadFile,
dict,
{
"filename": filename,
"mimetype": mimetype,
},
):
url = response.get("url")
if not url:
raise Exception("upload file failed, could not get signed url")
response = requests.post(url, files={"file": (filename, content, mimetype)}) # noqa: S113
if response.status_code != 201:
raise Exception(f"upload file failed, status code: {response.status_code}, response: {response.text}")
return UploadFileResponse(**response.json())
raise Exception("upload file failed, empty response from server")