Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redmine req handling #128

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions lib/buchungsstreber/redmine_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "net/https"
require "json"
require "yaml"
require 'open3'

class Buchungsstreber::RedmineApi
attr_reader :config
Expand Down Expand Up @@ -64,7 +65,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
Expand All @@ -82,23 +83,22 @@ 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 = {
"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)
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', '-m', '2', '-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 %<url>s: %<error>s, Rückgabe: %<content>s" % h
end

Expand Down
Loading