1919require "json"
2020
2121module Drip
22+ class TooManyRedirectsError < StandardError ; end
23+
2224 class Client
2325 include Accounts
2426 include Broadcasts
@@ -36,14 +38,9 @@ class Client
3638 include Workflows
3739 include WorkflowTriggers
3840
39- attr_accessor :access_token , :api_key , :account_id , :url_prefix
41+ REDIRECT_LIMIT = 10
4042
41- VERB_MAPPING = {
42- get : Net ::HTTP ::Get ,
43- post : Net ::HTTP ::Post ,
44- put : Net ::HTTP ::Put ,
45- delete : Net ::HTTP ::Delete
46- } . freeze
43+ attr_accessor :access_token , :api_key , :account_id , :url_prefix
4744
4845 def initialize ( options = { } )
4946 @account_id = options [ :account_id ]
@@ -62,26 +59,31 @@ def content_type
6259 end
6360
6461 def get ( url , options = { } )
65- make_request ( Net ::HTTP ::Get , url , options )
62+ make_request ( Net ::HTTP ::Get , make_uri ( url ) , options )
6663 end
6764
6865 def post ( url , options = { } )
69- make_request ( Net ::HTTP ::Post , url , options )
66+ make_request ( Net ::HTTP ::Post , make_uri ( url ) , options )
7067 end
7168
7269 def put ( url , options = { } )
73- make_request ( Net ::HTTP ::Put , url , options )
70+ make_request ( Net ::HTTP ::Put , make_uri ( url ) , options )
7471 end
7572
7673 def delete ( url , options = { } )
77- make_request ( Net ::HTTP ::Delete , url , options )
74+ make_request ( Net ::HTTP ::Delete , make_uri ( url ) , options )
7875 end
7976
8077 private
8178
82- def make_request ( verb_klass , url , options )
79+ def make_uri ( path )
80+ URI ( url_prefix ) + URI ( path )
81+ end
82+
83+ def make_request ( verb_klass , uri , options , step = 0 )
84+ raise TooManyRedirectsError , 'too many HTTP redirects' if step >= REDIRECT_LIMIT
85+
8386 build_response do
84- uri = URI ( url_prefix ) + URI ( url )
8587 Net ::HTTP . start ( uri . host , uri . port , use_ssl : uri . scheme == "https" ) do |http |
8688 if verb_klass . is_a? Net ::HTTP ::Get
8789 uri . query = URI . encode_www_form ( options )
@@ -92,7 +94,7 @@ def make_request(verb_klass, url, options)
9294 unless verb_klass . is_a? Net ::HTTP ::Get
9395 request . body = options . to_json
9496 end
95-
97+
9698 request [ 'User-Agent' ] = "Drip Ruby v#{ Drip ::VERSION } "
9799 request [ 'Content-Type' ] = content_type
98100 request [ 'Accept' ] = "*/*"
@@ -103,7 +105,12 @@ def make_request(verb_klass, url, options)
103105 request . basic_auth api_key , ""
104106 end
105107
106- http . request request
108+ response = http . request request
109+ if response . is_a? ( Net ::HTTPRedirection )
110+ return make_request ( verb_klass , URI ( response [ "Location" ] ) , options , step + 1 )
111+ else
112+ response
113+ end
107114 end
108115 end
109116 end
0 commit comments