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

[BUG] Filter all messages does not work as described in documentation #454

Open
shelmire opened this issue Jan 8, 2021 · 5 comments · May be fixed by #485
Open

[BUG] Filter all messages does not work as described in documentation #454

shelmire opened this issue Jan 8, 2021 · 5 comments · May be fixed by #485
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@shelmire
Copy link

shelmire commented Jan 8, 2021

Issue Summary

Getting messages via the described steps in the documentation does not work. Note that below I demonstrate that the example code fails.

Steps to Reproduce

  1. Create a script with the following code (code copied exactly from the initialize step and this section), per https://github.com/sendgrid/sendgrid-ruby/blob/main/USAGE.md#email-activity .

  2. Execute the script.

  3. Observe error.

  4. Note that what I really want to do is get any email status updates within the past hour or so (5 mins in example) with something like this, which should probably work according to https://sendgrid.com/docs/for-developers/sending-email/getting-started-email-activity-api/#creating-compound-queries:

filter_key = 'last_event_time'
filter_operator = ERB::Util.url_encode('>')
filter_value = (Time.now - 300).utc.strftime('%Y-%m-%dT%H:%M:%S.%L%z') # check the last 5 minutes
filter_value = ERB::Util.url_encode(format('"%s"', filter_value))
query_params = {}
query_params['query'] = format("%s%s%s", filter_key, filter_operator, filter_value)
query_params['limit'] = '10000'

Code Snippet

require 'sendgrid-ruby'

sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])

require 'erb'

filter_key = 'to_email'
filter_operator = ERB::Util.url_encode('=')
filter_value = '[email protected]'
filter_value = ERB::Util.url_encode(format('"%s"', filter_value))
query_params = {}
query_params['query'] = format("%s%s%s", filter_key, filter_operator, filter_value)
query_params['limit'] = '1'

params = query_params
response = sg.client.messages.get(query_params: params)
puts response.status_code
puts response.body
puts response.headers

Exception/Log

400
{"errors":[{"message":"error parsing input at line 1, position 11: syntax error: unexpected IDENTIFIER","field":"query"}]}
{"server"=>["nginx"], "date"=>["Fri, 08 Jan 2021 14:43:14 GMT"], "content-type"=>["application/json"], "content-length"=>["123"], "connection"=>["close"], "x-request-id"=>["b753cf16-8c0b-43c4-9301-275a2d002678"], "access-control-allow-methods"=>["HEAD, GET, PUT, POST, DELETE, OPTIONS, PATCH"], "access-control-max-age"=>["21600"], "access-control-expose-headers"=>["Link, Location"], "access-control-allow-origin"=>["*"], "access-control-allow-headers"=>["AUTHORIZATION, Content-Type, On-behalf-of, x-sg-elas-acl, X-Recaptcha, X-Request-Source, Browser-Fingerprint"], "content-security-policy"=>["default-src https://api.sendgrid.com; frame-src 'none'; object-src 'none'"], "x-content-type-options"=>["nosniff"], "strict-transport-security"=>["max-age=31536000"], "x-client-ff"=>["1000"], "x-ratelimit-remaining"=>["7"], "x-ratelimit-limit"=>["10"], "x-ratelimit-reset"=>["1610117100"]}

Technical details:

  • sendgrid-ruby version: tried with 6.3.8 and 6.3.3
  • ruby version: ruby 2.6.6p146
@shelmire
Copy link
Author

shelmire commented Jan 8, 2021

Note that doing a query directly with curl DOES work, so the gem is not behaving as expected:

curl --request GET \
 --url 'https://api.sendgrid.com/v3/messages?limit=10&query=to_email%3D%22testing%40sendgrid.net%22' \
 --header 'authorization: Bearer <<api_key>>'

Or

curl --request GET \
 --url 'https://api.sendgrid.com/v3/messages?limit=10&query=last_event_time%20BETWEEN%20TIMESTAMP%20%22#{s}%22%20AND%20TIMESTAMP%20%22#{e}%22' \
 --header "authorization: Bearer $SENDGRID_API_KEY"

Or

curl --request GET --url 'https://api.sendgrid.com/v3/messages?limit=10&query=last_event_time%20%3E%20TIMESTAMP%20%222021-01-07T20%3A02%3A25Z%22' --header "authorization: Bearer $SENDGRID_API_KEY"

@thinkingserious thinkingserious added status: help wanted requesting help from the community type: question question directed at the library labels Jan 12, 2021
@thinkingserious
Copy link
Contributor

Hello @shelmire,

I'm in the process of trying to reproduce this and I will post my findings here. Thank you!

With best regards,

Elmer

@shelmire
Copy link
Author

@thinkingserious Thanks!

@thinkingserious
Copy link
Contributor

Hello @shelmire,

Thank you for your patience!

I was able to reproduce this issue and confirm that via cURL your query works as expected and within this helper library the error you described is being thrown.

This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

With best regards,

Elmer

@thinkingserious thinkingserious added type: bug bug in the library and removed type: question question directed at the library labels Mar 1, 2021
@saurabh-dgdp
Copy link

Hello @thinkingserious,

The issue was we don't need to encode the query params, it is even mentioned in USAGE.md.

Queries may need to be URL encoded. URL encoding depends on how you're using the API - if you are trying it out here, or using one of the Libraries, we handle the encoding for you. If you are using cURL, or your own implementation, you probably need to encode it.

I have raised a PR for this issue. #485

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants