http_client.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'uri'
  3. class GitHub
  4. class HttpClient
  5. attr_reader :api_token, :endpoint
  6. def initialize(endpoint, api_token)
  7. raise 'api_token required' if api_token.blank?
  8. raise 'endpoint required' if endpoint.blank? || endpoint.exclude?('/graphql') || endpoint.scan(URI::DEFAULT_PARSER.make_regexp).blank?
  9. @api_token = api_token
  10. @endpoint = endpoint
  11. end
  12. def perform(payload)
  13. response = UserAgent.post(
  14. endpoint,
  15. payload,
  16. {
  17. headers: headers,
  18. json: true,
  19. open_timeout: 6,
  20. read_timeout: 16,
  21. log: {
  22. facility: 'GitHub',
  23. },
  24. verify_ssl: true,
  25. },
  26. )
  27. if !response.success?
  28. Rails.logger.error response.error
  29. raise __('GitHub request failed! Please have a look at the log file for details')
  30. end
  31. response.data
  32. end
  33. private
  34. def headers
  35. {
  36. Authorization: "bearer #{api_token}"
  37. }
  38. end
  39. end
  40. end