Skip to content

Commit ec9a308

Browse files
authored
Support GetRateLimits endpoint (#41)
1 parent d6308ba commit ec9a308

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ status_response = client.get_export_channel_status(response['task_id'])
189189
# status_response['status'] == 'pending', 'completed'
190190
```
191191

192+
### Rate limits
193+
```ruby
194+
# Get all rate limits
195+
limits = client.get_rate_limits
196+
197+
# Get rate limits for specific platform(s)
198+
limits = client.get_rate_limits(server_side: true)
199+
200+
# Get rate limits for specific platforms and endpoints
201+
limits = client.get_rate_limits(android: true, ios: true, endpoints: ['QueryChannels', 'SendMessage'])
202+
```
203+
192204
### Example Rails application
193205

194206
See [an example rails application using the Ruby SDK](https://github.com/GetStream/rails-chat-example).

lib/stream-chat/client.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,17 @@ def get_devices(user_id)
232232
get('devices', params: { user_id: user_id })
233233
end
234234

235+
def get_rate_limits(server_side: false, android: false, ios: false, web: false, endpoints: [])
236+
params = {}
237+
params['server_side'] = server_side if server_side
238+
params['android'] = android if android
239+
params['ios'] = ios if ios
240+
params['web'] = web if web
241+
params['endpoints'] = endpoints.join(',') unless endpoints.empty?
242+
243+
get('rate_limits', params: params)
244+
end
245+
235246
def verify_webhook(request_body, x_signature)
236247
signature = OpenSSL::HMAC.hexdigest('SHA256', @api_secret, request_body)
237248
signature == x_signature
@@ -342,7 +353,7 @@ def make_http_request(method, relative_url, params: nil, data: nil)
342353
headers['stream-auth-type'] = 'jwt'
343354
url = [@base_url, relative_url].join('/')
344355
params = params.nil? ? {} : params
345-
params = Hash[get_default_params.merge(params).sort_by { |k, _v| k.to_s }]
356+
params = (get_default_params.merge(params).sort_by { |k, _v| k.to_s }).to_h
346357
url = "#{url}?#{URI.encode_www_form(params)}"
347358

348359
body = data.to_json if %w[patch post put].include? method.to_s

spec/client_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,36 @@
211211
expect(response['devices'].length).to eq 1
212212
end
213213

214+
describe 'get rate limits' do
215+
it 'lists all limits' do
216+
response = @client.get_rate_limits
217+
expect(response['android']).not_to be_nil
218+
expect(response['ios']).not_to be_nil
219+
expect(response['web']).not_to be_nil
220+
expect(response['server_side']).not_to be_nil
221+
end
222+
223+
it 'lists limits for a single platform' do
224+
response = @client.get_rate_limits(server_side: true)
225+
expect(response['server_side']).not_to be_nil
226+
expect(response['android']).to be_nil
227+
expect(response['ios']).to be_nil
228+
expect(response['web']).to be_nil
229+
end
230+
231+
it 'lists limits for a few endpoints' do
232+
response = @client.get_rate_limits(server_side: true, android: true, endpoints: %w[GetRateLimits QueryChannels])
233+
expect(response['ios']).to be_nil
234+
expect(response['web']).to be_nil
235+
expect(response['android']).not_to be_nil
236+
expect(response['android'].length).to eq(2)
237+
expect(response['android']['GetRateLimits']['limit']).to eq(response['android']['GetRateLimits']['remaining'])
238+
expect(response['server_side']).not_to be_nil
239+
expect(response['server_side'].length).to eq(2)
240+
expect(response['server_side']['GetRateLimits']['limit']).to be > response['server_side']['GetRateLimits']['remaining']
241+
end
242+
end
243+
214244
it 'search for messages' do
215245
text = SecureRandom.uuid
216246
@channel.send_message({ text: text }, 'legolas')

0 commit comments

Comments
 (0)