http_client.rb 955 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class GitLab
  3. class HttpClient
  4. attr_reader :api_token, :endpoint
  5. def initialize(endpoint, api_token)
  6. raise 'api_token required' if api_token.blank?
  7. raise 'endpoint required' if endpoint.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: 'GitLab',
  22. },
  23. },
  24. )
  25. if !response.success?
  26. Rails.logger.error response.error
  27. raise "Error while requesting GitLab GraphQL API: #{response.error}"
  28. end
  29. response.data
  30. end
  31. private
  32. def headers
  33. {
  34. "PRIVATE-TOKEN": @api_token
  35. }
  36. end
  37. end
  38. end