Skip to content

Commit bfff20d

Browse files
authored
feat: ability to provide custom http client (#75)
1 parent 7891005 commit bfff20d

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

lib/stream-chat/client.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ class Client
3030
#
3131
# @param [string] api_key your application api_key
3232
# @param [string] api_secret your application secret
33-
# @param [string]
34-
# @param [hash] options extra options
33+
# @param [float] timeout the timeout for the http requests
34+
# @param [hash] options extra options such as base_url
3535
#
3636
# @example initialized the client with a timeout setting
3737
# StreamChat::Client.new('my_key', 'my_secret', 3.0)
@@ -54,6 +54,19 @@ def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
5454
end
5555
end
5656

57+
# initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET
58+
# environmental variables.
59+
def self.from_env
60+
Client.new(ENV['STREAM_KEY'], ENV['STREAM_SECRET'])
61+
end
62+
63+
# Sets the underlying Faraday http client.
64+
#
65+
# @param [client] an instance of Faraday::Connection
66+
def set_http_client(client)
67+
@conn = client
68+
end
69+
5770
def create_token(user_id, exp = nil, iat = nil)
5871
payload = { user_id: user_id }
5972
payload['exp'] = exp unless exp.nil?
@@ -526,10 +539,9 @@ def make_http_request(method, relative_url, params: nil, data: nil)
526539
headers = get_default_headers
527540
headers['Authorization'] = @auth_token
528541
headers['stream-auth-type'] = 'jwt'
529-
url = [@base_url, relative_url].join('/')
530542
params = {} if params.nil?
531543
params = (get_default_params.merge(params).sort_by { |k, _v| k.to_s }).to_h
532-
url = "#{url}?#{URI.encode_www_form(params)}"
544+
url = "#{relative_url}?#{URI.encode_www_form(params)}"
533545

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

spec/channel_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def loop_times(times)
1919
end
2020

2121
before(:all) do
22-
@client = StreamChat::Client.new(ENV['STREAM_KEY'], ENV['STREAM_SECRET'], base_url: ENV['STREAM_CHAT_URL'])
22+
@client = StreamChat::Client.from_env
2323
end
2424

2525
before(:each) do

spec/client_spec.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def loop_times(times)
2020
end
2121

2222
before(:all) do
23-
@client = StreamChat::Client.new(ENV['STREAM_KEY'], ENV['STREAM_SECRET'], base_url: ENV['STREAM_CHAT_URL'])
23+
@client = StreamChat::Client.from_env
2424

2525
@fellowship_of_the_ring = [
2626
{ id: 'frodo-baggins', name: 'Frodo Baggins', race: 'Hobbit', age: 50 },
@@ -43,6 +43,17 @@ def loop_times(times)
4343
@client.update_users(@random_users)
4444
end
4545

46+
it 'properly sets up a new client' do
47+
client = StreamChat::Client.from_env
48+
49+
client.set_http_client(Faraday.new(url: 'https://getstream.io'))
50+
expect { client.get_app_settings }.to raise_error(StreamChat::StreamAPIException)
51+
52+
client.set_http_client(Faraday.new(url: 'https://chat.stream-io-api.com'))
53+
response = client.get_app_settings
54+
expect(response).to include 'app'
55+
end
56+
4657
it 'properly handles stream response class' do
4758
response = @client.get_app_settings
4859
expect(response.rate_limit.limit).to be > 0

0 commit comments

Comments
 (0)