Skip to content

Process dynamic headers before making actual request#606

Merged
jnunemaker merged 7 commits intojnunemaker:masterfrom
ajitsing:master
Aug 27, 2018
Merged

Process dynamic headers before making actual request#606
jnunemaker merged 7 commits intojnunemaker:masterfrom
ajitsing:master

Conversation

@ajitsing
Copy link
Contributor

I had a requirement where I had to pass Request-ID header in all the http calls. So I tried using HTTParty.headers() method to achieve this. But while testing I realized that once the value is set in headers using HTTParty.headers(), it will remain as is.

But I want different behaviour where the value is evaluated every time the call is fired.

One idea is to accept block as header value which can be evaluated when the request is fired. I have solved it in my project by adding a patch to httparty. I thought it would be useful to others as well.

Example of dynamic header:

require 'rails_distributed_tracing'

class BaseApi
  include HTTParty
  headers DistributedTracing::REQUEST_HEADER_KEY => lambda {DistributedTracing.current_request_id}
end

Awaiting for suggestion and feedback.

@jnunemaker
Copy link
Owner

Hey! Thanks for contribution. I'm definitely going to review this but low on time right now. Know that I appreciate it and will do my best. Feel free to bug me if I haven't responded in a week or two.

lib/httparty.rb Outdated
end

def process_dynamic_headers(headers)
headers = headers.map do |header, value|
Copy link
Collaborator

@TheSmartnik TheSmartnik Aug 27, 2018

Choose a reason for hiding this comment

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

What you want here is Enumerable#each_with_object. Resulting code will much cleaner than map + conversion to hash

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@TheSmartnik thanks for the suggestion, changed the code accordingly.

Copy link
Owner

@jnunemaker jnunemaker left a comment

Choose a reason for hiding this comment

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

I like this idea.

@jnunemaker jnunemaker merged commit 9bbeccf into jnunemaker:master Aug 27, 2018
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.

3 participants