Class: Gitlab::Triage::NetworkAdapters::HttpartyAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/gitlab/triage/network_adapters/httparty_adapter.rb

Constant Summary

Constants inherited from BaseAdapter

BaseAdapter::USER_AGENT

Instance Attribute Summary

Attributes inherited from BaseAdapter

#options

Instance Method Summary collapse

Methods inherited from BaseAdapter

#initialize

Constructor Details

This class inherits a constructor from Gitlab::Triage::NetworkAdapters::BaseAdapter

Instance Method Details

#delete(token, url) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 56

def delete(token, url)
  response = HTTParty.delete(
    url,
    headers: {
      'User-Agent' => USER_AGENT,
      'PRIVATE-TOKEN' => token
    }
  )

  raise_on_unauthorized_error!(response)
  raise_on_internal_server_error!(response)
  raise_on_too_many_requests!(response)

  {
    results: response.parsed_response,
    ratelimit_remaining: response.headers["ratelimit-remaining"].to_i,
    ratelimit_reset_at: Time.at(response.headers["ratelimit-reset"].to_i)
  }
end

#get(token, url) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 11

def get(token, url)
  response = HTTParty.get(
    url,
    headers: {
      'User-Agent' => USER_AGENT,
      'Content-type' => 'application/json',
      'PRIVATE-TOKEN' => token
    }
  )

  raise_on_unauthorized_error!(response)
  raise_on_internal_server_error!(response)
  raise_on_too_many_requests!(response)

  {
    more_pages: (response.headers["x-next-page"].to_s != ""),
    next_page_url: next_page_url(url, response),
    results: response.parsed_response,
    ratelimit_remaining: response.headers["ratelimit-remaining"].to_i,
    ratelimit_reset_at: Time.at(response.headers["ratelimit-reset"].to_i)
  }
end

#next_page_url(url, response) ⇒ Object (private)



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 102

def next_page_url(url, response)
  return unless response.headers['x-next-page'].present?

  next_page = "&page=#{response.headers['x-next-page']}"

  if url.include?('&page')
    url.gsub(/&page=\d+/, next_page)
  else
    url + next_page
  end
end

#post(token, url, body) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 34

def post(token, url, body)
  response = HTTParty.post(
    url,
    body: body.to_json,
    headers: {
      'User-Agent' => "GitLab Triage #{Gitlab::Triage::VERSION}",
      'Content-type' => 'application/json',
      'PRIVATE-TOKEN' => token
    }
  )

  raise_on_unauthorized_error!(response)
  raise_on_internal_server_error!(response)
  raise_on_too_many_requests!(response)

  {
    results: response.parsed_response,
    ratelimit_remaining: response.headers["ratelimit-remaining"].to_i,
    ratelimit_reset_at: Time.at(response.headers["ratelimit-reset"].to_i)
  }
end

#raise_on_internal_server_error!(response) ⇒ Object (private)



86
87
88
89
90
91
92
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 86

def raise_on_internal_server_error!(response)
  return unless response.response.is_a?(Net::HTTPInternalServerError)

  puts Gitlab::Triage::UI.debug response.inspect if options.debug

  raise Errors::Network::InternalServerError, 'Internal server error encountered!'
end

#raise_on_too_many_requests!(response) ⇒ Object (private)



94
95
96
97
98
99
100
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 94

def raise_on_too_many_requests!(response)
  return unless response.response.is_a?(Net::HTTPTooManyRequests)

  puts Gitlab::Triage::UI.debug response.inspect if options.debug

  raise Errors::Network::TooManyRequests, 'Too many requests made!'
end

#raise_on_unauthorized_error!(response) ⇒ Object (private)



78
79
80
81
82
83
84
# File 'lib/gitlab/triage/network_adapters/httparty_adapter.rb', line 78

def raise_on_unauthorized_error!(response)
  return unless response.response.is_a?(Net::HTTPUnauthorized)

  puts Gitlab::Triage::UI.debug response.inspect if options.debug

  raise 'The provided token is unauthorized!'
end