Search code examples
rubytwittertwitter-gemfaraday-oauth

Configuring twitter gem version 3 with apigee


I was using version 2 of twitter gem in combination with apigee by setting:

Twitter.gateway = 'myapp-myapigee.apiggeee.com'

In version 3, support for API gateways via gateway configuration is removed. Any pointers along the lines of implementing a custom Faraday middleware, as pointed out in the updated release is appreciated.


Solution

  • I removed the code that allowed custom gateway configuration in version 3, since it was untested. If you look at the code I removed, all it did was set the host part of the URL to the gateway immediately before sending the request. If this middleware was working for you in version 2, you should able to copy it into your application:

    require 'faraday'
    
    class ApigeeGatewayMiddleware < Faraday::Middleware
    
      def call(env)
        url = env[:url].dup
        url.host = 'myapp-myapigee.apiggeee.com'
        env[:url] = url
        @app.call(env)
      end
    
    end
    

    Then run:

    Twitter.middleware.insert_after Faraday::Request::UrlEncoded, ApigeeGatewayMiddleware
    

    All subsequent requests should go through the gateway.

    I would consider adding the gateway configuration back into the library if you submitted a pull requests that included tests but I'm unwilling to have untested code in the codebase.