Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update_user example fails #11

Closed
the-bass opened this issue Mar 25, 2020 · 10 comments
Closed

update_user example fails #11

the-bass opened this issue Mar 25, 2020 · 10 comments
Assignees
Labels
bug Something isn't working

Comments

@the-bass
Copy link

Running

client.update_user({
  'id' => 'bob-1',
  'role' => 'admin',
  'name' => 'Robert Tables'
})

, as seen in the README, fails with error users[] is a required field.

Details of what I did:

% docker run -it --rm ruby:2.5 bash
root@2d2208282bd1:/# gem install stream-chat-ruby
Fetching stream-chat-ruby-1.1.0.gem
Fetching faraday-1.0.0.gem
Fetching jwt-2.2.1.gem
Fetching multipart-post-2.1.1.gem
Successfully installed multipart-post-2.1.1
Successfully installed faraday-1.0.0
Successfully installed jwt-2.2.1
Successfully installed stream-chat-ruby-1.1.0
4 gems installed
root@2d2208282bd1:/# irb
irb(main):001:0> require 'stream-chat'
=> true
irb(main):002:0> client = StreamChat::Client.new(api_key="abcdefg", api_secret="12346789jeie")
=> #<StreamChat::Client:0x000055b47099a528 @api_key="abcdefg", @api_secret="12346789jeie", @timeout=6.0, @options={}, @auth_token="mklelkmqlkfmql", @base_url="https://chat-us-east-1.stream-io-api.com", @conn=#<Faraday::Connection:0x000055b470999808 @parallel_manager=nil, @headers={"User-Agent"=>"Faraday v1.0.0"}, @params={}, @options=#<Faraday::RequestOptions timeout=6.0, open_timeout=6.0>, @ssl=#<Faraday::SSLOptions (empty)>, @default_parallel_manager=nil, @builder=#<Faraday::RackBuilder:0x000055b470999358 @adapter=Faraday::Adapter::NetHttp, @handlers=[Faraday::Request::Multipart]>, @url_prefix=#<URI::HTTPS https://chat-us-east-1.stream-io-api.com/>, @manual_proxy=false, @proxy=nil>>
irb(main):003:0> client.update_user({
irb(main):004:2*     'id' => 'bob-1',
irb(main):005:2*     'role' => 'admin',
irb(main):006:2*     'name' => 'Robert Tables'
irb(main):007:2> })
#<Faraday::Response:0x000055b470ad6220 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:post @request_body="{\"users\":{\"\":{\"id\":\"bob-1\",\"role\":\"admin\",\"name\":\"Robert Tables\"}}}" @url=#<URI::HTTPS https://chat-us-east-1.stream-io-api.com/users?api_key=abcdefg> @request=#<Faraday::RequestOptions timeout=6.0, open_timeout=6.0> @request_headers={"User-Agent"=>"Faraday v1.0.0", "Content-type"=>"application/json", "X-stream-client"=>"stream-ruby-client-1.1.0", "Authorization"=>"mklelkmqlkfmql", "stream-auth-type"=>"jwt"} @ssl=#<Faraday::SSLOptions verify=true> @response=#<Faraday::Response:0x000055b470ad6220 ...> @response_headers={"server"=>"fasthttp", "date"=>"Wed, 25 Mar 2020 10:07:52 GMT", "content-type"=>"application/json;charset=utf-8", "content-length"=>"183", "x-ratelimit-limit"=>"300", "access-control-allow-origin"=>"*", "access-control-max-age"=>"86400", "cache-control"=>"no-cache", "access-control-allow-headers"=>"x-requested-with, content-type, accept, origin, authorization, x-csrftoken, x-stream-client, stream-auth-type", "access-control-allow-methods"=>"GET, POST, PUT, PATCH, DELETE, OPTIONS", "x-ratelimit-remaining"=>"299", "x-ratelimit-reset"=>"1585130880", "connection"=>"close"} @status=400 @reason_phrase="Bad Request" @response_body="{\"code\":4,\"message\":\"UpdateUsers failed with error: \\\"users[] is a required field\\\"\",\"exception_fields\":{\"users[]\":\"users[] is a required field\"},\"StatusCode\":400,\"duration\":\"0.00ms\"}">>
Traceback (most recent call last):
        7: from /usr/local/bin/irb:11:in `<main>'
        6: from (irb):3
        5: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:101:in `update_user'
        4: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:97:in `update_users'
        3: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:254:in `post'
        2: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:336:in `make_http_request'
        1: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:312:in `parse_response'
StreamChat::StreamAPIException (StreamChat::StreamAPIException)
@tbarbugli tbarbugli added the bug Something isn't working label Mar 25, 2020
@tbarbugli
Copy link
Member

thanks for reporting this, in the meantime can you try client.update_user([user])?

@the-bass
Copy link
Author

You mean like

client.update_user([{
  'id' => 'bob-1',
  'role' => 'admin',
  'name' => 'Robert Tables'
}])

?

Gives me

irb(main):004:0> client.update_user([{
irb(main):005:3*   'id' => 'bob-1',
irb(main):006:3*   'role' => 'admin',
irb(main):007:3*   'name' => 'Robert Tables'
irb(main):008:3> }])
Traceback (most recent call last):
        6: from /usr/local/bin/irb:11:in `<main>'
        5: from (irb):4
        4: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:101:in `update_user'
        3: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:94:in `update_users'
        2: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:94:in `each'
        1: from /usr/local/bundle/gems/stream-chat-ruby-1.1.0/lib/stream-chat/client.rb:95:in `block in update_users'
TypeError (no implicit conversion of Symbol into Integer)

@keyneston
Copy link
Contributor

Just recreated this and the following works:

# update a single user
client.update_user({
  :id => 'bob-1',
  :role => 'admin',
  :name => 'Robert Tables'
})

# update multiple users
client.update_users([{
  :id => 'bob-1',
  :role => 'admin',
  :name => 'Robert Tables'
}])

Turns out the keys need to be symbols not strings. I'll update the specific example right away. I'm not sure which other examples have similar problems.

@tbarbugli
Copy link
Member

@bogdan-d any advice here?

@keyneston
Copy link
Contributor

Copying what I put in #12

I've fixed this specific issue, but there seems to be a lot of inconsistencies here.

Like this one which uses strings.

    def create_channel_type(data)
      if !data.key? "commands" || data["commands"].nil? || data["commands"].empty?
        data["commands"] = ["all"]
      end
      post("channeltypes", data: data)
    end

Easiest fix is to switch to using rails' HashWithIndifferentAccess see here. Although that requires a big import. If we want to avoid that path and just fix the code itself I can't say I'm experienced enough in ruby to know the best way (adding support for both, switching all to one way, or what).

@bogdan-getstream
Copy link
Contributor

Using symbols as hash keys is preferred in ruby, as they are immutable and not trigger GC.
But on parsing request keys are currently strings, and it's not easy to fix it as it's breaking change for the SDK users.

This was referenced Mar 26, 2020
@keyneston
Copy link
Contributor

@tbarbugli can you please unassign me :D it seems I don't have permissions.

@ferhatelmas
Copy link
Contributor

@keyneston 👋🏻 replaced with me 😃

@ffenix113
Copy link
Contributor

@peterdeme can you please also check this issue out when you will have time? Should be a quick one. Thank you.

@peterdeme
Copy link
Contributor

#104

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants