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

How to set headers Dynamically #45

Open
mra-wtag opened this issue Jan 20, 2021 · 0 comments
Open

How to set headers Dynamically #45

mra-wtag opened this issue Jan 20, 2021 · 0 comments

Comments

@mra-wtag
Copy link

Hello,

For this example:

require 'my_client/paginator'
require 'rest_in_peace'
require 'faraday'

module MyClient
  class Fabric
    include RESTinPeace

    rest_in_peace do
      use_api ->() do
        ::Faraday.new(url: 'http://localhost:3001', headers: { 'Accept' => 'application/json' }) do |faraday|
          faraday.request :json
          faraday.response :json
          faraday.adapter :excon
          faraday.headers['Accept-Version'] = 'v1'
        end
      end
    
      attributes do
        read :id
        write :name
      end

      resource do
        patch :save, '/fabrics/:id'
        post :create, '/fabrics'
        delete :destroy, '/fabrics/:id'
        get :reload, '/fabrics/:id'
      end

      collection do
        get :all, '/fabrics', paginate_with: MyClient::Paginator
        get :find, '/fabrics/:id'
      end

      acts_as_active_model
    end
  end
end

We are setting static version in the headers in this example. We can access the client in this way:

MyClient::Fabric.find(id: 1)

But is there any way to set the headers['Accept-Version'] dynamically? So for example for find method I will use version v1 and for all method I will use version v2.

Is it possible via REST-in-Peace gem?

Thank You!

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

No branches or pull requests

1 participant