Skip to content

Commit 193401f

Browse files
Merge pull request #60 from DripEmail/refactor_request_data_clumping
Refactor request data clumping
2 parents cf807d4 + c31246c commit 193401f

21 files changed

Lines changed: 341 additions & 207 deletions

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010

1111
### Changed
1212
- `Drip::Client#url_prefix` parameter no longer includes `/vN` part of the URL in order to prepare for Shopper Activity API. This breaks backwards compatibility for this option, but there is no expected production usage of this parameter.
13-
- `Drip::Client#get`, `Drip::Client#post`, `Drip::Client#put`, and `Drip::Client#delete` methods no longer auto-prepend `/v2` if the given path starts with `v2/` or `v3/`. This behavior is deprecated and will produce a warning. If you are using one of these methods and get this warning, just add `v2/` to the beginning of the path when you call it.
13+
- `Drip::Client#get`, `Drip::Client#post`, `Drip::Client#put`, and `Drip::Client#delete` are deprecated. If you are using these to hit a Drip API endpoint, please file a ticket or PR to fix the use case.
1414
- `Drip::Client#generate_resource` is deprecated and will be removed in a future version.
1515
- `Drip::Client#content_type` is deprecated and will be removed in a future version. It is no longer used internally, effective immediately.
1616
- When using the block form of parameter initialization, a configuration object is provided instead of the client itself.
1717
- Calling configuration setters on `Drip::Client` is deprecated.
18+
- `Drip::Client::REDIRECT_LIMIT` constant is now private. This is supposed to be an implementation detail and shouldn't leak.
1819

1920
### Removed
2021
- Drop support for Ruby 2.1.

lib/drip/client.rb

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "drip/errors"
2+
require "drip/request"
23
require "drip/response"
34
require "drip/client/accounts"
45
require "drip/client/broadcasts"
@@ -9,6 +10,7 @@
910
require "drip/client/custom_fields"
1011
require "drip/client/events"
1112
require "drip/client/forms"
13+
require "drip/client/http_client"
1214
require "drip/client/orders"
1315
require "drip/client/subscribers"
1416
require "drip/client/tags"
@@ -20,7 +22,7 @@
2022
require "json"
2123

2224
module Drip
23-
class Client # rubocop:disable Metrics/ClassLength
25+
class Client
2426
include Accounts
2527
include Broadcasts
2628
include Campaigns
@@ -36,8 +38,6 @@ class Client # rubocop:disable Metrics/ClassLength
3638
include Workflows
3739
include WorkflowTriggers
3840

39-
REDIRECT_LIMIT = 10
40-
4141
Drip::Client::Configuration::CONFIGURATION_FIELDS.each do |config_key|
4242
define_method(config_key) do
4343
@config.public_send(config_key)
@@ -68,69 +68,31 @@ def content_type
6868
JSON_API_CONTENT_TYPE
6969
end
7070

71-
def get(url, options = {})
72-
make_request(Net::HTTP::Get, make_uri(url), options)
73-
end
74-
75-
def post(url, options = {})
76-
make_request(Net::HTTP::Post, make_uri(url), options)
71+
Drip::Request::VERB_CLASS_MAPPING.keys.each do |verb|
72+
define_method(verb) do |path, options = {}|
73+
warn "[DEPRECATED] Drip::Client##{verb} please use the API endpoint specific methods"
74+
make_json_api_request(verb, "v2/#{path}", options)
75+
end
7776
end
7877

79-
def put(url, options = {})
80-
make_request(Net::HTTP::Put, make_uri(url), options)
81-
end
78+
private
8279

83-
def delete(url, options = {})
84-
make_request(Net::HTTP::Delete, make_uri(url), options)
80+
def make_json_api_request(http_verb, path, options = {})
81+
make_request Drip::Request.new(http_verb, make_uri(path), options, JSON_API_CONTENT_TYPE)
8582
end
8683

87-
private
88-
8984
def private_generate_resource(key, *args)
9085
# No reason for this to be part of the public API, so making a duplicate method to make it private.
9186
{ key => args }
9287
end
9388

9489
def make_uri(path)
95-
if !path.start_with?("v2/") && !path.start_with?("v3/")
96-
warn "[DEPRECATED] Automatically prepended path with 'v2/'"
97-
path = "v2/#{path}"
98-
end
9990
URI(@config.url_prefix) + URI(path)
10091
end
10192

102-
def make_request(verb_klass, uri, options, step = 0)
103-
raise TooManyRedirectsError, 'too many HTTP redirects' if step >= REDIRECT_LIMIT
104-
93+
def make_request(drip_request)
10594
build_response do
106-
Net::HTTP.start(uri.host, uri.port, connection_options(uri.scheme)) do |http|
107-
if verb_klass == Net::HTTP::Get
108-
uri.query = URI.encode_www_form(options)
109-
end
110-
111-
request = verb_klass.new uri
112-
113-
unless verb_klass == Net::HTTP::Get
114-
request.body = options.to_json
115-
end
116-
117-
request['User-Agent'] = "Drip Ruby v#{Drip::VERSION}"
118-
request['Content-Type'] = JSON_API_CONTENT_TYPE
119-
request['Accept'] = "*/*"
120-
121-
if @config.access_token
122-
request['Authorization'] = "Bearer #{@config.access_token}"
123-
else
124-
request.basic_auth @config.api_key, ""
125-
end
126-
127-
response = http.request request
128-
if response.is_a?(Net::HTTPRedirection)
129-
return make_request(verb_klass, URI(response["Location"]), options, step + 1)
130-
else
131-
response
132-
end
133-
end
95+
Drip::Client::HTTPClient.new(@config).make_request(drip_request)
13496
end
13597
end
13698

@@ -140,18 +102,5 @@ def build_response(&block)
140102
rescue JSON::ParserError
141103
Drip::Response.new(response.code.to_i, nil)
142104
end
143-
144-
def connection_options(uri_scheme)
145-
options = { use_ssl: uri_scheme == "https" }
146-
147-
if @config.http_open_timeout
148-
options[:open_timeout] = @config.http_open_timeout
149-
options[:ssl_timeout] = @config.http_open_timeout
150-
end
151-
152-
options[:read_timeout] = @config.http_timeout if @config.http_timeout
153-
154-
options
155-
end
156105
end
157106
end

lib/drip/client/accounts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Accounts
66
# Returns a Drip::Response.
77
# See https://www.getdrip.com/docs/rest-api#accounts
88
def accounts
9-
get "v2/accounts"
9+
make_json_api_request :get, "v2/accounts"
1010
end
1111

1212
# Public: Fetch an account.
@@ -16,7 +16,7 @@ def accounts
1616
# Returns a Drip::Response.
1717
# See https://www.getdrip.com/docs/rest-api#accounts
1818
def account(id)
19-
get "v2/accounts/#{id}"
19+
make_json_api_request :get, "v2/accounts/#{id}"
2020
end
2121
end
2222
end

lib/drip/client/broadcasts.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Broadcasts
1212
# Returns a Drip::Response
1313
# See https://www.getdrip.com/docs/rest-api#broadcasts
1414
def broadcasts(options = {})
15-
get "v2/#{account_id}/broadcasts", options
15+
make_json_api_request :get, "v2/#{account_id}/broadcasts", options
1616
end
1717

1818
# Public: Fetch a broadcast.
@@ -22,7 +22,7 @@ def broadcasts(options = {})
2222
# Returns a Drip::Response.
2323
# See https://www.getdrip.com/docs/rest-api#broadcasts
2424
def broadcast(id)
25-
get "v2/#{account_id}/broadcasts/#{id}"
25+
make_json_api_request :get, "v2/#{account_id}/broadcasts/#{id}"
2626
end
2727
end
2828
end

lib/drip/client/campaign_subscriptions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module CampaignSubscriptions
88
# Returns a Drip::Response.
99
# See https://www.getdrip.com/docs.rest-api#campaign_subscriptions
1010
def campaign_subscriptions(subscriber_id)
11-
get "v2/#{account_id}/subscribers/#{subscriber_id}/campaign_subscriptions"
11+
make_json_api_request :get, "v2/#{account_id}/subscribers/#{subscriber_id}/campaign_subscriptions"
1212
end
1313
end
1414
end

lib/drip/client/campaigns.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Campaigns
1414
# Returns a Drip::Response.
1515
# See https://www.getdrip.com/docs.rest-api#campaigns
1616
def campaigns(options = {})
17-
get "v2/#{account_id}/campaigns", options
17+
make_json_api_request :get, "v2/#{account_id}/campaigns", options
1818
end
1919

2020
# Public: Fetch a campaign.
@@ -24,7 +24,7 @@ def campaigns(options = {})
2424
# Returns a Drip::Response.
2525
# See https://www.getdrip.com/docs/rest-api#campaigns
2626
def campaign(id)
27-
get "v2/#{account_id}/campaigns/#{id}"
27+
make_json_api_request :get, "v2/#{account_id}/campaigns/#{id}"
2828
end
2929

3030
# Public: Activate a campaign.
@@ -34,7 +34,7 @@ def campaign(id)
3434
# Returns a Drip::Response.
3535
# See https://www.getdrip.com/docs/rest-api#campaigns
3636
def activate_campaign(id)
37-
post "v2/#{account_id}/campaigns/#{id}/activate"
37+
make_json_api_request :post, "v2/#{account_id}/campaigns/#{id}/activate"
3838
end
3939

4040
# Public: Pause a campaign.
@@ -44,7 +44,7 @@ def activate_campaign(id)
4444
# Returns a Drip::Response.
4545
# See https://www.getdrip.com/docs/rest-api#campaigns
4646
def pause_campaign(id)
47-
post "v2/#{account_id}/campaigns/#{id}/pause"
47+
make_json_api_request :post, "v2/#{account_id}/campaigns/#{id}/pause"
4848
end
4949

5050
# Public: List everyone subscribed to a campaign.
@@ -68,7 +68,7 @@ def pause_campaign(id)
6868
# Returns a Drip::Response.
6969
# See https://www.getdrip.com/docs/rest-api#campaigns
7070
def campaign_subscribers(id, options = {})
71-
get "v2/#{account_id}/campaigns/#{id}/subscribers", options
71+
make_json_api_request :get, "v2/#{account_id}/campaigns/#{id}/subscribers", options
7272
end
7373
end
7474
end

lib/drip/client/conversions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Conversions
1010
# Returns a Drip::Response.
1111
# See https://www.getdrip.com/docs/rest-api#conversions
1212
def conversions(options = {})
13-
get "v2/#{account_id}/goals", options
13+
make_json_api_request :get, "v2/#{account_id}/goals", options
1414
end
1515

1616
# Public: Fetch a conversion.
@@ -20,7 +20,7 @@ def conversions(options = {})
2020
# Returns a Drip::Response.
2121
# See https://www.getdrip.com/docs/rest-api#conversions
2222
def conversion(id)
23-
get "v2/#{account_id}/goals/#{id}"
23+
make_json_api_request :get, "v2/#{account_id}/goals/#{id}"
2424
end
2525
end
2626
end

lib/drip/client/custom_fields.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module CustomFields
66
# Returns a Drip::Response.
77
# See https://www.getdrip.com/docs/rest-api#custom_fields
88
def custom_fields
9-
get "v2/#{account_id}/custom_field_identifiers"
9+
make_json_api_request :get, "v2/#{account_id}/custom_field_identifiers"
1010
end
1111
end
1212
end

lib/drip/client/events.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module Events
1414
# See https://www.getdrip.com/docs/rest-api#record_event
1515
def track_event(email, action, properties = {}, options = {})
1616
data = options.merge({ "email" => email, "action" => action, "properties" => properties })
17-
post "v2/#{account_id}/events", private_generate_resource("events", data)
17+
make_json_api_request :post, "v2/#{account_id}/events", private_generate_resource("events", data)
1818
end
1919

2020
# Public: Track a collection of events all at once.
@@ -28,7 +28,7 @@ def track_event(email, action, properties = {}, options = {})
2828
# See https://www.getdrip.com/docs/rest-api#event_batches
2929
def track_events(events)
3030
url = "v2/#{account_id}/events/batches"
31-
post url, private_generate_resource("batches", { "events" => events })
31+
make_json_api_request :post, url, private_generate_resource("batches", { "events" => events })
3232
end
3333

3434
# Public: Fetch all custom event actions.
@@ -41,7 +41,7 @@ def track_events(events)
4141
# Returns a Drip::Response.
4242
# See https://www.getdrip.com/docs/rest-api#events
4343
def event_actions(options = {})
44-
get "v2/#{account_id}/event_actions", options
44+
make_json_api_request :get, "v2/#{account_id}/event_actions", options
4545
end
4646
end
4747
end

lib/drip/client/forms.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Forms
66
# Returns a Drip::Response.
77
# See https://www.getdrip.com/docs/rest-api#forms
88
def forms
9-
get "v2/#{account_id}/forms"
9+
make_json_api_request :get, "v2/#{account_id}/forms"
1010
end
1111

1212
# Public: Fetch a form.
@@ -16,7 +16,7 @@ def forms
1616
# Returns a Drip::Response.
1717
# See https://www.getdrip.com/docs/rest-api#forms
1818
def form(id)
19-
get "v2/#{account_id}/forms/#{id}"
19+
make_json_api_request :get, "v2/#{account_id}/forms/#{id}"
2020
end
2121
end
2222
end

0 commit comments

Comments
 (0)