|
|
|
@ -2,16 +2,30 @@
|
|
|
|
|
#
|
|
|
|
|
# 文档在 https://www.showdoc.cc/127895880302646?page_id=1077512172693249
|
|
|
|
|
#
|
|
|
|
|
require 'faraday'
|
|
|
|
|
|
|
|
|
|
class GitService
|
|
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
|
|
['add_repository', 'fork_repository', 'delete_repository', 'file_tree', 'update_file', 'file_content', 'commits', 'add_tree', 'delete_file'].each do |method|
|
|
|
|
|
['add_repository', 'fork_repository', 'delete_repository', 'file_tree', 'update_file', 'file_content', 'commits',
|
|
|
|
|
'add_tree', 'delete_file', 'update_file_base64'].each do |method|
|
|
|
|
|
define_method method do |params|
|
|
|
|
|
post(method, params)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def make_new_multipar_file(full_file_path)
|
|
|
|
|
Faraday::FilePart.new(full_file_path, 'application/octet-stream')
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
#上传二进制文件
|
|
|
|
|
#参数构造形式
|
|
|
|
|
# {a: 'a', file: make_new_multipar_file('1.txt') }
|
|
|
|
|
def update_file_binary(params)
|
|
|
|
|
post_form('update_file', params)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def root_url
|
|
|
|
@ -24,6 +38,19 @@ class GitService
|
|
|
|
|
Rails.logger
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post_form(action,params)
|
|
|
|
|
conn = Faraday.new(root_url) do |f|
|
|
|
|
|
f.request :multipart
|
|
|
|
|
f.request :url_encoded
|
|
|
|
|
f.adapter :net_http
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
resp = conn.post("/api/#{action}", params)
|
|
|
|
|
|
|
|
|
|
body = resp.body
|
|
|
|
|
parse_return(body)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def post(action, params)
|
|
|
|
|
uri = URI.parse("#{root_url}/api/#{action}")
|
|
|
|
|
https = Net::HTTP.new(uri.host, uri.port)
|
|
|
|
@ -32,6 +59,11 @@ class GitService
|
|
|
|
|
req.body = params.to_json
|
|
|
|
|
res = https.request(req)
|
|
|
|
|
body = res.body
|
|
|
|
|
|
|
|
|
|
parse_return(body)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def parse_return(body)
|
|
|
|
|
logger.info("--uri_exec: .....res is #{body}")
|
|
|
|
|
|
|
|
|
|
content = JSON.parse(body)
|
|
|
|
|