wrapi 0.4.8 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f311f0c8d5a16deead39d219d0d266202798df6cfb4d0b23cabfabd62d1cd9ec
4
- data.tar.gz: c9a7bba391164cad77f2e976ec24f7b9016e0840857a416886c81987c26015b3
3
+ metadata.gz: ed925af63ad49be5f7489b5af9a2702e755ab08b8fbdc5bfac74f11041488733
4
+ data.tar.gz: 818fda8bdea923c971cf2ea8b7854c7728df019ec922721ea800197d47a5d40b
5
5
  SHA512:
6
- metadata.gz: 934bfa4e242147dcad243f8bfd6ce3c3389bea2e55d85f9444aa8c082c20b103da16fd23ff9afcad655832de37c8271ead765433e7b092669247e844a5603ba7
7
- data.tar.gz: 1395f62ad20197b83a1a4f6159de38614131e3d3064229a47b77a3776c0ff251786b27b0c5978d7823ce40c2089e695b1938ca0e50bb7efff4ba4197a47e6100
6
+ metadata.gz: c7bbbfb4c65764f2a2a4a8f2ae8a0536d7a2eb606db91d2903e640f44f9b055de9317936a238b82cff6caf778c283037d10fe6b3e5587e58dfb11ac4fad3e636
7
+ data.tar.gz: 5d425345109615657aff5e211c34d4c7adc0311953daa49bc96567f4d394f4a46480ed2a1f7f7c3f80aee020e67b2c8e88107c5334e53a8a99218f0978076fca
data/CHANGELOG.md CHANGED
@@ -1,24 +1,31 @@
1
- ## Changelog
1
+ # Changelog
2
2
 
3
3
  ## [0.1.0] - 2024-02-2
4
+
4
5
  - Initial release extracted from CloudAlly gem
5
6
 
6
7
  ## [0.1.1] - 2024-02-5
8
+
7
9
  - default endpoint to nil and raise error in connection when not available
8
10
 
9
11
  ## [0.1.2] - 2024-02-5
12
+
10
13
  - default endpoint to nil and raise error in connection when not available
11
14
 
12
15
  ## [0.1.3] - 2024-02-5
16
+
13
17
  - fix entity should return empty array insted of nil
14
18
 
15
19
  ## [0.2.0] - 2024-02-8
20
+
16
21
  - implement json pagination
17
22
 
18
23
  ## [0.2.0] - 2024-02-13
24
+
19
25
  - implement option to manipulate request
20
26
 
21
27
  ## [0.4.0] - 2024-02-13
28
+
22
29
  - testing/code quality
23
30
  authentication tests (mocked)
24
31
  test string/{}/[] entities returned with mock
@@ -27,25 +34,37 @@
27
34
  Request option to return raw response
28
35
 
29
36
  ## [0.4.1] - 2024-02-28
37
+
30
38
  - fix issue with post body only supported as json
31
39
 
32
40
  ## [0.4.2] - 2024-03-03
41
+
33
42
  - fix issue with escaping query parameters included in path
34
43
 
35
44
  ## [0.4.3] - 2024-03-07
45
+
36
46
  - fix issue json generation for updated attributes
37
47
 
38
48
  ## [0.4.4] - 2024-03-12
49
+
39
50
  - fix typo and implement clone for entities
40
51
 
41
52
  ## [0.4.5] - 2024-03-12
53
+
42
54
  - refactorings code readability
43
55
 
44
56
  ## [0.4.6] - 2024-06-17
57
+
45
58
  - fix issue with loading Entity from yaml
46
59
 
47
60
  ## [0.4.7] - 2025-03-18
61
+
48
62
  - fix obsolete escape
49
63
 
50
64
  ## [0.4.8] - 2025-03-20
65
+
51
66
  - scramble passwords in www encoded content
67
+
68
+ ## [0.4.9] - 2025-03-20
69
+
70
+ - freeze causes issue so connection options can't be merged!
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 *WrAPI::Configuration::VALID_OPTIONS_KEYS
26
+ attr_accessor(*WrAPI::Configuration::VALID_OPTIONS_KEYS)
27
27
 
28
28
  # Initializes a new API object with the given options.
29
29
  #
@@ -62,7 +62,11 @@ module WrAPI
62
62
  ].freeze
63
63
 
64
64
  # By default, don't set any connection options
65
- DEFAULT_CONNECTION_OPTIONS = {}.freeze
65
+ DEFAULT_CONNECTION_OPTIONS = {}
66
+
67
+
68
+ # By default token type used in authorizaion header
69
+ DEFAULT_TOKEN_TYPE = 'Bearer'
66
70
 
67
71
  # The response format appended to the path and sent in the 'Accept' header if none is set
68
72
  #
@@ -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 *VALID_OPTIONS_KEYS
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
@@ -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'] = "Bearer #{access_token}" if access_token
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WrAPI
4
- VERSION = '0.4.8'
4
+ VERSION = '0.5.0'
5
5
  end
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.8
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-03-20 00:00:00.000000000 Z
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