http_client.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 __('Invalid GitHub configuration (missing endpoint or api_token).') if api_token.blank? || endpoint.blank? || endpoint.exclude?('/graphql') || endpoint.scan(URI::DEFAULT_PARSER.make_regexp).blank?
  8. @api_token = api_token
  9. @endpoint = endpoint
  10. end
  11. def perform(payload)
  12. response = UserAgent.post(
  13. endpoint,
  14. payload,
  15. {
  16. headers: headers,
  17. json: true,
  18. open_timeout: 6,
  19. read_timeout: 16,
  20. log: {
  21. facility: 'GitHub',
  22. },
  23. verify_ssl: true,
  24. },
  25. )
  26. if !response.success?
  27. Rails.logger.error response.error
  28. raise __('GitHub request failed. Please have a look at the log file for details.')
  29. end
  30. response.data
  31. end
  32. private
  33. def headers
  34. {
  35. Authorization: "bearer #{api_token}"
  36. }
  37. end
  38. end
  39. end