lib/lhj/command/yapi.rb in lhj-tools-0.1.73 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.74
- old
+ new
@@ -11,10 +11,13 @@
# generate model from yapi
class Yapi < Command
self.summary = '通过yapi接口生成请求'
self.description = '执行`lhj api`生成接口模型'
+ API_INTERFACE_URL = 'api/interface/get?id='.freeze
+ API_PROJECT_URL = 'api/project/get?id='.freeze
+
def self.options
[
%w[--id api的id],
%w[--model-pre 模型的前缀],
%w[--model-name 模型的名称],
@@ -37,35 +40,38 @@
@config_id = ''
@config_model_pre = 'ML'
@config_model_name = 'Result'
@model_default_suffix = 'Model'
@type_trans = {}
- @http_url = ''
+ @base_url = 'http://yapi.xx.com'
super
end
def handle
process(yml_file) if File.exist?(yml_file)
end
def process(config_file)
load_config(config_file)
- res_body = req_api_model
+ res_body = get_interface_api_model
unless res_body['errcode'].to_i.zero?
puts '请执行 lhj yapi-init 初始化配置'.red
return
end
return unless res_body && !res_body.empty?
+ # get project info
+ project_id = res_body['data']['project_id']
+ project_info = get_project_info(project_id)
# 1.print request result
result_model_name = print_res_body_model(res_body)
# 2.print request json body
param_model_name = print_req_body_model(res_body)
# 3.print request param
print_req_query(res_body['data'])
# 4.print request method
- service_code = print_http_method(res_body['data'], result_model_name, param_model_name)
+ service_code = print_http_method(res_body['data'], project_info, result_model_name, param_model_name)
# 5.save to file
file_map = save_to_file(service_code) if @save
# 6.push to git
push_to_git if @sync
# 7.notify robot
@@ -126,24 +132,28 @@
template = Lhj::ErbTemplateHelper.load('oc_code_notify')
output = Lhj::ErbTemplateHelper.render(template, template_vars, '-')
Lhj::Dingtalk.post_message_robot(robot_url, '生成代码', output)
end
- def url_str
- "#{@http_url}#{api_id}"
+ def interface_url_str
+ "#{@base_url}/#{API_INTERFACE_URL}#{api_id}"
end
+ def project_url_str(project_id)
+ "#{@base_url}/#{API_PROJECT_URL}#{project_id}"
+ end
+
def yml_file
File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
end
def load_config(file)
config = YAML.load_file(file)
config.each do |k, v|
@http_headers << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
end
- @http_url = config['url']
+ @base_url = config['base_url']
@config_id = config['id']
@config_model_pre = config['model_pre']
@config_model_suffix = config['model_suffix']
@config_model_name = config['model_name']
@type_trans = config['type_trans']
@@ -167,12 +177,12 @@
def model_suffix
@config_model_suffix || @model_default_suffix
end
- def req_api_model
- uri = URI.parse(url_str)
+ def get_interface_api_model
+ uri = URI.parse(interface_url_str)
req = Net::HTTP::Get.new(uri)
req['Cookie'] = @http_headers.join('; ')
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
@@ -180,10 +190,21 @@
puts res.body unless res_json['errcode'].to_i.zero?
puts res.body if @debug
res_json
end
+ def get_project_info(project_id)
+ url = project_url_str(project_id)
+ uri = URI.parse(url)
+ req = Net::HTTP::Get.new(uri)
+ req['Cookie'] = @http_headers.join('; ')
+ res = Net::HTTP.start(uri.hostname, uri.port) do |http|
+ http.request(req)
+ end
+ JSON.parse(res.body)
+ end
+
def print_res_body_model(res_json)
res_body = fetch_res_boy(res_json)
return unless res_body
puts "\n<===============打印返回数据模型-Begin=====================>\n".green
@@ -399,11 +420,11 @@
puts "\n\n"
when 'java'
end
end
- def print_http_method(data, result_model_name, param_model_name)
+ def print_http_method(data, project_info, result_model_name, param_model_name)
return unless data
path = data['path']
if path
path_name = path.split('/').map { |s| s.gsub(/[^A-Za-z0-9]/, '').gsub(/^\w/) { Regexp.last_match(0).upcase } }.join('')
@@ -419,10 +440,10 @@
yapi_temp = Lhj::ErbTemplateHelper.load('oc_code_service')
yapi_vars = { title: data['title'],
desc: data['desc'],
username: data['username'],
- path: path,
+ path: "#{project_info['data']['basepath']}#{path}",
path_key: path_key,
path_name: path_name,
result_model_name: result_model_name,
param_model_name: param_model_name || 'MLParamModel',
mth: mth,