wrapi 0.4.9 → 0.5.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 +4 -4
- data/lib/wrapi/api.rb +1 -1
- data/lib/wrapi/configuration.rb +7 -2
- data/lib/wrapi/connection.rb +2 -1
- data/lib/wrapi/middleware/rate_throttle_middleware.rb +72 -0
- data/lib/wrapi/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ed925af63ad49be5f7489b5af9a2702e755ab08b8fbdc5bfac74f11041488733
|
|
4
|
+
data.tar.gz: 818fda8bdea923c971cf2ea8b7854c7728df019ec922721ea800197d47a5d40b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c7bbbfb4c65764f2a2a4a8f2ae8a0536d7a2eb606db91d2903e640f44f9b055de9317936a238b82cff6caf778c283037d10fe6b3e5587e58dfb11ac4fad3e636
|
|
7
|
+
data.tar.gz: 5d425345109615657aff5e211c34d4c7adc0311953daa49bc96567f4d394f4a46480ed2a1f7f7c3f80aee020e67b2c8e88107c5334e53a8a99218f0978076fca
|
data/lib/wrapi/api.rb
CHANGED
|
@@ -23,7 +23,7 @@ module WrAPI
|
|
|
23
23
|
# @see WrAPI::Request
|
|
24
24
|
# @see WrAPI::Authentication
|
|
25
25
|
class API
|
|
26
|
-
attr_accessor
|
|
26
|
+
attr_accessor(*WrAPI::Configuration::VALID_OPTIONS_KEYS)
|
|
27
27
|
|
|
28
28
|
# Initializes a new API object with the given options.
|
|
29
29
|
#
|
data/lib/wrapi/configuration.rb
CHANGED
|
@@ -64,6 +64,10 @@ module WrAPI
|
|
|
64
64
|
# By default, don't set any connection options
|
|
65
65
|
DEFAULT_CONNECTION_OPTIONS = {}
|
|
66
66
|
|
|
67
|
+
|
|
68
|
+
# By default token type used in authorizaion header
|
|
69
|
+
DEFAULT_TOKEN_TYPE = 'Bearer'
|
|
70
|
+
|
|
67
71
|
# The response format appended to the path and sent in the 'Accept' header if none is set
|
|
68
72
|
#
|
|
69
73
|
# @note JSON is the only available format at this time
|
|
@@ -81,7 +85,7 @@ module WrAPI
|
|
|
81
85
|
# It uses the DefaultPager class from the WrAPI::RequestPagination module.
|
|
82
86
|
DEFAULT_PAGINATION = WrAPI::RequestPagination::DefaultPager
|
|
83
87
|
|
|
84
|
-
attr_accessor
|
|
88
|
+
attr_accessor(*VALID_OPTIONS_KEYS)
|
|
85
89
|
|
|
86
90
|
# When this module is extended, set all configuration options to their default values
|
|
87
91
|
def self.extended(base)
|
|
@@ -103,7 +107,6 @@ module WrAPI
|
|
|
103
107
|
# Reset all configuration options to defaults
|
|
104
108
|
def reset
|
|
105
109
|
self.access_token = nil
|
|
106
|
-
self.token_type = nil
|
|
107
110
|
self.refresh_token = nil
|
|
108
111
|
self.token_expires = nil
|
|
109
112
|
self.client_id = nil
|
|
@@ -113,6 +116,8 @@ module WrAPI
|
|
|
113
116
|
self.endpoint = nil
|
|
114
117
|
|
|
115
118
|
self.logger = nil
|
|
119
|
+
|
|
120
|
+
self.token_type = DEFAULT_TOKEN_TYPE
|
|
116
121
|
self.connection_options = DEFAULT_CONNECTION_OPTIONS
|
|
117
122
|
self.format = DEFAULT_FORMAT
|
|
118
123
|
self.page_size = DEFAULT_PAGE_SIZE
|
data/lib/wrapi/connection.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'faraday'
|
|
4
|
+
require File.expand_path('middleware/rate_throttle_middleware', __dir__)
|
|
4
5
|
|
|
5
6
|
module WrAPI
|
|
6
7
|
# @private
|
|
@@ -73,7 +74,7 @@ module WrAPI
|
|
|
73
74
|
# @return [void]
|
|
74
75
|
# @note The authorization header will only be set if the access_token is present.
|
|
75
76
|
def setup_authorization(connection)
|
|
76
|
-
connection.headers['Authorization'] = "
|
|
77
|
+
connection.headers['Authorization'] = "#{token_type} #{access_token}" if access_token && token_type
|
|
77
78
|
end
|
|
78
79
|
|
|
79
80
|
# Sets up the headers for the given connection. Override to set own headers.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'faraday'
|
|
4
|
+
|
|
5
|
+
module WrAPI
|
|
6
|
+
# A Faraday middleware for rate limiting requests.
|
|
7
|
+
#
|
|
8
|
+
# This middleware ensures that the number of requests made through a Faraday connection
|
|
9
|
+
# does not exceed a specified limit within a given time period.
|
|
10
|
+
#
|
|
11
|
+
# @example Add middleware to a Faraday connection
|
|
12
|
+
# connection = Faraday.new(url: 'https://api.example.com') do |faraday|
|
|
13
|
+
# faraday.use RateThrottleMiddleware, limit: 300, period: 60
|
|
14
|
+
# faraday.adapter Faraday.default_adapter
|
|
15
|
+
# end
|
|
16
|
+
#
|
|
17
|
+
# @see https://github.com/lostisland/faraday Faraday Documentation
|
|
18
|
+
#
|
|
19
|
+
class RateThrottleMiddleware < Faraday::Middleware
|
|
20
|
+
# Initializes the RateThrottleMiddleware.
|
|
21
|
+
#
|
|
22
|
+
# @param app [#call] The next middleware or the actual Faraday adapter.
|
|
23
|
+
# @param limit [Integer] The maximum number of requests allowed within the specified period. Default is 300.
|
|
24
|
+
# @param period [Integer] The time period in seconds over which the limit applies. Default is 60 seconds.
|
|
25
|
+
#
|
|
26
|
+
# @example
|
|
27
|
+
# middleware = RateThrottleMiddleware.new(app, limit: 300, period: 60)
|
|
28
|
+
#
|
|
29
|
+
def initialize(app, limit: 300, period: 60)
|
|
30
|
+
super(app)
|
|
31
|
+
@limit = limit
|
|
32
|
+
@period = period
|
|
33
|
+
@requests = []
|
|
34
|
+
@mutex = Mutex.new
|
|
35
|
+
@condition = ConditionVariable.new
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def call(env)
|
|
39
|
+
throttle_request
|
|
40
|
+
@app.call(env)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def throttle_request
|
|
46
|
+
@mutex.synchronize do
|
|
47
|
+
now = Time.now.to_f
|
|
48
|
+
remove_expired_requests(now)
|
|
49
|
+
|
|
50
|
+
rate_limited(now)
|
|
51
|
+
|
|
52
|
+
# Record the new request
|
|
53
|
+
@requests.push(Time.now.to_f)
|
|
54
|
+
@condition.broadcast
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def remove_expired_requests(now)
|
|
59
|
+
# Clear requests older than the rate limit period
|
|
60
|
+
@requests.pop while !@requests.empty? && @requests[0] < (now - @period)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def rate_limited(now)
|
|
64
|
+
# Wait if the request limit is reached
|
|
65
|
+
while @requests.size >= @limit
|
|
66
|
+
sleep_time = @requests[0] + @period - now
|
|
67
|
+
@condition.wait(@mutex, sleep_time) if sleep_time.positive?
|
|
68
|
+
remove_expired_requests(Time.now.to_f)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
data/lib/wrapi/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wrapi
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Janco Tanis
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date: 2025-
|
|
10
|
+
date: 2025-10-29 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: faraday
|
|
@@ -96,6 +96,7 @@ files:
|
|
|
96
96
|
- lib/wrapi/configuration.rb
|
|
97
97
|
- lib/wrapi/connection.rb
|
|
98
98
|
- lib/wrapi/entity.rb
|
|
99
|
+
- lib/wrapi/middleware/rate_throttle_middleware.rb
|
|
99
100
|
- lib/wrapi/pagination.rb
|
|
100
101
|
- lib/wrapi/request.rb
|
|
101
102
|
- lib/wrapi/respond_to.rb
|