From cbd7871d8a9f9e6029fcbddd4915fc6ca2b1a7e7 Mon Sep 17 00:00:00 2001 From: viletyy Date: Wed, 18 Jan 2023 21:44:39 +0800 Subject: [PATCH] add: config about logger --- .gitignore | 1 + Gemfile.lock | 2 +- lib/gitea/api/client.rb | 1 + lib/gitea/api/config.rb | 2 +- lib/gitea/api/http.rb | 25 ++++++++++++++++++------- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 9106b2a..cbb558b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ /pkg/ /spec/reports/ /tmp/ +/log/ \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index dc027bc..8901e66 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - gitea-client (0.10.0) + gitea-client (1.1.0) rest-client (~> 2.1.0) GEM diff --git a/lib/gitea/api/client.rb b/lib/gitea/api/client.rb index f6af8a8..60f20a3 100644 --- a/lib/gitea/api/client.rb +++ b/lib/gitea/api/client.rb @@ -13,6 +13,7 @@ module Gitea # @option opts [string] gitea_token [选填] 用户token def initialize(opts) @config = Config.new(opts) + Gitea::Common::Logging.set_log_file(@config.log_filepath) @http = Http.new(@config) end diff --git a/lib/gitea/api/config.rb b/lib/gitea/api/config.rb index 12453f8..34115cf 100644 --- a/lib/gitea/api/config.rb +++ b/lib/gitea/api/config.rb @@ -2,7 +2,7 @@ module Gitea module Api class Config < Common::Struct::Base - attrs :domain, :base_url, :username, :password, :token, :open_timeout, :read_timeout + attrs :domain, :base_url, :username, :password, :token, :open_timeout, :read_timeout, :log_filepath def initialize(opts = {}) super(opts) diff --git a/lib/gitea/api/http.rb b/lib/gitea/api/http.rb index 71b312f..1b3c088 100644 --- a/lib/gitea/api/http.rb +++ b/lib/gitea/api/http.rb @@ -65,7 +65,10 @@ module Gitea headers[:params] = http_options[:query] || {} headers[:params].merge!({access_token: @config.token}) if @config.token - logger.debug("Send HTTP request, verb: #{verb}, http_options: #{http_options}") + logger.info("Gitea Client Begin a Request!...") + logger.info("Send HTTP request, verb: #{verb}, http_options: #{http_options}") + logger.info("Relative Url: #{api_url}") + logger.info("Headers: #{headers}") request = RestClient::Request.new( :method => verb, @@ -102,13 +105,21 @@ module Gitea end response = RestClient::Response.create(nil, response, request) response.return! - end + end + + begin + if response.headers.has_key?(:x_total) + return {data: JSON.parse(response), total_data: response.headers[:x_total]} + else + return JSON.parse(response) + end + rescue => e + logger.error(e.to_s) + return {} + end + + logger.info("Gitea Hat Client Success End a Request!...") - if response.headers.has_key?(:x_total) - return {data: JSON.parse(response), total_data: response.headers[:x_total]} - else - return JSON.parse(response) - end rescue {} end def get_user_agent