Skip to content

Commit 78542ca

Browse files
committed
add 3rd party API proxy example
1 parent 48be820 commit 78542ca

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎examples/api_proxy.rb‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env ruby
2+
3+
# Rewrites and proxies requests to a third-party API, with HTTP basic authentication.
4+
5+
require'goliath'
6+
require'em-synchrony/em-http'
7+
8+
classTwilioResponse < Goliath::API
9+
useGoliath::Rack::Params
10+
useGoliath::Rack::JSONP
11+
12+
HEADERS={authorization: ENV.values_at("TWILIO_SID","TWILIO_AUTH_TOKEN")}
13+
BASE_URL="https://api.twilio.com/2010-04-01/Accounts/#{ENV['TWILIO_SID']}/AvailablePhoneNumbers/US"
14+
15+
defresponse(env)
16+
url="#{BASE_URL}#{env['REQUEST_PATH']}?#{env['QUERY_STRING']}"
17+
logger.debug"Proxying #{url}"
18+
19+
http=EM::HttpRequest.new(url).gethead: HEADERS
20+
logger.debug"Received #{http.response_header.status} from Twilio"
21+
22+
[200,{'X-Goliath'=>'Proxy','Content-Type'=>'application/javascript'},http.response]
23+
end
24+
end
25+
26+
classTwilio < Goliath::API
27+
get%r{^/(Local|TollFree)},TwilioResponse
28+
end

0 commit comments

Comments
(0)