Git Product home page Git Product logo

Comments (10)

pencil avatar pencil commented on May 23, 2024

Ran into a similar problem when we wanted to use the same model with two different endpoints (depending on a user selection). Did some nasty things with class unloading to make this work. It would be nice to have an official solution.

from her.

remi avatar remi commented on May 23, 2024

Did some nasty things with class unloading to make this work.

Can you share this particular solution? I just want to be sure of what youโ€™re talking about.

from her.

pencil avatar pencil commented on May 23, 2024

I have something like this in my Rails controller:

before_filter :initialize_foo_models

# ...

# FooPlugin.setup does the Her::API.new and .setup(...) things
def initialize_foo_models
  if foo?
    FooPlugin.setup configs[:foo]
    reload_foo_model_classes
  elsif bar?
    FooPlugin.setup configs[:bar]
    reload_foo_model_classes
  end
end

def reload_foo_model_classes
  foo_lib_path = Gem::Specification.find_by_name('foo_plugin').gem_dir + '/lib/foo_plugin'
  [:User, :Address].each do |class_name|
    if FooPlugin.const_defined?(class_name)
      FooPlugin.send(:remove_const, class_name)
      load "#{foo_lib_path}/#{class_name.downcase}.rb"
    end
  end
end

from her.

remi avatar remi commented on May 23, 2024

Iโ€™m gonna close this issue now for lack of activity. If you guys want to submit a pull request for this feature, youโ€™re more than welcome!

from her.

alfius avatar alfius commented on May 23, 2024

I'm in the need of something like this now. I would like to be able to do:

admin_api = Her::API.new do |c|
  c.use FaradayMiddleware::OAuth2, @admin_token
end

user_api = Her::API.new do |c|
  c.use FaradayMiddleware::OAuth2, @user_token
end

User.scoped_api(admin_api).all.first  # uses the admin token
User.scoped_api(user_api).all.first  # uses the user token

# it should also support relationships:
User.scoped_api(user_api).find(1).profiles.first  # /profiles should be retrieved using the scoped api

I've been trying to make this change in Her but apparently this might require a major refactor that separates the models from the apis. Are there any plans to add this feature?

from her.

alfius avatar alfius commented on May 23, 2024

Another option might be to introduce the idea of a Connection, untying the Faraday options from the Api object. A Connection could then be specified on the same way as my previous comment. Note that this will require an important refactor as well.

from her.

pencil avatar pencil commented on May 23, 2024

As a workaround you could use the newly introduced Proc support of use_api (#166)

from her.

alfius avatar alfius commented on May 23, 2024

@pencil thanks! That helps, although the app I'm dealing with makes the two types of requests (admin and current user) and I can't find a way to send that information down to the proc :\

from her.

pencil avatar pencil commented on May 23, 2024

@alfonsocora You would want to have some kind of class attribute on the model or a global variable. In your Proc you would check this to decide what API to use. It's an ugly workaround, but it could work ๐Ÿ˜

from her.

m3talsmith avatar m3talsmith commented on May 23, 2024

That's almost the precise example for using a proc - the need to
dynamically choose an API for use_api.
On Aug 17, 2013 2:28 AM, "Nils Caspar" [email protected] wrote:

@alfonsocora https://github.com/alfonsocora You would want to have some
kind of class attribute on the model or a global variable. In your Proc you
would check this to decide what API to use. It's an ugly workaround, but it
could work [image: ๐Ÿ˜]

โ€”
Reply to this email directly or view it on GitHubhttps://github.com//issues/25#issuecomment-22808610
.

from her.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.