trabajo-afectivo/lib/github/http_client.rb

50 lines
1.1 KiB
Ruby
Raw Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2021-03-10 16:25:26 +00:00
require 'uri'
2021-03-10 16:25:26 +00:00
class GitHub
class HttpClient
attr_reader :api_token, :endpoint
2021-03-10 16:25:26 +00:00
def initialize(endpoint, api_token)
raise 'api_token required' if api_token.blank?
raise 'endpoint required' if endpoint.blank? || endpoint.exclude?('/graphql') || endpoint.scan(URI::DEFAULT_PARSER.make_regexp).blank?
2021-03-10 16:25:26 +00:00
@api_token = api_token
@endpoint = endpoint
end
def perform(payload)
response = UserAgent.post(
endpoint,
payload,
{
headers: headers,
json: true,
open_timeout: 6,
read_timeout: 16,
log: {
facility: 'GitHub',
},
verify_ssl: true,
},
)
2021-03-10 16:25:26 +00:00
if !response.success?
Rails.logger.error response.error
raise __('GitHub request failed! Please have a look at the log file for details')
end
response.data
2021-03-10 16:25:26 +00:00
end
private
def headers
2021-03-10 16:25:26 +00:00
{
Authorization: "bearer #{api_token}"
2021-03-10 16:25:26 +00:00
}
end
end
end