Skip to content

Commit

Permalink
feat: ability to provide custom http client
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdeme committed Feb 1, 2022
1 parent 7891005 commit 38cebb6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
20 changes: 16 additions & 4 deletions lib/stream-chat/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class Client
#
# @param [string] api_key your application api_key
# @param [string] api_secret your application secret
# @param [string]
# @param [hash] options extra options
# @param [float] timeout the timeout for the http requests
# @param [hash] options extra options such as base_url
#
# @example initialized the client with a timeout setting
# StreamChat::Client.new('my_key', 'my_secret', 3.0)
Expand All @@ -54,6 +54,19 @@ def initialize(api_key = '', api_secret = '', timeout = 6.0, **options)
end
end

# initializes a Stream Chat API Client from STREAM_KEY and STREAM_SECRET
# environmental variables.
def self.from_env
Client.new(ENV['STREAM_KEY'], ENV['STREAM_SECRET'])
end

# Sets the underlying Faraday http client.
#
# @param [client] an instance of Faraday::Connection
def set_http_client(client)
@conn = client
end

def create_token(user_id, exp = nil, iat = nil)
payload = { user_id: user_id }
payload['exp'] = exp unless exp.nil?
Expand Down Expand Up @@ -526,10 +539,9 @@ def make_http_request(method, relative_url, params: nil, data: nil)
headers = get_default_headers
headers['Authorization'] = @auth_token
headers['stream-auth-type'] = 'jwt'
url = [@base_url, relative_url].join('/')
params = {} if params.nil?
params = (get_default_params.merge(params).sort_by { |k, _v| k.to_s }).to_h
url = "#{url}?#{URI.encode_www_form(params)}"
url = "#{relative_url}?#{URI.encode_www_form(params)}"

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

Expand Down
2 changes: 1 addition & 1 deletion spec/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def loop_times(times)
end

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

before(:each) do
Expand Down
13 changes: 12 additions & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def loop_times(times)
end

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

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

it 'properly sets up a new client' do
client = StreamChat::Client.from_env

client.set_http_client(Faraday.new(url: 'https://getstream.io'))
expect { client.get_app_settings }.to raise_error(StreamChat::StreamAPIException)

client.set_http_client(Faraday.new(url: 'https://chat.stream-io-api.com'))
response = client.get_app_settings
expect(response).to include 'app'
end

it 'properly handles stream response class' do
response = @client.get_app_settings
expect(response.rate_limit.limit).to be > 0
Expand Down

0 comments on commit 38cebb6

Please sign in to comment.