Module: Webhookdb::Http

Includes:
Appydays::Configurable
Defined in:
lib/webhookdb/http.rb

Defined Under Namespace

Classes: BaseError, Error

Class Method Summary collapse

Class Method Details

._setup_required_args(options) ⇒ Object

Raises:

  • (ArgumentError)


92
93
94
95
96
97
98
# File 'lib/webhookdb/http.rb', line 92

def self._setup_required_args(options)
  raise ArgumentError, "must pass :timeout keyword" unless options.key?(:timeout)

  raise ArgumentError, "must pass :logger keyword" unless options.key?(:logger)
  options[:log_format] = :appydays
  options[:log_level] = self.log_level
end

.check!(response, **options) ⇒ Object

Raises:



60
61
62
63
64
65
66
67
# File 'lib/webhookdb/http.rb', line 60

def self.check!(response, **options)
  # All oks are ok
  return if response.code < 300
  # We expect 300s if we aren't following redirects
  return if response.code < 400 && !options[:follow_redirects]
  # Raise for 400s, or 300s if we were meant to follow redirects
  raise Error, response
end

.chunked_download(request_url, rewindable: false, **down_kw) ⇒ Object

Convenience wrapper around Down so we can use our preferred implementation. See commit history for more info.

Raises:

  • (URI::InvalidURIError)


103
104
105
106
107
108
109
110
# File 'lib/webhookdb/http.rb', line 103

def self.chunked_download(request_url, rewindable: false, **down_kw)
  uri = URI(request_url)
  raise URI::InvalidURIError, "#{request_url} must be an http/s url" unless ["http", "https"].include?(uri.scheme)
  down_kw[:headers] ||= {}
  down_kw[:headers]["User-Agent"] ||= self.user_agent
  io = Down::Httpx.open(uri, rewindable:, **down_kw)
  return io
end

.extract_url_auth(url) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/webhookdb/http.rb', line 46

def self.extract_url_auth(url)
  parsed_uri = URI(url)
  if parsed_uri.userinfo.present?
    auth_params = {
      username: URI.decode_www_form_component(parsed_uri.user || ""),
      password: URI.decode_www_form_component(parsed_uri.password || ""),
    }
    parsed_uri.user = parsed_uri.password = nil
    cleaned_url = parsed_uri.to_s
    return cleaned_url, auth_params
  end
  return url, nil
end

.get(url, query = {}, **options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/webhookdb/http.rb', line 69

def self.get(url, query={}, **options, &)
  self._setup_required_args(options)
  opts = {query:, headers: {}}.merge(**options)
  opts[:headers]["User-Agent"] = self.user_agent
  # See https://github.com/jnunemaker/httparty/issues/784#issuecomment-1585714745
  # I *think* this should be safe to always use.
  opts[:headers]["Connection"] ||= "keep-alive"
  r = HTTParty.get(url, **opts, &)
  self.check!(r, **opts)
  return r
end

.post(url, body = {}, headers: {}, method: nil, check: true, **options) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/webhookdb/http.rb', line 81

def self.post(url, body={}, headers: {}, method: nil, check: true, **options, &)
  self._setup_required_args(options)
  headers["Content-Type"] ||= "application/json"
  headers["User-Agent"] = self.user_agent
  body = body.to_json if !body.is_a?(String) && headers["Content-Type"].include?("json")
  opts = {body:, headers:}.merge(**options)
  r = HTTParty.send(method || :post, url, **opts, &)
  self.check!(r, **options) if check
  return r
end

.user_agentObject



41
42
43
44
# File 'lib/webhookdb/http.rb', line 41

def self.user_agent
  return Webhookdb.http_user_agent unless Webhookdb.http_user_agent.blank?
  return "WebhookDB/#{Webhookdb::RELEASE} https://webhookdb.com #{Webhookdb::RELEASE_CREATED_AT}"
end