Merge pull request #6 from Zigngit/main

Add file upload and workflow support
This commit is contained in:
Eric Guo
2025-09-09 17:19:07 +08:00
committed by GitHub
6 changed files with 56 additions and 8 deletions
+2
View File
@@ -12,3 +12,5 @@ gem "minitest", "~> 5.0"
gem "rubocop", "~> 1.72"
gem "webmock"
gem "multipart-post"
+4 -1
View File
@@ -1,7 +1,8 @@
PATH
remote: .
specs:
dify_client (0.1.1)
dify_client (0.1.2)
multipart-post (~> 2.3)
GEM
remote: https://rubygems.org/
@@ -18,6 +19,7 @@ GEM
language_server-protocol (3.17.0.4)
lint_roller (1.1.0)
minitest (5.25.4)
multipart-post (2.4.1)
parallel (1.26.3)
parser (3.3.7.1)
ast (~> 2.4.1)
@@ -57,6 +59,7 @@ PLATFORMS
DEPENDENCIES
dify_client!
minitest (~> 5.0)
multipart-post
rake (~> 13.0)
rubocop (~> 1.72)
webmock
+1 -1
View File
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]
# Uncomment to register a new dependency of your gem
# spec.add_dependency "example-gem", "~> 1.0"
spec.add_dependency "multipart-post", "~> 2.3"
# For more information and examples about making a new gem, checkout our
# guide at: https://bundler.io/guides/creating_gem.html
+47 -4
View File
@@ -2,7 +2,10 @@
require_relative "client/version"
require "net/https"
require "net/http/post/multipart"
require "json"
require "uri"
module Dify
module Client
@@ -29,9 +32,31 @@ module Dify
@api_key = new_key
end
def upload(io_obj, user, filename = "localfile", _mine_type = "text/plain")
uri = URI.parse("#{@base_url}/files/upload")
request = Net::HTTP::Post::Multipart.new(uri.path, {
"file" => UploadIO.new(io_obj, "text/plain", filename),
"user" => user
})
request["Authorization"] = "Bearer #{@api_key}"
request["User-Agent"] = "Ruby-Dify-Uploader"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.request(request)
end
def upload_file(file_path, user, filename = "localfile", mine_type = "text/plain")
fileio = File.new(file_path)
upload(fileio, user, filename, mine_type)
end
private
def _send_request(method, endpoint, data = nil, params = nil, _stream = false)
def _send_request(method, endpoint, data = nil, params = nil, _stream: false)
uri = URI.parse("#{@base_url}#{endpoint}")
http = Net::HTTP.new(uri.host, uri.port)
@@ -62,7 +87,25 @@ module Dify
response_mode: response_mode,
user: user
}
_send_request("POST", "/completion-messages", data, nil, response_mode == "streaming")
_send_request("POST", "/completion-messages", data, nil, _stream: response_mode == "streaming")
end
end
class WorkflowClient < DifyClient
def run_workflow(inputs, user, response_mode = "blocking", trace_id = nil)
data = {
inputs: inputs,
user: user,
response_mode: response_mode
}
data[:trace_id] = trace_id if trace_id
_send_request("POST", "/workflows/run", data, nil, _stream: response_mode == "streaming")
end
def get_workflow(workflow_id)
_send_request("GET", "/workflows/run/#{workflow_id}", nil, nil)
end
end
@@ -76,7 +119,7 @@ module Dify
}
data[:conversation_id] = conversation_id if conversation_id
_send_request("POST", "/chat-messages", data, nil, response_mode == "streaming")
_send_request("POST", "/chat-messages", data, nil, _stream: response_mode == "streaming")
end
def get_conversation_messages(user, conversation_id = nil, first_id = nil, limit = nil)
@@ -99,4 +142,4 @@ module Dify
end
end
end
end
end
+1 -1
View File
@@ -2,6 +2,6 @@
module Dify
module Client
VERSION = "0.1.1"
VERSION = "0.1.2"
end
end
+1 -1
View File
@@ -8,7 +8,7 @@ require "dify/client"
class DifyClientTest < Minitest::Test
def setup
@api_key = "YOUR_API_KEY"
@client = DifyClient.new(@api_key)
@client = Dify::Client::DifyClient.new(@api_key)
end
def test_update_api_key