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

Pass request headers to receive_web_request #1415

Merged

Conversation

albertsun
Copy link
Contributor

For some web services (like one I'm writing to receive Twilio text messages and will send a pull for) info in the headers is important in order to verify or understand the request.

This adds a fourth options headers={} to receive_web_request. For any custom agents people might have that aren't committed here, it does an arity check to not send headers if the method doesn't support them.

I also wonder if we should just pass the entire request object http://guides.rubyonrails.org/action_controller_overview.html#the-request-object instead of splitting it across method, format and headers arguments.

if respond_to?(:receive_webhook)
Rails.logger.warn "DEPRECATED: The .receive_webhook method is deprecated, please switch your Agent to use .receive_web_request."
receive_webhook(params).tap do
self.last_web_request_at = Time.now
save!
end
else
elsif method(:receive_web_request).arity == 3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice thought to avoid breaking custom agents, but as of now we do not support agents in gems and normally just ensure that we do not break agent configurations with updates. I think it would be fine to modify the existing agents and always assume an arity of 4. What do you think @cantino ?

@dsander
Copy link
Collaborator

dsander commented Apr 9, 2016

Good idea! Maybe you could pass the request object as a fifth argument for agents that need access to everything, but keep the simplicity for the other agents?

@albertsun
Copy link
Contributor Author

Good idea. Added the request object as a fifth argument. The thought for keeping the arity check was kind of in case anyone is using agents that haven't been merged upstream here or for future agents to be able to write receive_web_request in a simpler manner. But could go either way on that, happy to just have it always support 5 arguments.

@albertsun albertsun force-pushed the feature/request-headers-in-web-request branch from fb9d315 to 2a3fa43 Compare April 10, 2016 16:51
@@ -160,7 +160,7 @@ def push_hubs
interpolated['push_hubs'].presence || []
end

def receive_web_request(params, method, format)
def receive_web_request(params, method, format, headers={}, request=ActionDispatch::Request.new({}))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be receive_web_request(params, method, format), right? Since we support both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, it and other current agents could be like that.

@cantino
Copy link
Member

cantino commented Apr 10, 2016

Good idea! We'll need to update the wiki too.

@albertsun albertsun force-pushed the feature/request-headers-in-web-request branch from 05c35db to a4e0112 Compare April 11, 2016 22:26
@albertsun
Copy link
Contributor Author

Ok, so I've rebased the branch off latest master. Everything look ok to merge now? After merging I can update the documentation on that wiki page.

@dsander
Copy link
Collaborator

dsander commented Apr 11, 2016

I am still not a huge fan of the arity check as it introduces complexity that we probably never be able to remove afterwards. If we go with it couldn't the receive_web_request method definition of existing agents stay the same as before?

@albertsun
Copy link
Contributor Author

I don't feel strongly about the arity check either way. Thought it could be nice to preserve some agents with the simpler syntax. Happy to take it out and just update every receive_web_hook call to take 5 arguments if everyone agrees that's the way to go. @cantino thoughts on that?

@knu
Copy link
Member

knu commented Apr 12, 2016

I'd consider five arguments as way too many. Given the fact that the request object should include everything including params, method, format and headers, I'd go with receive_web_request(request).

@knu
Copy link
Member

knu commented Apr 12, 2016

I think retaining compatibility by checking the arity is preferable here, because one with an in-house/personal agent wouldn't even know it got broken by the method signature change until it received the first request after upgrading and then found a failure in the application log afterwards.

As for @dsander's concern, we could probably add a check in Agent loader and show a banner to admins after login that urges them to update their agents if the check fails. After a certain amount of time (grace period) it'd be fine to lift it and remove the arity check.

@cantino
Copy link
Member

cantino commented Apr 13, 2016

I'd probably do this:

if method(:receive_web_request).arity == 3
  handled_request = receive_web_request(params, method, format)
else
  handled_request = receive_web_request(request)
end

Maybe not worry about the banner for now, though?

@dsander
Copy link
Collaborator

dsander commented Apr 13, 2016

I think retaining compatibility by checking the arity is preferable here, because one with an in-house/personal agent wouldn't even know it got broken by the method signature change until it received the first request after upgrading and then found a failure in the application log afterwards.

I would assume the in house agent had specs which would run and fail before the Huginn instance is updated :)

As for @dsander's concern, we could probably add a check in Agent loader and show a banner to admins after login that urges them to update their agents if the check fails. After a certain amount of time (grace period) it'd be fine to lift it and remove the arity check.

That would still break for users that update very infrequently. If we really want to maintain backwards compatibility we can never remove it.

Since we do not support external Agents yet I consider our Concern an internal API and thus not worry about breaking something external. If we go with it I would prefer @cantino approach without modifying the existing agents.

@knu
Copy link
Member

knu commented Apr 13, 2016

I would assume the in house agent had specs which would run and fail before the Huginn instance is updated :)

I'd really hope so, but even the fact that many users are running Huginn in development was beyond my expectation, and I wouldn't dare to assume much. ;D

That would still break for users that update very infrequently. If we really want to maintain backwards compatibility we can never remove it.

I mean after a grace period backward compatibility code should be removed and the arity check in Agent loader be turned into part of sanity check, refusing to boot on failure instead of relying on backward compatibility code and showing a banner. I hopefully think it is not asking too much that infrequent updates sometimes fail and require admins to take action.

One problem we have is we do not really have a channel for announcing breaking changes to users/admins. I first thought we could add a migration to perform the arity check for existing agents, but it turned out that it'd be unrealistic to load all agents in a migration process. So, I thought it could be an option to take a grace period and show a prewarning, if not this time.

@albertsun
Copy link
Contributor Author

Ok. I've modified it to take either 1 argument (request) or 3 arguments (same signature as before) and left the existing agents using 3 arguments.

I wonder if for now we just keep this as a non-breaking change but that at some point in the future we can batch together a whole bunch of breaking changes at once and push them as a versioned release or something, once there is a method for announcing breaking changes and the like.

@cantino
Copy link
Member

cantino commented Apr 13, 2016

I agree, we should bundle some breaking stuff into a versioned update sometime soon. (How does one version a Rails application?)

Is this ready to go?

@@ -100,6 +100,10 @@ def receive_web_request(params, method, format)
["not implemented", 404]
end

# alternate method signature for receive_web_request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it helps anyone reading the code to see that there's an alternate way for them to define receive_web_request but if you think it muddies things up too much I'll remove it.

@albertsun
Copy link
Contributor Author

Other than whether or not to leave that comment in, I believe this is ready to go

@albertsun
Copy link
Contributor Author

Closing/re-opening to trigger a travis re-build to hopefully clear up a transient bundler install failure

@albertsun albertsun closed this Apr 14, 2016
@albertsun albertsun reopened this Apr 14, 2016
@dsander
Copy link
Collaborator

dsander commented Apr 15, 2016

Looks good to me, I don't feel strongly about the comment.

@@ -149,15 +153,21 @@ def update_event_expirations!
end
end

def trigger_web_request(params, method, format)
def trigger_web_request(request)
params = request.params.except(:action, :controller, :agent_id, :user_id, :format)
if respond_to?(:receive_webhook)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's being used below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, should we still offer this backward compatibility? It's been there with a deprecation warning for years, so I thought it might be the time to get rid of it.

@cantino
Copy link
Member

cantino commented Apr 16, 2016

Looks good to me!

@cantino cantino merged commit ee504ba into huginn:master Apr 16, 2016
@albertsun
Copy link
Contributor Author

Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants