sendgrid / ruby-http-client

SendGrid's Ruby HTTP Client for calling APIs
https://sendgrid.com
MIT License
23 stars 54 forks source link

Client instance clobbers internal state #93

Closed jacobat closed 5 years ago

jacobat commented 5 years ago

Issue Summary

Reusing the Sendgrid client causes a 404.

Steps to Reproduce

If you try to reuse a client with a path multiple times it fails on subsequent attempts:

mail = SendGrid::Mail.new
[ ... do some mail setup ...]
client = SendGrid::API.new(api_key: API_KEY, host: 'https://api.sendgrid.com').client.mail
client._('send').post(request_body: mail.to_json) # This succeeds
client._('send').post(request_body: mail.to_json) # This fails

if instead you move the clients #mail method to be called twice things work:

mail = SendGrid::Mail.new
[ ... do some mail setup ...]
client = SendGrid::API.new(api_key: API_KEY, host: 'https://api.sendgrid.com').client
client.mail._('send').post(request_body: mail.to_json) # This succeeds
client.mail._('send').post(request_body: mail.to_json) # This succeeds

This is very unexpected behaviour, the code should not require you to call mail over an over. Instead you should be able to reuse the same client instance over and over.

Technical details:

jacobat commented 5 years ago

I believe the issue is caused by these lines: https://github.com/sendgrid/ruby-http-client/blob/master/lib/ruby_http_client.rb#L215-L216

I think replacing those two lines by will fix the issue:

url_path = name ? @url_path + [name] : @url_path