Class: Datahen::Client::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/datahen/client/base.rb

Constant Summary collapse

DEFAULT_RETRY_LIMIT =
{
  seeder: nil,
  parser: nil,
  finisher: nil
}
CHECK_NIL =
lambda{|v|v.nil?}
CHECK_EMPTY_BODY =
lambda{|v|v.body.nil? || v.body.empty?}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/datahen/client/base.rb', line 96

def initialize(opts={})
  @ignore_ssl = opts[:ignore_ssl]
  self.class.base_uri(env_api_url)
  self.auth_token = opts[:auth_token] unless opts[:auth_token].nil?
  @options = {
    headers: {
      "Authorization" => "Bearer #{auth_token}",
      "Content-Type" => "application/json",
    },
    verify: !ignore_ssl
  }

  # extract and merge retry limits
  @default_retry_limit = self.left_merge(DEFAULT_RETRY_LIMIT, opts[:retry_limit])

  query = {}
  query[:p] = opts[:page] if opts[:page]
  query[:pp] = opts[:per_page] if opts[:per_page]
  query[:fetchfail] = opts[:fetch_fail] if opts[:fetch_fail]
  query[:parsefail] = opts[:parse_fail] if opts[:parse_fail]
  query[:status] = opts[:status] if opts[:status]
  query[:page_type] = opts[:page_type] if opts[:page_type]
  query[:url] = opts[:url] if opts[:url]
  query[:effective_url] = opts[:effective_url] if opts[:effective_url]
  query[:body] = opts[:body] if opts[:body]
  query[:parent_gid] = opts[:parent_gid] if opts[:parent_gid]
  query[:gid] = opts[:gid] if opts[:gid]
  query[:"min-timestamp"] = opts[:"min-timestamp"] if opts[:"min-timestamp"]
  query[:"max-timestamp"] = opts[:"max-timestamp"] if opts[:"max-timestamp"]
  query[:limit] = opts[:limit] if opts[:limit]
  query[:order] = opts[:order] if opts[:order]
  query[:filter] = opts[:filter] if opts[:filter]
  query[:force] = opts[:force] if opts[:force]
  query[:action] = opts[:action] if opts[:action]
  query[:"include-system"] = opts[:"include-system"] if opts[:"include-system"]
  query[:"pod"] = opts[:"pod"] if opts[:"pod"]
  query[:"container"] = opts[:"container"] if opts[:"container"]
  query[:"executor"] = opts[:"executor"] if opts[:"executor"]
  

  if opts[:query]
    if opts[:query].is_a?(Hash)
      query[:q] = opts[:query].to_json
    elsif opts[:query].is_a?(String)
      query[:q] = JSON.parse(opts[:query]).to_json
    end
  end

  unless query.empty?
    @options.merge!(query: query)
  end
end

Class Method Details

.env_auth_tokenObject



19
20
21
# File 'lib/datahen/client/base.rb', line 19

def self.env_auth_token
  ENV['DATAHEN_TOKEN']
end

.env_ignore_sslObject



23
24
25
# File 'lib/datahen/client/base.rb', line 23

def self.env_ignore_ssl
  ENV['DATAHEN_IGNORE_SSL'].to_s.strip == '1'
end

.random_delay(max_seconds = 2) ⇒ Object



27
28
29
# File 'lib/datahen/client/base.rb', line 27

def self.random_delay max_seconds = 2
  (rand * max_seconds * 1000.0).to_i / 1000.0
end

Instance Method Details

#auth_tokenObject



41
42
43
# File 'lib/datahen/client/base.rb', line 41

def auth_token
  @auth_token ||= self.class.env_auth_token
end

#auth_token=(value) ⇒ Object



45
46
47
# File 'lib/datahen/client/base.rb', line 45

def auth_token= value
  @auth_token = value
end

#default_retry_limitObject



49
50
51
# File 'lib/datahen/client/base.rb', line 49

def default_retry_limit
  @default_retry_limit ||= DEFAULT_RETRY_LIMIT.dup
end

#env_api_urlObject



31
32
33
# File 'lib/datahen/client/base.rb', line 31

def env_api_url
  ENV['DATAHEN_API_URL'].nil? ? 'https://app.datahen.com/api/v1' : ENV['DATAHEN_API_URL']
end

#ignore_sslObject



35
36
37
38
39
# File 'lib/datahen/client/base.rb', line 35

def ignore_ssl
  return @ignore_ssl unless @ignore_ssl.nil?
  @ignore_ssl = self.class.env_ignore_ssl
  @ignore_ssl
end

#left_merge(target, source) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/datahen/client/base.rb', line 53

def left_merge target, source
  # validate source and target
  return {} if target.nil? || !target.is_a?(Hash)
  return target if source.nil? || !source.is_a?(Hash)

  # left merge source into target
  target.merge(source.select{|k,v|target.has_key?(k)})
end

#retry(times, delay = nil, err_msg = nil, stream = false, check_nil = CHECK_NIL) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/datahen/client/base.rb', line 62

def retry(times, delay = nil, err_msg = nil, stream = false, check_nil = CHECK_NIL)
  limit = times.nil? ? nil : times.to_i
  delay = delay.nil? ? 5 : delay.to_i
  count = 0
  begin
    val = yield
    if stream
      return if check_nil.call(val)
      if val['error'] != ""
        raise StandardError.new(val['error'])
      end
    end
  rescue Error::CustomRetryError, StandardError => e
    is_custom_retry = e.is_a? Error::CustomRetryError
    real_delay = is_custom_retry ? e.delay : delay
    err_msg = is_custom_retry ? e.error : e.inspect
    
    STDERR.puts(err_msg)

    # wait before retry (default 5 sec)
    sleep(delay) if real_delay > 0

    # raise error when retry limit is reached
    raise e unless limit.nil? || count < limit

    # retry with a 100+ failsafe to prevent overflow error due integer limit
    should_aprox = limit.nil? && count > 99
    count += 1 unless should_aprox
    puts "#{err_msg.nil? ? '' : "#{err_msg} "}Retry \##{count}#{should_aprox ? '+' : ''}..."
    retry
  end
  val
end