http_client.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'uri'
  3. class GitLab
  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. # returns path of the subfolder of the endpoint if exists
  13. def endpoint_path
  14. path = URI.parse(endpoint).path
  15. return if path.blank?
  16. return if path == '/api/graphql'
  17. if path.start_with?('/')
  18. path = path[1..]
  19. end
  20. path.sub('api/graphql', '')
  21. end
  22. def perform(payload)
  23. response = UserAgent.post(
  24. endpoint,
  25. payload,
  26. {
  27. headers: headers,
  28. json: true,
  29. open_timeout: 6,
  30. read_timeout: 16,
  31. log: {
  32. facility: 'GitLab',
  33. },
  34. verify_ssl: true,
  35. },
  36. )
  37. if !response.success?
  38. Rails.logger.error response.error
  39. raise __('GitLab request failed! Please have a look at the log file for details')
  40. end
  41. response.data
  42. end
  43. private
  44. def headers
  45. {
  46. 'PRIVATE-TOKEN': @api_token
  47. }
  48. end
  49. end
  50. end