togglr-sdk 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +15 -0
- data/LICENSE +21 -0
- data/README.md +297 -0
- data/generated/Gemfile +9 -0
- data/generated/README.md +121 -0
- data/generated/Rakefile +10 -0
- data/generated/docs/DefaultApi.md +282 -0
- data/generated/docs/Error.md +18 -0
- data/generated/docs/ErrorBadRequest.md +18 -0
- data/generated/docs/ErrorError.md +18 -0
- data/generated/docs/ErrorInternalServerError.md +18 -0
- data/generated/docs/ErrorNotFound.md +18 -0
- data/generated/docs/ErrorPermissionDenied.md +18 -0
- data/generated/docs/ErrorTooManyRequests.md +18 -0
- data/generated/docs/ErrorUnauthorized.md +18 -0
- data/generated/docs/EvaluateResponse.md +22 -0
- data/generated/docs/FeatureErrorReport.md +22 -0
- data/generated/docs/FeatureHealth.md +30 -0
- data/generated/docs/HealthResponse.md +20 -0
- data/generated/git_push.sh +57 -0
- data/generated/lib/togglr-client/api/default_api.rb +284 -0
- data/generated/lib/togglr-client/api_client.rb +437 -0
- data/generated/lib/togglr-client/api_error.rb +58 -0
- data/generated/lib/togglr-client/configuration.rb +392 -0
- data/generated/lib/togglr-client/models/error.rb +237 -0
- data/generated/lib/togglr-client/models/error_bad_request.rb +244 -0
- data/generated/lib/togglr-client/models/error_error.rb +220 -0
- data/generated/lib/togglr-client/models/error_internal_server_error.rb +244 -0
- data/generated/lib/togglr-client/models/error_not_found.rb +244 -0
- data/generated/lib/togglr-client/models/error_permission_denied.rb +244 -0
- data/generated/lib/togglr-client/models/error_too_many_requests.rb +244 -0
- data/generated/lib/togglr-client/models/error_unauthorized.rb +244 -0
- data/generated/lib/togglr-client/models/evaluate_response.rb +289 -0
- data/generated/lib/togglr-client/models/feature_error_report.rb +274 -0
- data/generated/lib/togglr-client/models/feature_health.rb +342 -0
- data/generated/lib/togglr-client/models/health_response.rb +287 -0
- data/generated/lib/togglr-client/version.rb +15 -0
- data/generated/lib/togglr-client.rb +52 -0
- data/generated/spec/api/default_api_spec.rb +81 -0
- data/generated/spec/models/error_bad_request_spec.rb +36 -0
- data/generated/spec/models/error_error_spec.rb +36 -0
- data/generated/spec/models/error_internal_server_error_spec.rb +36 -0
- data/generated/spec/models/error_not_found_spec.rb +36 -0
- data/generated/spec/models/error_permission_denied_spec.rb +36 -0
- data/generated/spec/models/error_spec.rb +36 -0
- data/generated/spec/models/error_too_many_requests_spec.rb +36 -0
- data/generated/spec/models/error_unauthorized_spec.rb +36 -0
- data/generated/spec/models/evaluate_response_spec.rb +48 -0
- data/generated/spec/models/feature_error_report_spec.rb +48 -0
- data/generated/spec/models/feature_health_spec.rb +72 -0
- data/generated/spec/models/health_response_spec.rb +46 -0
- data/generated/spec/spec_helper.rb +111 -0
- data/generated/togglr-client.gemspec +41 -0
- data/lib/togglr/cache.rb +45 -0
- data/lib/togglr/client.rb +267 -0
- data/lib/togglr/config.rb +37 -0
- data/lib/togglr/errors.rb +25 -0
- data/lib/togglr/logger.rb +38 -0
- data/lib/togglr/metrics.rb +44 -0
- data/lib/togglr/models.rb +56 -0
- data/lib/togglr/options.rb +57 -0
- data/lib/togglr/request_context.rb +147 -0
- data/lib/togglr/version.rb +3 -0
- data/lib/togglr-client/api/default_api.rb +284 -0
- data/lib/togglr-client/api_client.rb +437 -0
- data/lib/togglr-client/api_error.rb +58 -0
- data/lib/togglr-client/configuration.rb +392 -0
- data/lib/togglr-client/models/error.rb +237 -0
- data/lib/togglr-client/models/error_bad_request.rb +244 -0
- data/lib/togglr-client/models/error_error.rb +220 -0
- data/lib/togglr-client/models/error_internal_server_error.rb +244 -0
- data/lib/togglr-client/models/error_not_found.rb +244 -0
- data/lib/togglr-client/models/error_permission_denied.rb +244 -0
- data/lib/togglr-client/models/error_too_many_requests.rb +244 -0
- data/lib/togglr-client/models/error_unauthorized.rb +244 -0
- data/lib/togglr-client/models/evaluate_response.rb +289 -0
- data/lib/togglr-client/models/feature_error_report.rb +274 -0
- data/lib/togglr-client/models/feature_health.rb +342 -0
- data/lib/togglr-client/models/health_response.rb +287 -0
- data/lib/togglr-client/version.rb +15 -0
- data/lib/togglr-client.rb +52 -0
- data/lib/togglr.rb +14 -0
- data/spec/examples.txt +11 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/togglr_spec.rb +98 -0
- metadata +199 -0
@@ -0,0 +1,111 @@
|
|
1
|
+
=begin
|
2
|
+
#SDK API
|
3
|
+
|
4
|
+
#No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
5
|
+
|
6
|
+
The version of the OpenAPI document: 1.0.0
|
7
|
+
|
8
|
+
Generated by: https://openapi-generator.tech
|
9
|
+
Generator version: 7.15.0
|
10
|
+
|
11
|
+
=end
|
12
|
+
|
13
|
+
# load the gem
|
14
|
+
require 'togglr-client'
|
15
|
+
|
16
|
+
# The following was generated by the `rspec --init` command. Conventionally, all
|
17
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
18
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
19
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
20
|
+
# files.
|
21
|
+
#
|
22
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
23
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
24
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
25
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
26
|
+
# a separate helper file that requires the additional dependencies and performs
|
27
|
+
# the additional setup, and require it from the spec files that actually need
|
28
|
+
# it.
|
29
|
+
#
|
30
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
31
|
+
# users commonly want.
|
32
|
+
#
|
33
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
34
|
+
RSpec.configure do |config|
|
35
|
+
# rspec-expectations config goes here. You can use an alternate
|
36
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
37
|
+
# assertions if you prefer.
|
38
|
+
config.expect_with :rspec do |expectations|
|
39
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
40
|
+
# and `failure_message` of custom matchers include text for helper methods
|
41
|
+
# defined using `chain`, e.g.:
|
42
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
43
|
+
# # => "be bigger than 2 and smaller than 4"
|
44
|
+
# ...rather than:
|
45
|
+
# # => "be bigger than 2"
|
46
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
47
|
+
end
|
48
|
+
|
49
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
50
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
51
|
+
config.mock_with :rspec do |mocks|
|
52
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
53
|
+
# a real object. This is generally recommended, and will default to
|
54
|
+
# `true` in RSpec 4.
|
55
|
+
mocks.verify_partial_doubles = true
|
56
|
+
end
|
57
|
+
|
58
|
+
# The settings below are suggested to provide a good initial experience
|
59
|
+
# with RSpec, but feel free to customize to your heart's content.
|
60
|
+
=begin
|
61
|
+
# These two settings work together to allow you to limit a spec run
|
62
|
+
# to individual examples or groups you care about by tagging them with
|
63
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
64
|
+
# get run.
|
65
|
+
config.filter_run :focus
|
66
|
+
config.run_all_when_everything_filtered = true
|
67
|
+
|
68
|
+
# Allows RSpec to persist some state between runs in order to support
|
69
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
70
|
+
# you configure your source control system to ignore this file.
|
71
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
72
|
+
|
73
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
74
|
+
# recommended. For more details, see:
|
75
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
76
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
77
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
78
|
+
config.disable_monkey_patching!
|
79
|
+
|
80
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
81
|
+
# be too noisy due to issues in dependencies.
|
82
|
+
config.warnings = true
|
83
|
+
|
84
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
85
|
+
# file, and it's useful to allow more verbose output when running an
|
86
|
+
# individual spec file.
|
87
|
+
if config.files_to_run.one?
|
88
|
+
# Use the documentation formatter for detailed output,
|
89
|
+
# unless a formatter has already been configured
|
90
|
+
# (e.g. via a command-line flag).
|
91
|
+
config.default_formatter = 'doc'
|
92
|
+
end
|
93
|
+
|
94
|
+
# Print the 10 slowest examples and example groups at the
|
95
|
+
# end of the spec run, to help surface which specs are running
|
96
|
+
# particularly slow.
|
97
|
+
config.profile_examples = 10
|
98
|
+
|
99
|
+
# Run specs in random order to surface order dependencies. If you find an
|
100
|
+
# order dependency and want to debug it, you can fix the order by providing
|
101
|
+
# the seed, which is printed after each run.
|
102
|
+
# --seed 1234
|
103
|
+
config.order = :random
|
104
|
+
|
105
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
106
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
107
|
+
# test failures related to randomization by passing the same `--seed` value
|
108
|
+
# as the one that triggered the failure.
|
109
|
+
Kernel.srand config.seed
|
110
|
+
=end
|
111
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
=begin
|
4
|
+
#SDK API
|
5
|
+
|
6
|
+
#No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
7
|
+
|
8
|
+
The version of the OpenAPI document: 1.0.0
|
9
|
+
|
10
|
+
Generated by: https://openapi-generator.tech
|
11
|
+
Generator version: 7.15.0
|
12
|
+
|
13
|
+
=end
|
14
|
+
|
15
|
+
$:.push File.expand_path("../lib", __FILE__)
|
16
|
+
require "togglr-client/version"
|
17
|
+
|
18
|
+
Gem::Specification.new do |s|
|
19
|
+
s.name = "togglr-client"
|
20
|
+
s.version = TogglrClient::VERSION
|
21
|
+
s.platform = Gem::Platform::RUBY
|
22
|
+
s.authors = ["OpenAPI-Generator"]
|
23
|
+
s.email = [""]
|
24
|
+
s.homepage = "https://openapi-generator.tech"
|
25
|
+
s.summary = "SDK API Ruby Gem"
|
26
|
+
s.description = "No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)"
|
27
|
+
s.license = "Unlicense"
|
28
|
+
s.required_ruby_version = ">= 2.7"
|
29
|
+
s.metadata = {}
|
30
|
+
|
31
|
+
s.add_runtime_dependency 'faraday', '>= 1.0.1', '< 3.0'
|
32
|
+
s.add_runtime_dependency 'faraday-multipart'
|
33
|
+
s.add_runtime_dependency 'marcel'
|
34
|
+
|
35
|
+
s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
|
36
|
+
|
37
|
+
s.files = `find *`.split("\n").uniq.sort.select { |f| !f.empty? }
|
38
|
+
s.test_files = `find spec/*`.split("\n")
|
39
|
+
s.executables = []
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
end
|
data/lib/togglr/cache.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'lru_redux'
|
2
|
+
|
3
|
+
module Togglr
|
4
|
+
class Cache
|
5
|
+
class Entry
|
6
|
+
attr_reader :value, :enabled, :found, :expires_at
|
7
|
+
|
8
|
+
def initialize(value, enabled, found, ttl)
|
9
|
+
@value = value
|
10
|
+
@enabled = enabled
|
11
|
+
@found = found
|
12
|
+
@expires_at = Time.now + ttl
|
13
|
+
end
|
14
|
+
|
15
|
+
def expired?
|
16
|
+
Time.now > @expires_at
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(size, ttl)
|
21
|
+
@cache = LruRedux::TTL::Cache.new(size, ttl)
|
22
|
+
end
|
23
|
+
|
24
|
+
def get(key)
|
25
|
+
entry = @cache[key]
|
26
|
+
return nil if entry.nil? || entry.expired?
|
27
|
+
|
28
|
+
entry
|
29
|
+
end
|
30
|
+
|
31
|
+
def set(key, value, enabled, found)
|
32
|
+
entry = Entry.new(value, enabled, found, @cache.ttl)
|
33
|
+
@cache[key] = entry
|
34
|
+
end
|
35
|
+
|
36
|
+
def clear
|
37
|
+
@cache.clear
|
38
|
+
end
|
39
|
+
|
40
|
+
def size
|
41
|
+
@cache.size
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
@@ -0,0 +1,267 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'retries'
|
3
|
+
require 'digest'
|
4
|
+
|
5
|
+
# Load generated client files directly
|
6
|
+
require_relative '../togglr-client/api_client'
|
7
|
+
require_relative '../togglr-client/api_error'
|
8
|
+
require_relative '../togglr-client/version'
|
9
|
+
require_relative '../togglr-client/configuration'
|
10
|
+
require_relative '../togglr-client/models/feature_error_report'
|
11
|
+
require_relative '../togglr-client/models/feature_health'
|
12
|
+
require_relative '../togglr-client/models/evaluate_request'
|
13
|
+
require_relative '../togglr-client/api/default_api'
|
14
|
+
|
15
|
+
module Togglr
|
16
|
+
class Client
|
17
|
+
def initialize(config)
|
18
|
+
@config = config
|
19
|
+
@cache = config.cache_enabled ? Cache.new(config.cache_size, config.cache_ttl) : nil
|
20
|
+
|
21
|
+
# Initialize generated API client
|
22
|
+
api_config = TogglrClient::Configuration.new
|
23
|
+
api_config.base_path = config.base_url
|
24
|
+
api_config.api_key['Authorization'] = config.api_key
|
25
|
+
api_config.ssl_verify = !config.insecure
|
26
|
+
@api_client = TogglrClient::DefaultApi.new(TogglrClient::ApiClient.new(api_config))
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.new_with_defaults(api_key, *options)
|
30
|
+
config = Config.default(api_key)
|
31
|
+
|
32
|
+
# Apply options
|
33
|
+
options.each do |option|
|
34
|
+
option.call(config) if option.respond_to?(:call)
|
35
|
+
end
|
36
|
+
|
37
|
+
yield(config) if block_given?
|
38
|
+
new(config)
|
39
|
+
end
|
40
|
+
|
41
|
+
def evaluate(feature_key, context)
|
42
|
+
evaluate_with_context(feature_key, context, @config.api_key)
|
43
|
+
end
|
44
|
+
|
45
|
+
def evaluate_with_context(feature_key, context, project_api_key)
|
46
|
+
start_time = Time.now
|
47
|
+
@config.metrics.inc_evaluate_request
|
48
|
+
|
49
|
+
# Check cache first
|
50
|
+
cache_key = build_cache_key(feature_key, context)
|
51
|
+
if @cache
|
52
|
+
entry = @cache.get(cache_key)
|
53
|
+
if entry
|
54
|
+
@config.metrics.inc_cache_hit
|
55
|
+
@config.logger.debug('cache hit', feature_key: feature_key, cache_key: cache_key)
|
56
|
+
return [entry.value, entry.enabled, entry.found]
|
57
|
+
end
|
58
|
+
@config.metrics.inc_cache_miss
|
59
|
+
end
|
60
|
+
|
61
|
+
# Make API call with retries
|
62
|
+
result = with_retries(max_tries: @config.retries + 1) do
|
63
|
+
evaluate_single(feature_key, context, project_api_key)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Record metrics
|
67
|
+
latency = Time.now - start_time
|
68
|
+
@config.metrics.observe_evaluate_latency(latency)
|
69
|
+
|
70
|
+
value, enabled, found, error = result
|
71
|
+
if error
|
72
|
+
@config.metrics.inc_evaluate_error(error.class.name)
|
73
|
+
raise error
|
74
|
+
end
|
75
|
+
|
76
|
+
# Cache result if successful
|
77
|
+
@cache&.set(cache_key, value, enabled, found)
|
78
|
+
|
79
|
+
[value, enabled, found]
|
80
|
+
end
|
81
|
+
|
82
|
+
def is_enabled(feature_key, context)
|
83
|
+
_, enabled, found = evaluate(feature_key, context)
|
84
|
+
raise FeatureNotFoundError, "Feature #{feature_key} not found" unless found
|
85
|
+
|
86
|
+
enabled
|
87
|
+
end
|
88
|
+
|
89
|
+
def is_enabled_or_default(feature_key, context, default_value)
|
90
|
+
is_enabled(feature_key, context)
|
91
|
+
rescue StandardError => e
|
92
|
+
@config.logger.warn('evaluation failed, using default',
|
93
|
+
feature_key: feature_key, error: e.message, default: default_value)
|
94
|
+
default_value
|
95
|
+
end
|
96
|
+
|
97
|
+
def health_check
|
98
|
+
begin
|
99
|
+
@api_client.health_check
|
100
|
+
rescue TogglrClient::ApiError => e
|
101
|
+
raise "Health check failed with status #{e.code}: #{e.message}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# Report an error for a feature
|
106
|
+
def report_error(feature_key, error_type, error_message, context = {})
|
107
|
+
error = report_error_with_retries(feature_key, error_type, error_message, context)
|
108
|
+
raise error if error
|
109
|
+
nil # Success - error queued for processing
|
110
|
+
end
|
111
|
+
|
112
|
+
# Get feature health information
|
113
|
+
def get_feature_health(feature_key)
|
114
|
+
health, error = get_feature_health_with_retries(feature_key)
|
115
|
+
raise error if error
|
116
|
+
health
|
117
|
+
end
|
118
|
+
|
119
|
+
# Check if feature is healthy (simple boolean check)
|
120
|
+
def is_feature_healthy(feature_key)
|
121
|
+
health = get_feature_health(feature_key)
|
122
|
+
health.healthy?
|
123
|
+
end
|
124
|
+
|
125
|
+
def close
|
126
|
+
@cache&.clear
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def build_cache_key(feature_key, context)
|
132
|
+
context_hash = Digest::SHA256.hexdigest(context.to_h.to_json)[0, 16]
|
133
|
+
"#{feature_key}:#{context_hash}"
|
134
|
+
end
|
135
|
+
|
136
|
+
def evaluate_single(feature_key, context, project_api_key)
|
137
|
+
begin
|
138
|
+
# Create evaluate request using generated client
|
139
|
+
evaluate_request = TogglrClient::EvaluateRequest.new(context.to_h)
|
140
|
+
response = @api_client.evaluate_feature(feature_key, evaluate_request)
|
141
|
+
|
142
|
+
[response.value, response.enabled, true, nil]
|
143
|
+
rescue TogglrClient::ApiError => e
|
144
|
+
case e.code
|
145
|
+
when 404
|
146
|
+
['', false, false, nil] # Feature not found, not an error
|
147
|
+
when 401
|
148
|
+
[nil, nil, nil, UnauthorizedError.new('Authentication required')]
|
149
|
+
when 400
|
150
|
+
[nil, nil, nil, BadRequestError.new('Bad request')]
|
151
|
+
when 500
|
152
|
+
[nil, nil, nil, InternalServerError.new('Internal server error')]
|
153
|
+
else
|
154
|
+
[nil, nil, nil, APIError.new(e.code.to_s, e.message, e.code)]
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def with_retries(max_tries:)
|
160
|
+
retries = 0
|
161
|
+
begin
|
162
|
+
yield
|
163
|
+
rescue StandardError => e
|
164
|
+
retries += 1
|
165
|
+
raise e unless retries < max_tries && should_retry?(e)
|
166
|
+
|
167
|
+
delay = calculate_backoff_delay(retries)
|
168
|
+
@config.logger.debug('retrying after delay', attempt: retries, delay: delay)
|
169
|
+
sleep(delay)
|
170
|
+
retry
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def should_retry?(error)
|
175
|
+
case error
|
176
|
+
when NetworkError, TimeoutError, InternalServerError
|
177
|
+
true
|
178
|
+
else
|
179
|
+
false
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def calculate_backoff_delay(attempt)
|
184
|
+
delay = @config.backoff.base_delay
|
185
|
+
(attempt - 1).times do
|
186
|
+
delay = [delay * @config.backoff.factor, @config.backoff.max_delay].min
|
187
|
+
end
|
188
|
+
delay
|
189
|
+
end
|
190
|
+
|
191
|
+
def report_error_with_retries(feature_key, error_type, error_message, context)
|
192
|
+
with_retries(max_tries: @config.retries + 1) do
|
193
|
+
error = report_error_single(feature_key, error_type, error_message, context)
|
194
|
+
raise error if error
|
195
|
+
nil # Success
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def report_error_single(feature_key, error_type, error_message, context)
|
200
|
+
begin
|
201
|
+
error_report = TogglrClient::FeatureErrorReport.new(
|
202
|
+
error_type: error_type,
|
203
|
+
error_message: error_message,
|
204
|
+
context: context
|
205
|
+
)
|
206
|
+
|
207
|
+
@api_client.report_feature_error(feature_key, error_report)
|
208
|
+
# Success - error queued for processing
|
209
|
+
nil
|
210
|
+
rescue TogglrClient::ApiError => e
|
211
|
+
case e.code
|
212
|
+
when 401
|
213
|
+
UnauthorizedError.new('Authentication required')
|
214
|
+
when 400
|
215
|
+
BadRequestError.new('Bad request')
|
216
|
+
when 404
|
217
|
+
FeatureNotFoundError.new("Feature #{feature_key} not found")
|
218
|
+
when 500
|
219
|
+
InternalServerError.new('Internal server error')
|
220
|
+
else
|
221
|
+
APIError.new(e.code.to_s, e.message, e.code)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def get_feature_health_with_retries(feature_key)
|
227
|
+
with_retries(max_tries: @config.retries + 1) do
|
228
|
+
get_feature_health_single(feature_key)
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
def get_feature_health_single(feature_key)
|
233
|
+
begin
|
234
|
+
api_health = @api_client.get_feature_health(feature_key)
|
235
|
+
health = convert_feature_health(api_health)
|
236
|
+
[health, nil] # health, error
|
237
|
+
rescue TogglrClient::ApiError => e
|
238
|
+
case e.code
|
239
|
+
when 401
|
240
|
+
[nil, UnauthorizedError.new('Authentication required')]
|
241
|
+
when 400
|
242
|
+
[nil, BadRequestError.new('Bad request')]
|
243
|
+
when 404
|
244
|
+
[nil, FeatureNotFoundError.new("Feature #{feature_key} not found")]
|
245
|
+
when 500
|
246
|
+
[nil, InternalServerError.new('Internal server error')]
|
247
|
+
else
|
248
|
+
[nil, APIError.new(e.code.to_s, e.message, e.code)]
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
private
|
254
|
+
|
255
|
+
def convert_feature_health(api_health)
|
256
|
+
FeatureHealth.new(
|
257
|
+
feature_key: api_health.feature_key,
|
258
|
+
environment_key: api_health.environment_key,
|
259
|
+
enabled: api_health.enabled || false,
|
260
|
+
auto_disabled: api_health.auto_disabled || false,
|
261
|
+
error_rate: api_health.error_rate || 0,
|
262
|
+
threshold: api_health.threshold || 0,
|
263
|
+
last_error_at: api_health.last_error_at
|
264
|
+
)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Togglr
|
2
|
+
class Config
|
3
|
+
attr_accessor :base_url, :api_key, :timeout, :retries, :backoff,
|
4
|
+
:cache_enabled, :cache_size, :cache_ttl, :use_circuit_breaker,
|
5
|
+
:logger, :metrics, :max_connections, :insecure
|
6
|
+
|
7
|
+
def initialize(api_key)
|
8
|
+
@base_url = 'http://localhost:8090'
|
9
|
+
@api_key = api_key
|
10
|
+
@timeout = 0.8 # seconds
|
11
|
+
@retries = 2
|
12
|
+
@backoff = Backoff.new
|
13
|
+
@cache_enabled = false
|
14
|
+
@cache_size = 100
|
15
|
+
@cache_ttl = 5 # seconds
|
16
|
+
@use_circuit_breaker = false
|
17
|
+
@logger = NoOpLogger.new
|
18
|
+
@metrics = NoOpMetrics.new
|
19
|
+
@max_connections = 100
|
20
|
+
@insecure = false
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.default(api_key)
|
24
|
+
new(api_key)
|
25
|
+
end
|
26
|
+
|
27
|
+
class Backoff
|
28
|
+
attr_accessor :base_delay, :max_delay, :factor
|
29
|
+
|
30
|
+
def initialize
|
31
|
+
@base_delay = 0.1 # seconds
|
32
|
+
@max_delay = 2.0 # seconds
|
33
|
+
@factor = 2.0
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Togglr
|
2
|
+
class Error < StandardError; end
|
3
|
+
|
4
|
+
class UnauthorizedError < Error; end
|
5
|
+
class ForbiddenError < Error; end
|
6
|
+
class NotFoundError < Error; end
|
7
|
+
class TooManyRequestsError < Error; end
|
8
|
+
class NetworkError < Error; end
|
9
|
+
class TimeoutError < Error; end
|
10
|
+
class InvalidConfigError < Error; end
|
11
|
+
class FeatureNotFoundError < Error; end
|
12
|
+
class BadRequestError < Error; end
|
13
|
+
class InternalServerError < Error; end
|
14
|
+
|
15
|
+
class APIError < Error
|
16
|
+
attr_reader :code, :message, :status_code
|
17
|
+
|
18
|
+
def initialize(code, message, status_code)
|
19
|
+
@code = code
|
20
|
+
@message = message
|
21
|
+
@status_code = status_code
|
22
|
+
super(message)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Togglr
|
2
|
+
class Logger
|
3
|
+
def debug(msg, **kwargs); end
|
4
|
+
def info(msg, **kwargs); end
|
5
|
+
def warn(msg, **kwargs); end
|
6
|
+
def error(msg, **kwargs); end
|
7
|
+
end
|
8
|
+
|
9
|
+
class NoOpLogger < Logger
|
10
|
+
# No-op implementation
|
11
|
+
end
|
12
|
+
|
13
|
+
class StdoutLogger < Logger
|
14
|
+
def debug(msg, **kwargs)
|
15
|
+
puts "[DEBUG] #{msg} #{format_kwargs(kwargs)}"
|
16
|
+
end
|
17
|
+
|
18
|
+
def info(msg, **kwargs)
|
19
|
+
puts "[INFO] #{msg} #{format_kwargs(kwargs)}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def warn(msg, **kwargs)
|
23
|
+
puts "[WARN] #{msg} #{format_kwargs(kwargs)}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def error(msg, **kwargs)
|
27
|
+
puts "[ERROR] #{msg} #{format_kwargs(kwargs)}"
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def format_kwargs(kwargs)
|
33
|
+
return '' if kwargs.empty?
|
34
|
+
|
35
|
+
kwargs.map { |k, v| "#{k}=#{v}" }.join(' ')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Togglr
|
2
|
+
class Metrics
|
3
|
+
def inc_evaluate_request; end
|
4
|
+
def inc_evaluate_error(code); end
|
5
|
+
def observe_evaluate_latency(duration); end
|
6
|
+
def inc_cache_hit; end
|
7
|
+
def inc_cache_miss; end
|
8
|
+
end
|
9
|
+
|
10
|
+
class NoOpMetrics < Metrics
|
11
|
+
# No-op implementation
|
12
|
+
end
|
13
|
+
|
14
|
+
class StdoutMetrics < Metrics
|
15
|
+
def initialize
|
16
|
+
@counters = Hash.new(0)
|
17
|
+
end
|
18
|
+
|
19
|
+
def inc_evaluate_request
|
20
|
+
@counters[:evaluate_requests] += 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def inc_evaluate_error(code)
|
24
|
+
@counters[:"evaluate_errors_#{code}"] += 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def observe_evaluate_latency(duration)
|
28
|
+
@counters[:total_latency] += duration
|
29
|
+
@counters[:latency_count] += 1
|
30
|
+
end
|
31
|
+
|
32
|
+
def inc_cache_hit
|
33
|
+
@counters[:cache_hits] += 1
|
34
|
+
end
|
35
|
+
|
36
|
+
def inc_cache_miss
|
37
|
+
@counters[:cache_misses] += 1
|
38
|
+
end
|
39
|
+
|
40
|
+
def stats
|
41
|
+
@counters.dup
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Togglr
|
2
|
+
# Model for error reporting
|
3
|
+
class ErrorReport
|
4
|
+
attr_accessor :error_type, :error_message, :context
|
5
|
+
|
6
|
+
def initialize(error_type, error_message, context = {})
|
7
|
+
@error_type = error_type
|
8
|
+
@error_message = error_message
|
9
|
+
@context = context
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_h
|
13
|
+
{
|
14
|
+
error_type: @error_type,
|
15
|
+
error_message: @error_message,
|
16
|
+
context: @context
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.new_with_context(error_type, error_message, context = {})
|
21
|
+
new(error_type, error_message, context)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Model for feature health information
|
26
|
+
class FeatureHealth
|
27
|
+
attr_accessor :feature_key, :environment_key, :enabled, :auto_disabled,
|
28
|
+
:error_rate, :threshold, :last_error_at
|
29
|
+
|
30
|
+
def initialize(data = {})
|
31
|
+
@feature_key = data['feature_key']
|
32
|
+
@environment_key = data['environment_key']
|
33
|
+
@enabled = data['enabled']
|
34
|
+
@auto_disabled = data['auto_disabled']
|
35
|
+
@error_rate = data['error_rate']
|
36
|
+
@threshold = data['threshold']
|
37
|
+
@last_error_at = data['last_error_at']
|
38
|
+
end
|
39
|
+
|
40
|
+
def healthy?
|
41
|
+
!@auto_disabled && @enabled
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_h
|
45
|
+
{
|
46
|
+
feature_key: @feature_key,
|
47
|
+
environment_key: @environment_key,
|
48
|
+
enabled: @enabled,
|
49
|
+
auto_disabled: @auto_disabled,
|
50
|
+
error_rate: @error_rate,
|
51
|
+
threshold: @threshold,
|
52
|
+
last_error_at: @last_error_at
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|