Giter Site home page Giter Site logo

Access to Response Headers about restforce HOT 3 OPEN

restforce avatar restforce commented on June 28, 2024
Access to Response Headers

from restforce.

Comments (3)

aaronbbrown avatar aaronbbrown commented on June 28, 2024 1

I had this exact same problem and used a Middleware to solve it:

module Thing
  module Middleware
    class APIUsage < Faraday::Response::Middleware
      def initialize(app, options = {})
        @app = app
        @options = options
      end

      def call(env)
        ap api_usage(env[:response_headers])
        @app.call(env)
      end

      def api_usage(response)
        return nil unless response['sforce-limit-info']
        matches = response['sforce-limit-info'].match(%r{api-usage=(?<used>\d+)/(?<max>\d+)})
        { used: matches['used'].to_i, max: matches['max'].to_i } if matches
      end
    end
  end
end

# ...

Faraday::Response.register_middleware :api_usage => lambda { Thing::Middleware::APIUsage }
client = Restforce.new
client.middleware.response :api_usage

I'm open to suggetions on how to improve this.

from restforce.

ejholmes avatar ejholmes commented on June 28, 2024

I think a configuration option that determines whether or not the client
should return the full faraday response or just the body would make sense.

On Friday, October 4, 2013, Anton Litvinenko wrote:

Salesforce returns API usage rate in Sforce-Limit-Info header:
api-usage=49/5000000.

I would like to have access to that information. Is it possible with
current implementation? If not, any ideas how this could be implemented in
a most reasonable way?


Reply to this email directly or view it on GitHubhttps://github.com//issues/88
.

from restforce.

antonlitvinenko avatar antonlitvinenko commented on June 28, 2024

That would have effect on all requests. Which means that I would need to process the whole response manually. Couple of other ideas:

  1. to have some sort of back reference to original response or only its headers in Restforce::Collection or Restforce::Mash objects
  2. Another idea is to have an attribute on the client (e.g. api_rates) that would be updated with each response through middleware (I noticed that Restforce::Middleware has a proxy back to a client)

Wouldn't one of these work better?

from restforce.

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.