Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add 3rd party API proxy example

  • Loading branch information...
commit 78542ca21b84470052a69ac782f0c3582f01c67c 1 parent 48be820
@subelsky subelsky authored
Showing with 28 additions and 0 deletions.
  1. +28 −0 examples/api_proxy.rb
View
28 examples/api_proxy.rb
@@ -0,0 +1,28 @@
+#!/usr/bin/env ruby
+
+# Rewrites and proxies requests to a third-party API, with HTTP basic authentication.
+
+require 'goliath'
+require 'em-synchrony/em-http'
+
+class TwilioResponse < Goliath::API
+ use Goliath::Rack::Params
+ use Goliath::Rack::JSONP
+
+ HEADERS = { authorization: ENV.values_at("TWILIO_SID","TWILIO_AUTH_TOKEN") }
+ BASE_URL = "https://api.twilio.com/2010-04-01/Accounts/#{ENV['TWILIO_SID']}/AvailablePhoneNumbers/US"
+
+ def response(env)
+ url = "#{BASE_URL}#{env['REQUEST_PATH']}?#{env['QUERY_STRING']}"
+ logger.debug "Proxying #{url}"
+
+ http = EM::HttpRequest.new(url).get head: HEADERS
+ logger.debug "Received #{http.response_header.status} from Twilio"
+
+ [200, {'X-Goliath' => 'Proxy','Content-Type' => 'application/javascript'}, http.response]
+ end
+end
+
+class Twilio < Goliath::API
+ get %r{^/(Local|TollFree)}, TwilioResponse
+end
Please sign in to comment.
Something went wrong with that request. Please try again.