From 80dbfea249b44418fe94e570427cda68b1bb0c82 Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Tue, 23 Jul 2024 18:00:10 +0200 Subject: [PATCH 1/4] redmine api, revise http headers * get wants an accept and not a content type --- lib/buchungsstreber/redmine_api.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/buchungsstreber/redmine_api.rb b/lib/buchungsstreber/redmine_api.rb index bf7e4b4..437ef58 100644 --- a/lib/buchungsstreber/redmine_api.rb +++ b/lib/buchungsstreber/redmine_api.rb @@ -64,7 +64,7 @@ def post(path, dto) header = { "Content-Type" => "application/json", - "X-Redmine-API-Key" => @config["server"]["apikey"] + "X-Redmine-API-Key" => @config["server"]["apikey"], } request = Net::HTTP::Post.new(uri, header) request.body = dto.to_json @@ -86,8 +86,8 @@ def get(path, params = nil) https.use_ssl = true header = { - "Content-Type" => "application/json", - "X-Redmine-API-Key" => @config["server"]["apikey"] + "Accept" => "application/json", + "X-Redmine-API-Key" => @config["server"]["apikey"], } request = Net::HTTP::Get.new(uri, header) result = https.request(request) From 08591c55d2972e3d8f21ec668b2b6510500a50ad Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Tue, 23 Jul 2024 18:00:55 +0200 Subject: [PATCH 2/4] redmine api, use different get api in net:http * try to fix endless hang on certain hosts --- lib/buchungsstreber/redmine_api.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/buchungsstreber/redmine_api.rb b/lib/buchungsstreber/redmine_api.rb index 437ef58..7c67c7b 100644 --- a/lib/buchungsstreber/redmine_api.rb +++ b/lib/buchungsstreber/redmine_api.rb @@ -82,15 +82,12 @@ def post(path, dto) def get(path, params = nil) uri = URI.parse("#{@config['server']['url']}#{path}.json") uri.query = URI.encode_www_form(params) if params - https = Net::HTTP.new(uri.host, uri.port) - https.use_ssl = true header = { "Accept" => "application/json", "X-Redmine-API-Key" => @config["server"]["apikey"], } - request = Net::HTTP::Get.new(uri, header) - result = https.request(request) + result = Net::HTTP.get_response(uri, header) raise 'Unexpected result code' unless result.code == "200" result.body.force_encoding("utf-8") From a62951ff88ab476e53ded89122add3698d90d68e Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Wed, 24 Jul 2024 10:56:40 +0200 Subject: [PATCH 3/4] redmine api, further testing of hanging code --- lib/buchungsstreber/redmine_api.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/buchungsstreber/redmine_api.rb b/lib/buchungsstreber/redmine_api.rb index 7c67c7b..a495c0e 100644 --- a/lib/buchungsstreber/redmine_api.rb +++ b/lib/buchungsstreber/redmine_api.rb @@ -3,6 +3,7 @@ require "net/https" require "json" require "yaml" +require 'open3' class Buchungsstreber::RedmineApi attr_reader :config @@ -87,15 +88,17 @@ def get(path, params = nil) "Accept" => "application/json", "X-Redmine-API-Key" => @config["server"]["apikey"], } - result = Net::HTTP.get_response(uri, header) - raise 'Unexpected result code' unless result.code == "200" - result.body.force_encoding("utf-8") - body = JSON.parse(result.body) + mh = header.map{|k,v| ['-H', "#{k}: #{v}"]}.flatten + body, _, status = Open3.capture3('/usr/bin/curl', '-s', *mh, uri.to_s) + raise 'Unexpected result code' unless status == 0 + + body.force_encoding("utf-8") + body = JSON.parse(body) (yield body if block_given?) || body rescue StandardError => e - h = { url: path, error: e, content: result&.body } + h = { url: path, error: e, content: body } raise "Fehler beim Laden von %s: %s, Rückgabe: %s" % h end From 6329c668096a2f33d419b614b98521ebb8461a6b Mon Sep 17 00:00:00 2001 From: Jonathan Buch Date: Wed, 24 Jul 2024 10:56:58 +0200 Subject: [PATCH 4/4] redmine api, get timeout 2 seconds --- lib/buchungsstreber/redmine_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/buchungsstreber/redmine_api.rb b/lib/buchungsstreber/redmine_api.rb index a495c0e..588a9c7 100644 --- a/lib/buchungsstreber/redmine_api.rb +++ b/lib/buchungsstreber/redmine_api.rb @@ -90,7 +90,7 @@ def get(path, params = nil) } mh = header.map{|k,v| ['-H', "#{k}: #{v}"]}.flatten - body, _, status = Open3.capture3('/usr/bin/curl', '-s', *mh, uri.to_s) + body, _, status = Open3.capture3('/usr/bin/curl', '-m', '2', '-s', *mh, uri.to_s) raise 'Unexpected result code' unless status == 0 body.force_encoding("utf-8")