lib/lhj/command/yapi.rb in lhj-tools-0.1.87 vs lib/lhj/command/yapi.rb in lhj-tools-0.1.88
- old
+ new
@@ -49,21 +49,33 @@
def handle
process(yml_file) if File.exist?(yml_file)
end
def process(config_file)
- load_config(config_file)
- res_body = get_interface_api_model
+ config = load_config(config_file)
+ process_config(config)
+ res_body = get_interface_api_model(config)
unless res_body['errcode'].to_i.zero?
+ if File.exist?(user_yml_file)
+ user_infos = load_config(user_yml_file)
+ user_infos.each do |c|
+ res_body = get_interface_api_model(c)
+ config = c
+ break if res_body['errcode'].to_i.zero?
+ end
+ save_config(config) if res_body['errcode'].to_i.zero?
+ end
+ end
+ 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)
+ project_info = get_project_info(project_id, config)
# 1.print request result
res_models = print_res_body_model(res_body)
# 2.print request json body
req_models = print_req_body_model(res_body) if res_body['data']['req_body_is_json_schema']
# 3.print request param
@@ -174,25 +186,39 @@
def yml_file
File.join(Lhj::Config.instance.home_dir, 'yapi.yml')
end
+ def user_yml_file
+ File.join(Lhj::Config.instance.home_dir, 'yapi_user.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
+ YAML.load_file(file)
+ end
+
+ def process_config(config)
@config_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']
@config_robot_url = config['dingtalk']
@type_trans = config['type_trans']
@config_property_mapper = config['property_mapper']
end
+ def save_config(config)
+ yml = load_config(yml_file)
+ yml['__wpkreporterwid_'] = config['__wpkreporterwid_']
+ yml['_yapi_uid'] = config['_yapi_uid']
+ yml['_yapi_token'] = config['_yapi_token']
+ file_to_save = File.open(yml_file, 'w+')
+ YAML.dump(yml, file_to_save)
+ file_to_save.close
+ end
+
def api_id
@id || @config_id.to_s
end
def model_pre
@@ -217,27 +243,35 @@
def property_mapper
@config_property_mapper || { 'id' => 'gid' }
end
- def get_interface_api_model
+ def get_interface_api_model(config)
uri = URI.parse(interface_url_str)
req = Net::HTTP::Get.new(uri)
- req['Cookie'] = @http_headers.join('; ')
+ header_arr = []
+ config.each do |k, v|
+ header_arr << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
+ end
+ req['Cookie'] = header_arr.join('; ')
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
res_json = JSON.parse(res.body)
puts res.body unless res_json['errcode'].to_i.zero?
puts res.body if @debug
res_json
end
- def get_project_info(project_id)
+ def get_project_info(project_id, config)
url = project_url_str(project_id)
uri = URI.parse(url)
req = Net::HTTP::Get.new(uri)
- req['Cookie'] = @http_headers.join('; ')
+ header_arr = []
+ config.each do |k, v|
+ header_arr << "#{k}=#{v}" if k.eql?('__wpkreporterwid_') || k.eql?('_yapi_token') || k.eql?('_yapi_uid')
+ end
+ req['Cookie'] = header_arr.join('; ')
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
JSON.parse(res.body)
end