Skip to content

Commit 596dac9

Browse files
committed
Following ixti advice to add post_file to fix #78, still not working yet
httprb/http#293 (comment)
1 parent 123bcb1 commit 596dac9

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

bin/wechat

+1-2
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ class App < Thor
334334

335335
desc 'material_add [MEDIA_TYPE, PATH]', '永久媒体上传'
336336
def material_add(type, path)
337-
file = File.new(path)
338-
puts wechat_api.material_add(type, file)
337+
puts wechat_api.material_add(type, path)
339338
end
340339

341340
desc 'material_delete [MEDIA_ID]', '删除永久素材'

lib/wechat/api.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def material_list(type, offset, count)
9494
end
9595

9696
def material_add(type, file)
97-
post 'material/add_material', { upload: { media: file } }, params: { type: type }
97+
post_file 'material/add_material', file, params: { type: type }
9898
end
9999

100100
def material_delete(media_id)

lib/wechat/api_base.rb

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ def post(path, payload, headers = {})
3434
end
3535
end
3636

37+
def post_file(path, file, headers = {})
38+
with_access_token(headers[:params]) do |params|
39+
client.post_file path, file, headers.merge(params: params)
40+
end
41+
end
42+
3743
def with_access_token(params = {}, tries = 2)
3844
params ||= {}
3945
yield(params.merge(access_token: access_token.token))

lib/wechat/client.rb

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ def post(path, payload, post_header = {})
2525
end
2626
end
2727

28+
def post_file(path, file, post_header = {})
29+
request(path, post_header) do |url, header|
30+
params = header.delete(:params)
31+
HTTP.headers(header).post(url, params: params, form: { media: HTTP::FormData::File.new(file) }, ssl_context: ssl_context)
32+
end
33+
end
34+
2835
private
2936

3037
def request(path, header = {}, &_block)

lib/wechat/corp_api.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def material(media_id)
152152
end
153153

154154
def material_add(type, file)
155-
post 'material/add_material', { upload: { media: file } }, params: { type: type, agentid: agentid }
155+
post_file 'material/add_material', file, params: { type: type, agentid: agentid }
156156
end
157157

158158
def material_delete(media_id)

0 commit comments

Comments
 (0)