mailgun-ruby: Problems with pagination in v1.1.8

I recently updated to v1.1.8 of this gem (from v1.1.6) and my code started to fail. I tracked it down to the pagination that seems to be buggy.

What I’m doing is getting the “delivered” events for two of my domains:

mailgun = Mailgun::Client.new
%w(domain1.com domain2.com).each do |domain|
  mailgun_events = Mailgun::Events.new(mailgun, domain)
  events = mailgun_events.get(event: 'delivered').to_h['items']
  while events.any?
    events.each do |event|
      # Do something with the event
    end
    events = mailgun_events.next.to_h['items']
  end
end

What started happening is that not only “delivered”, but ALL events were returned from the API. When I downgraded to v1.1.6 again, the error went away.

I assume the bug is somewhere in here: https://github.com/mailgun/mailgun-ruby/commit/9ad5cbd95f2570b3a3c681cc1e362c702b84eded#diff-e904dd8e3437a9a9c5e35c6dcb227d30

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (12 by maintainers)

Most upvoted comments

Although I wasn’t able to identify the reason why it had originally worked in the past versions, but at least a quick analysis helped me understand what happened with the above cases.

TL; DR

mailgun’s event API “forgets” parameters EACH TIME you fetch the data.

So I’d suggest to set parameters when calling next and previous, as it now accepts arguments.

For instance, in your case @aldrienht , instead of simply hitting next, previous

@all_mails = @mg_events.next.to_h['items']

you could specify the parameters with every request

event_settings = { limit: 5. pretty: 'yes', event: 'delivered' }

@all_mails = @mg_events.next(event_settings).to_h['items']

As far as I can see the mailgun-ruby gem hasn’t been changed its behavior about how to handle the parameters in recent versions, so this would be the only way to keep the original settings by far.

Hope that solves the problems above.

FYI, suppressions API seems to be working differently. It returns requested params each time as part of the next url, which allows us to retrieve bounced mails with the same conditions over again. (you simply can hit next and previous without specifying parameters)

API responses

Below shows part of what I have done for an analysis.

You could quickly check the API feature as follows:

# requesting two mails directly (without using the gem)

response = RestClient.get( "https://api:YOUR_API_KEY@api.mailgun.net/v3/YOUR_DOMAIN/events", params: { limit: 2 } )

JSON.parse(response.body)['paging']['next']
=> https://api.mailgun.net/v3/YOUR_DOMAIN/events/YACI4pW3siLTA0LTA.....

# params are not set as part of the next URI

On the other hand, when you request bounces…

# requesting two mails directly (without using the gem)

response = RestClient.get( "https://api:YOUR_API_KEY@api.mailgun.net/v3/YOUR_DOMAIN/bounces", params: { limit: 2 } )

JSON.parse(response.body)['paging']['next']
=> "https://api.mailgun.net/v3/YOUR_DOMAIN/bounces?page=next&address=somebody%40example.com&limit=2"

# params are embedded as part of the next URI

Due to this feature, you are requested to set parameters (eg. limit, begin, end…etc) each time, or otherwise mailgun seems to forget the past settings, and responds based on its default settings.

I’m not really sure why/how it worked in the past, but it apparently shows it’s coming from API feature itself.

Hope that saves somebody’s day…

I opened a ticket for the Mailgun support about this and will keep you updated about their response!

I’m seeing similar behavior in 1.1.9.

I think I have solved the problem here: https://github.com/mailgun/mailgun-ruby/pull/142