api_key_manager 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11683c2f8e53a827dc507f85c5a87d4a0cae2a8019dd369831f8aaf3270009fd
4
+ data.tar.gz: 3a9631cc13a0610c44e52ae852b473aeef854829cccc05bc5bb0d155bdde9248
5
+ SHA512:
6
+ metadata.gz: 1781509a546e698ecc42f4396d318236cd86b7310581820dc73bc2d3331b4d301856a0de0f1eddc7f469e1f011f587c66f00e9f5d1cf9193ca98b9a26359053e
7
+ data.tar.gz: 6fc121c1de40e2a2c6d1811bce1745c9d268b974247088961a86b706fcb969ea82964a1dff2d1b056df43fa7c35f8903be600cd60c6f99e985b210e5db5fae32
data/.envrc ADDED
@@ -0,0 +1 @@
1
+ export RR=`pwd`
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-09-29
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Dewayne VanHoozer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # ApiKeyManager
2
+
3
+ Manage one or more API keys to the same resource that are rate limited.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ bundle add api_key_manager
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ gem install api_key_manager
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ API = ApiKeyManager::RateLimited.new(
19
+ api_keys: ENV['API_KEYS'],
20
+ delay: ENV['API_RATE_DELAY'],
21
+ rate_count: EMV['API_RATE_COUNT'],
22
+ rate_period: ENV['API_RATE_PERIOD']
23
+ )
24
+ #
25
+ API.key
26
+ ```
27
+
28
+ Now everytime `API.key` is called the ApiKeyManager::RateLimted` class complies with the rate limits. When `delay:` is set to a TRUE value, if the limited count has already been reached then `API.key` will block until the limit period has been passed.
29
+
30
+ If you are accessing a rate limited API without a wrpaaer library - for example just using Faraday ...
31
+
32
+ ```ruby
33
+ CONNECTION = Faraday.new(url: "https://example.com")
34
+ CONNECTION.get "/query?apikey=#{API.key}&q='xyzzy'"
35
+ CONNECTION.get "/query?apikey=#{API.key}&q='puff'"
36
+ ```
37
+
38
+ If you are using some kind of bad (e.g. one that does not honor rate limitations) wrapper library around an API resource, you can reset that library's client configuration.
39
+
40
+ ```ruby
41
+ config = BadWrapperLibrary.configuration
42
+ config.api_key = API.key
43
+ response = BadWrapperLibery.get_something
44
+ config.api_key = API.key
45
+ response = BadWrapperLibery.get_something_else
46
+ ```
47
+
48
+ It that weird case you should create your own meta wrapper around the bad wrapper library.
49
+
50
+ Be a good developer; honor the rate limits.
51
+
52
+ ## Development
53
+
54
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
55
+
56
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api_key_manager.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require 'rake/testtask'
5
+
6
+ task default: %i[]
7
+
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.pattern = 'tests/**/*_test.rb'
10
+ end
@@ -0,0 +1 @@
1
+ e4a1760e61b582a3344b54ff1dc6c3c749f50225af7df997a41ffc2db5242d607c2883a47ca1d8f1c2425edb44b1f449a61beac8c66c7642cf61061c055a06df
@@ -0,0 +1,53 @@
1
+ # api_key_manager/lib/api_key_manager/rate_limited.rb
2
+
3
+ class ApiKeyManager::RateLimited
4
+
5
+ def initialize(api_keys:, delay: false, rate_count: 5, rate_period: 60)
6
+ @api_keys = api_keys.is_a?(String) ? api_keys.split(',') : api_keys
7
+ @delay = delay
8
+ @rate_count = rate_count.is_a?(String) ? rate_count.to_i : rate_count
9
+ @rate_period = rate_period.is_a?(String) ? rate_period.to_i : rate_period
10
+ @start_timer = Time.now.to_i
11
+ @end_timer = @start_timer - 1 # prevent delay
12
+ @counter = @rate_count
13
+ @current_index = 0
14
+ end
15
+
16
+
17
+ def reset_counter
18
+ @counter = @rate_count
19
+ end
20
+
21
+
22
+ def reset_timer
23
+ now = Time.now.to_i
24
+
25
+ if @delay && now < @end_timer
26
+ delta = @end_timer - now
27
+ sleep(delta) # NOTE: Will block IO process
28
+ now = Time.now.to_i
29
+ end
30
+
31
+ @start_timer = now
32
+ @end_timer = @start_timer + @rate_period
33
+ end
34
+
35
+
36
+ def api_key
37
+ now = Time.now.to_i
38
+
39
+ if now <= @end_timer && @counter < 1
40
+ @current_index = (@current_index + 1) % @api_keys.length
41
+ reset_timer
42
+ reset_counter
43
+ elsif now > @end_timer
44
+ # Continue using same api key
45
+ reset_timer
46
+ reset_counter
47
+ end
48
+
49
+ @counter -= 1
50
+ @api_keys[@current_index]
51
+ end
52
+ alias_method :key, :api_key
53
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ApiKeyManager
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,6 @@
1
+ # api_key_manager/lib/api_key_manager.rb
2
+
3
+ module ApiKeyManager; end
4
+
5
+ require_relative "api_key_manager/version"
6
+ require_relative "api_key_manager/rate_limited"
@@ -0,0 +1,71 @@
1
+ # api_key_manager/tests/api_key_manager_test.rb
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../lib/api_key_manager'
5
+
6
+ class TestRate < Minitest::Test
7
+ def setup
8
+ @api_keys = ['key1', 'key2', 'key3']
9
+ end
10
+
11
+ def test_api_key_returns_a_key
12
+ rate = ApiKeyManager::RateLimited.new(api_keys: @api_keys)
13
+
14
+ assert_includes @api_keys, rate.api_key
15
+ end
16
+
17
+
18
+ def test_api_key_returns_a_new_key_after_rate_count
19
+ rate = ApiKeyManager::RateLimited.new(api_keys: @api_keys, rate_count: 2)
20
+
21
+ first_key = rate.api_key
22
+ second_key = rate.api_key
23
+ third_key = rate.api_key
24
+
25
+ assert_equal @api_keys[0], first_key
26
+ assert_equal @api_keys[0], second_key
27
+ assert_equal @api_keys[1], third_key
28
+ end
29
+
30
+
31
+ def test_api_key_returns_a_new_key_after_rate_period
32
+ rate = ApiKeyManager::RateLimited.new(api_keys: @api_keys, rate_period: 3)
33
+
34
+ first_key = rate.api_key
35
+ second_key = rate.api_key
36
+
37
+ sleep(4)
38
+
39
+ third_key = rate.api_key
40
+
41
+ assert_equal @api_keys[0], first_key
42
+ assert_equal @api_keys[0], second_key
43
+ assert_equal @api_keys[0], third_key
44
+ end
45
+
46
+
47
+ def test_api_key_returns_different_key_after_period_if_delay_is_true
48
+ rate_period = 5
49
+ rate = ApiKeyManager::RateLimited.new(
50
+ api_keys: @api_keys,
51
+ delay: true,
52
+ rate_count: 1,
53
+ rate_period: rate_period
54
+ )
55
+
56
+ start_time = Time.now.to_i
57
+
58
+ first_key = rate.api_key
59
+ second_key = rate.api_key
60
+
61
+ end_time = Time.now.to_i
62
+ delta_time = end_time - start_time
63
+
64
+ # show that the 2nd time was blocked until end of period
65
+ # has expired.
66
+ assert delta_time >= rate_period
67
+
68
+ assert_equal @api_keys[0], first_key
69
+ assert_equal @api_keys[1], second_key
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api_key_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dewayne VanHoozer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Manage one or more API keys to the same resource that is rate limited.
56
+ email:
57
+ - dvanhoozer@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".envrc"
63
+ - CHANGELOG.md
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - checksums/api_key_manager-0.1.0.gem.sha512
68
+ - lib/api_key_manager.rb
69
+ - lib/api_key_manager/rate_limited.rb
70
+ - lib/api_key_manager/version.rb
71
+ - tests/api_key_manager_test.rb
72
+ homepage: https://github.com/MadBomber/api_key_manager
73
+ licenses:
74
+ - MIT
75
+ metadata:
76
+ allowed_push_host: https://rubygems.org
77
+ homepage_uri: https://github.com/MadBomber/api_key_manager
78
+ source_code_uri: https://github.com/MadBomber/api_key_manager
79
+ changelog_uri: https://github.com/MadBomber/api_key_manager
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 2.6.0
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubygems_version: 3.4.20
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Manage one or more API keys to the same resource that is rate limited.
99
+ test_files: []