Model-specific headers #17

Closed
jsmestad opened this Issue Jul 18, 2012 · 5 comments

Comments

Projects
None yet
2 participants

What

Set headers for a model to be a Hash, Proc or lambda.

Why

This is for the case when your using an OAuth-based API, like Facebook, where you need to use the specific user token for any request. Token is also an attribute on the object so it must be evaluated per-user on request.

Proposed Change Example

class User
  include Her::Model

  headers Proc.new do |obj|
    'Authorize' => "token #{obj.token}"
  end

What are your thoughts? I can try to take a pass at this unless there is a better way or somehow I missed the feature while surfing through the source code.

Actually a better question here is how would you use Her against an API that required you to use authorization headers or parameters to verify resource ownership. For example, something that uses OAuth2 expiring keys

Owner

remiprev commented Jul 19, 2012

@jsmestad Have you looked at the OAuth example in the readme? That, in combination with something like this should solve your problem.

Hope that helps!

If you are working with multiple users per request, this probably would not work sharing a single connection?


For more context, check out the issue I opened over at applicake/doorkeeper#115 , I think Her would be a great solution for consuming doorkeeper-based API's

Owner

remiprev commented Jul 20, 2012

If you are working with multiple users per request, this probably would not work sharing a single connection?

You can create a middleware that execute code to send a different token per request. This is what I did in this example. Each time Her makes a request, MyApp.current_user.token is checked and sent as an HTTP header if it’s present.

If you’re looking for a different connection per model, you can look into the uses_api class method, documented here.

Owner

remiprev commented Dec 3, 2012

I’m going to close this issue since it had no development for the last months.

@remiprev remiprev closed this Dec 3, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment