zuora_api 1.7.00 → 1.8.00

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,7 @@ module ZuoraAPI
9
9
  super
10
10
  end
11
11
 
12
- def new_session(auth_type: :basic, debug: false)
12
+ def new_session(auth_type: :basic, debug: false, zuora_track_id: nil)
13
13
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Basic Login, does not support Authentication of Type: #{auth_type}") if auth_type != :basic
14
14
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Request Basic Login but either 'Username' or 'Password' were not passed.") if (self.password.blank? && self.username.blank?)
15
15
 
@@ -30,8 +30,11 @@ module ZuoraAPI
30
30
  input_xml = Nokogiri::XML(request.to_xml(:save_with => XML_SAVE_OPTIONS).strip)
31
31
  input_xml.xpath('//ns1:session', 'ns1' =>'http://api.zuora.com/').children.remove
32
32
  Rails.logger.debug('Connect') {"SOAP XML: #{input_xml.to_xml(:save_with => XML_SAVE_OPTIONS).strip}"} if debug
33
-
34
- response_query = HTTParty.post(self.url,:body => request.to_xml(:save_with => XML_SAVE_OPTIONS).strip, :headers => {'Content-Type' => "text/xml; charset=utf-8"}, :timeout => 10)
33
+
34
+ headers = { 'Content-Type' => "text/xml; charset=utf-8" }
35
+ headers['Zuora-Track-Id'] = zuora_track_id if zuora_track_id.present?
36
+
37
+ response_query = HTTParty.post(self.url,:body => request.to_xml(:save_with => XML_SAVE_OPTIONS).strip, :headers => headers, :timeout => 10)
35
38
  output_xml = Nokogiri::XML(response_query.body)
36
39
  Rails.logger.debug('Connect') {"Response Code: #{response_query.code} SOAP XML: #{output_xml.to_xml(:save_with => XML_SAVE_OPTIONS).strip}"} if debug
37
40
 
@@ -94,12 +97,18 @@ module ZuoraAPI
94
97
  self.current_session = retrieved_session
95
98
  end
96
99
  return self.status
97
- rescue *(CONNECTION_EXCEPTIONS).concat(CONNECTION_READ_EXCEPTIONS) => ex
98
- if !(tries -= 1).zero?
99
- Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"}
100
+ rescue *(CONNECTION_EXCEPTIONS + CONNECTION_READ_EXCEPTIONS) => ex
101
+ if !tries.zero?
102
+ tries -= 1
100
103
  sleep(self.timeout_sleep)
101
104
  retry
102
105
  else
106
+ if Rails.logger.class.to_s == "Ougai::Logger"
107
+ Rails.logger.error("BasicLogin - Timed out", ex)
108
+ else
109
+ Rails.logger.error("BasicLogin - #{ex.class} Timed out")
110
+ end
111
+
103
112
  self.current_error = "Request timed out. Try again"
104
113
  self.status = 'Timeout'
105
114
  return self.status
@@ -1,6 +1,6 @@
1
1
  module ZuoraAPI
2
2
  class Oauth < Login
3
- attr_accessor :oauth_client_id, :oauth_secret, :bearer_token, :oauth_session_expires_at
3
+ attr_accessor :oauth_client_id, :oauth_secret, :bearer_token, :oauth_session_expires_at, :scope_entities
4
4
 
5
5
  def initialize(oauth_client_id: nil, oauth_secret: nil, bearer_token: nil, oauth_session_expires_at: nil, **keyword_args)
6
6
  self.oauth_client_id = oauth_client_id
@@ -11,15 +11,15 @@ module ZuoraAPI
11
11
  super
12
12
  end
13
13
 
14
- def new_session(raise_errors: false, auth_type: nil)
14
+ def new_session(auth_type: nil, zuora_track_id: nil)
15
15
  if auth_type == :bearer
16
- get_bearer_token()
16
+ get_bearer_token(zuora_track_id: zuora_track_id)
17
17
  elsif auth_type == :basic
18
- get_bearer_token() if self.oauth_expired?
19
- get_z_session() if self.status == 'Active'
18
+ get_bearer_token(zuora_track_id: zuora_track_id) if self.oauth_expired?
19
+ get_z_session(zuora_track_id: zuora_track_id) if self.status == 'Active'
20
20
  else
21
- get_bearer_token()
22
- get_z_session() if self.status == 'Active'
21
+ get_bearer_token(zuora_track_id: zuora_track_id)
22
+ get_z_session(zuora_track_id: zuora_track_id) if self.status == 'Active'
23
23
  end
24
24
  return self.status
25
25
  end
@@ -29,66 +29,83 @@ module ZuoraAPI
29
29
  return self.bearer_token
30
30
  end
31
31
 
32
- def get_z_session(debug: false)
32
+ def get_z_session(debug: false, zuora_track_id: nil)
33
33
  tries ||= 2
34
- headers = self.entity_id.present? ? {"Zuora-Entity-Ids" => self.entity_id } : {}
34
+ headers = self.entity_id.present? ? {"Zuora-Entity-Ids" => self.entity_id } : {}
35
+ headers['Zuora-Track-Id'] = zuora_track_id if zuora_track_id.present?
35
36
  output_json, response = self.rest_call(:url => self.rest_endpoint("connections"), :session_type => :bearer, :headers => headers)
36
- self.current_session = response.headers.to_h['set-cookie'][0].split(';')[0].split('=',2)[1].gsub('%3D', '=')
37
+ begin
38
+ self.current_session = response.headers.to_h['set-cookie'][0].split(';')[0].split('=',2)[1].gsub('%3D', '=')
39
+ rescue NoMethodError => ex
40
+ Rails.logger.fatal("Failure Parsing Cookie Headers", response.headers.to_s)
41
+ raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Failure Parsing Cookie Headers")
42
+ end
37
43
  rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex
38
- if !(tries -= 1).zero?
44
+ if !tries.zero?
45
+ tries -= 1
39
46
  Rails.logger.debug {"Session Invalid"}
40
47
  self.new_session(auth_type: :bearer)
41
48
  retry
42
- else
43
- if errors.include?(ex.class)
44
- raise ex
45
- else
46
- return [output_json, response]
47
- end
48
49
  end
50
+ raise ex if errors.include?(ex.class)
51
+ return [output_json, response]
52
+
49
53
  rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition => ex
50
- if errors.include?(ex.class)
51
- raise ex
52
- else
53
- return [output_json, response]
54
- end
55
- rescue *(CONNECTION_EXCEPTIONS).concat(CONNECTION_READ_EXCEPTIONS) => ex
56
- if !(tries -= 1).zero?
57
- Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"}
54
+ raise ex if errors.include?(ex.class)
55
+ return [output_json, response]
56
+
57
+ rescue *(CONNECTION_EXCEPTIONS + CONNECTION_READ_EXCEPTIONS) => ex
58
+ if !tries.zero?
59
+ tries -= 1
58
60
  sleep(self.timeout_sleep)
59
61
  retry
62
+ end
63
+ if Rails.logger.class.to_s == "Ougai::Logger"
64
+ Rails.logger.error("OAuthLogin - Timed out", ex)
60
65
  else
61
- self.current_error = "Request timed out. Try again"
62
- self.status = 'Timeout'
63
- return self.status
66
+ Rails.logger.error("OAuthLogin - #{ex.class} Timed out")
64
67
  end
68
+ self.current_error = "Request timed out. Try again"
69
+ self.status = 'Timeout'
70
+ return self.status
65
71
  end
66
72
 
67
- def get_bearer_token
73
+ def get_bearer_token(zuora_track_id: nil)
68
74
  tries ||= 2
69
75
  raise ZuoraAPI::Exceptions::ZuoraAPIAuthenticationTypeError.new("Request Oauth Login but either 'Oauth Client Id' or 'Oauth Secret' were not passed") if self.oauth_client_id.blank? || self.oauth_secret.blank?
70
76
 
77
+ headers = { "content-type" => "application/x-www-form-urlencoded" }
78
+ headers['Zuora-Track-Id'] = zuora_track_id if zuora_track_id.present?
79
+
71
80
  output_json, response = self.rest_call(:method => :post,
72
81
  :url => self.rest_endpoint.chomp('v1/').concat("oauth/token"),
73
82
  :z_session => false,
74
83
  :session_type => :bearer,
75
- :headers => {"content-type" => "application/x-www-form-urlencoded"},
84
+ :headers => headers,
76
85
  :body => {"client_id"=> self.oauth_client_id, "client_secret"=>self.oauth_secret, "grant_type" =>"client_credentials"}
77
86
  )
78
87
 
79
88
  self.bearer_token = output_json["access_token"]
89
+ self.scope_entities = output_json.fetch('scope', '').split(" ").map { |scope| scope.split('.').last.gsub('-', '') if scope.include?('entity.') }.compact.uniq
80
90
  self.oauth_session_expires_at = Time.now.to_i + output_json["expires_in"].to_i
81
91
  self.current_error = nil
82
92
  self.status = 'Active'
83
93
 
84
94
  return self.status
95
+
96
+ rescue ZuoraAPI::Exceptions::ZuoraAPIInternalServerError => ex
97
+ raise ex if tries.zero?
98
+
99
+ tries -= 1
100
+ sleep(self.timeout_sleep)
101
+ retry
85
102
  rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex
86
103
  self.bearer_token = nil
87
104
  self.oauth_session_expires_at = nil
88
105
  self.current_error = ex.message
89
106
  case ex.message
90
107
  when "Forbidden"
91
- self.current_error = "The user associated to OAuth credential, '#{self.oauth_client_id}', set has been deactivated."
108
+ self.current_error = "The user associated to OAuth credential set has been deactivated."
92
109
  self.status = 'Deactivated'
93
110
  else
94
111
  self.current_error = "Invalid login, please check client ID and Client Secret or URL endpoint"
@@ -98,12 +115,17 @@ module ZuoraAPI
98
115
  return self.status
99
116
  rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition => ex
100
117
  raise ex
101
- rescue *(CONNECTION_EXCEPTIONS).concat(CONNECTION_READ_EXCEPTIONS) => ex
102
- if !(tries -= 1).zero?
103
- Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"}
118
+ rescue *(CONNECTION_EXCEPTIONS + CONNECTION_READ_EXCEPTIONS) => ex
119
+ if !tries.zero?
120
+ tries -= 1
104
121
  sleep(self.timeout_sleep)
105
122
  retry
106
123
  else
124
+ if Rails.logger.class.to_s == "Ougai::Logger"
125
+ Rails.logger.error("OAuthLogin - Timed out will retry after #{self.timeout_sleep} seconds", ex)
126
+ else
127
+ Rails.logger.error("OAuthLogin - #{ex.class} Timed out will retry after #{self.timeout_sleep} seconds")
128
+ end
107
129
  self.current_error = "Invalid login, please check client ID and Client Secret or URL endpoint"
108
130
  self.status = 'Timeout'
109
131
  return self.status
@@ -1,3 +1,3 @@
1
1
  module ZuoraAPI
2
- VERSION = "1.7.00"
2
+ VERSION = "1.8.00"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zuora_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.00
4
+ version: 1.8.00
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zuora Strategic Solutions Group
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-05 00:00:00.000000000 Z
11
+ date: 2020-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler