zuora_api_oauth_alpha 2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Rakefile +6 -0
- data/lib/insights_api/login.rb +221 -0
- data/lib/zuora_api_oauth_alpha/exceptions.rb +86 -0
- data/lib/zuora_api_oauth_alpha/login.rb +707 -0
- data/lib/zuora_api_oauth_alpha/logins/basic.rb +139 -0
- data/lib/zuora_api_oauth_alpha/logins/oauth.rb +171 -0
- data/lib/zuora_api_oauth_alpha/version.rb +3 -0
- data/lib/zuora_api_oauth_alpha.rb +10 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 697c4df3ca9920d3096ca7346cc65d78c04add6999c39b34c365fa6963b2495d
|
4
|
+
data.tar.gz: 329058a2fe9d56f9b2c621bb7c5fe4fa7c846246f73f2c681a7a67c242fe6311
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 543f66de8b66a9a349351cfe4611a807e61dfa9071ae3254300e32e60e2a2c03612a85760ecf90dad22e10ec225ccf15a55e12afcd7126ba19308b2d26bd80f2
|
7
|
+
data.tar.gz: 24e3dfce9db371969124cdc1ea410dbc29968ab9e56cffd29c002399fcc93eee626a3c1eda7a564f3f6f12a59c9d322fc39a9ae651d2b826622bb49167376427
|
data/Rakefile
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
require "httparty"
|
2
|
+
module InsightsAPI
|
3
|
+
class Login
|
4
|
+
attr_accessor :api_token, :url
|
5
|
+
|
6
|
+
def initialize(api_token: nil, url: nil, **keyword_args)
|
7
|
+
@api_token = api_token
|
8
|
+
@url = url
|
9
|
+
@status = "Active"
|
10
|
+
end
|
11
|
+
|
12
|
+
def insight_getstatus(uuid)
|
13
|
+
response = HTTParty.get(
|
14
|
+
"https://#{@url}/export/status/#{uuid}",
|
15
|
+
:basic_auth => { :username => @api_token })
|
16
|
+
if response.code == 200
|
17
|
+
parsed = JSON.parse(response.body)
|
18
|
+
return parsed
|
19
|
+
#error handing here
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def data_export_insights_file(objecttype, segmentuuid, startDate, endDate, tries: 30)
|
24
|
+
status = data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30)
|
25
|
+
if status['status']== "COMPLETE"
|
26
|
+
signedUrl = status['signedUrl']
|
27
|
+
return get_file(file_name: "insights-#{startDate}-#{endDate}.csv", url: signedUrl, headers: {}, count: 3, file_type: "zip")
|
28
|
+
else
|
29
|
+
return status
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def data_export_insights(objecttype, segmentuuid, startDate, endDate, tries: 30)
|
34
|
+
status = insights_fetch_all(objecttype, segmentuuid, startDate, endDate)
|
35
|
+
if status['uuid'] == nil
|
36
|
+
return "Failed: #{status["response"]}"
|
37
|
+
else
|
38
|
+
fileid = status['uuid']
|
39
|
+
end
|
40
|
+
for retries in 1..tries
|
41
|
+
status = insight_getstatus(fileid)
|
42
|
+
if status['status']== "COMPLETE"
|
43
|
+
signedUrl = status['signedUrl']
|
44
|
+
return status
|
45
|
+
elsif status['status'] == "FAILED"
|
46
|
+
return status
|
47
|
+
else
|
48
|
+
sleep(60)
|
49
|
+
retries+=1
|
50
|
+
end
|
51
|
+
if retries > tries - 1
|
52
|
+
return "Timeout"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
return signedUrl
|
56
|
+
end
|
57
|
+
|
58
|
+
def insights_fetch_all(objecttype, segmentuuid, startDate, endDate)
|
59
|
+
if segmentuuid.is_a? Array
|
60
|
+
segmentsForAPI = segmentuuid.join('","')
|
61
|
+
elsif segmentuuid.is_a? String
|
62
|
+
segmentsForAPI = segmentuuid
|
63
|
+
elsif segmentuuid.is_a? Integer
|
64
|
+
segmentsForAPI = segmentuuid.to_s
|
65
|
+
else
|
66
|
+
raise "Error fetching Insights data: Segmentuuid must be either an array of uuids or an single uuid in string or interger format."
|
67
|
+
end
|
68
|
+
|
69
|
+
response = HTTParty.post(
|
70
|
+
"https://#{@url}/export/type/#{objecttype}",
|
71
|
+
:basic_auth => { :username => @api_token },
|
72
|
+
:headers => {'Content-Type'=> "Application/json"},
|
73
|
+
:body =>
|
74
|
+
'
|
75
|
+
{
|
76
|
+
"endDate": "' + (dateFormat(date: endDate)).to_s + '",
|
77
|
+
"startDate":"' + (dateFormat(date: startDate)).to_s + '",
|
78
|
+
"segments": [
|
79
|
+
"'+segmentsForAPI+'"
|
80
|
+
]
|
81
|
+
}
|
82
|
+
')
|
83
|
+
if response.code == 200
|
84
|
+
parsed = JSON.parse(response.body)
|
85
|
+
return parsed
|
86
|
+
else
|
87
|
+
return {"uuid"=> nil, "status"=>"Error", "signedUrl"=>"signedUrl", "response" => response.body}
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def dateFormat(date: nil)
|
92
|
+
date ||= DateTime.now
|
93
|
+
if date.is_a? String
|
94
|
+
if date.include? "T"
|
95
|
+
return (date.to_datetime).to_s
|
96
|
+
else
|
97
|
+
return (date.to_date).to_s + "T#{DateTime.now.to_s(:time)}:00Z"
|
98
|
+
end
|
99
|
+
elsif date.instance_of?(DateTime)
|
100
|
+
return date.to_s
|
101
|
+
elsif date.instance_of?(Date)
|
102
|
+
return (date.to_date).to_s + "T#{DateTime.now.to_s(:time)}:00Z"
|
103
|
+
else
|
104
|
+
raise "Please pass in a in format of 'YYYY-MM-DD', 'YYYY-MM-DDT00:00:00+00:00' ruby Date, or ruby DateTime"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def upload_into_insights(dataSourceName, recordType, batchDate, filePath)
|
109
|
+
begin
|
110
|
+
temp_date = dateFormat(date: batchDate)
|
111
|
+
response = HTTParty.post(
|
112
|
+
"https://#{@url}/files/upload",
|
113
|
+
:basic_auth => { :username => @api_token }, :body => {
|
114
|
+
:dataSource => dataSourceName, :recordType => recordType,
|
115
|
+
:batchDate => temp_date})
|
116
|
+
parsed = JSON.parse(response.body)
|
117
|
+
signedUrl = parsed['signedUrl']
|
118
|
+
if !File.extname(filePath) == ".gz"
|
119
|
+
zipPath = gzip_file(filePath)
|
120
|
+
else
|
121
|
+
zipPath = filePath
|
122
|
+
end
|
123
|
+
|
124
|
+
gzippedFile = File.open(zipPath)
|
125
|
+
post = HTTParty.put(signedUrl,
|
126
|
+
:body => gzippedFile.read)
|
127
|
+
if post.code == 200
|
128
|
+
return {"status"=>"COMPLETE", "signedUrl"=>"#{signedUrl}", "response" => post.code, "batchDate" => "#{temp_date}"}
|
129
|
+
else
|
130
|
+
return {"status"=>"Error", "signedUrl"=>"#{signedUrl}", "response" => post.code, "message"=> "#{post.message}", "batchDate" => "#{temp_date}"}
|
131
|
+
end
|
132
|
+
rescue Exception => e
|
133
|
+
Rails.logger.debug "[ZuoraGem]: While uploading to insights Error: #{e}"
|
134
|
+
raise e
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def describe(type: "ACCOUNT", object: "attributes")
|
139
|
+
url = "https://#{@url}/export/#{object}/#{type}"
|
140
|
+
uri = URI.parse(url)
|
141
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
142
|
+
http.use_ssl = true
|
143
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
144
|
+
begin
|
145
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
146
|
+
request.basic_auth(@api_token, "")
|
147
|
+
return http.request(request)
|
148
|
+
rescue Exception => e
|
149
|
+
Rails.logger.debug "[ZuoraGem]: While describing Zoura Insights objects: #{e}"
|
150
|
+
return "Failed"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def get_file(file_name: nil, url: nil, basic: {:username => nil, :password => nil}, headers: {}, count: 3, file_type: "zip")
|
155
|
+
tries ||= 2
|
156
|
+
temp_file = nil
|
157
|
+
uri = URI(url)
|
158
|
+
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
159
|
+
request = Net::HTTP::Get.new(uri)
|
160
|
+
headers.each do |k,v|
|
161
|
+
request["#{k}"] = v
|
162
|
+
end
|
163
|
+
request.basic_auth(basic[:username], basic[:password]) if (!basic[:username].blank? && !basic[:password].blank?)
|
164
|
+
http.request request do |response|
|
165
|
+
case response
|
166
|
+
when Net::HTTPNotFound
|
167
|
+
Rails.logger.fatal("[ZuoraGem]: 404 - Not Found")
|
168
|
+
raise response
|
169
|
+
|
170
|
+
when Net::HTTPUnauthorized
|
171
|
+
raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new(zuora_client.current_error) if count <= 0
|
172
|
+
Rails.logger.fatal("[ZuoraGem]: Retry")
|
173
|
+
zuora_client.new_session
|
174
|
+
return get_file(:url => url, :count => count - 1, :headers => headers)
|
175
|
+
|
176
|
+
when Net::HTTPClientError
|
177
|
+
Rails.logger.debug("[ZuoraGem]: #{response}")
|
178
|
+
raise response
|
179
|
+
|
180
|
+
when Net::HTTPOK
|
181
|
+
Tempfile.open([file_name.rpartition('.').first, ".#{file_name.rpartition('.').last}"], "#{Rails.root}/tmp") do |tmp_file|
|
182
|
+
temp_file ||= tmp_file
|
183
|
+
tmp_file.binmode if (response.to_hash["content-type"].include?("application/zip") || response.to_hash["content-type"] == "application/zip")
|
184
|
+
response.read_body do |chunk|
|
185
|
+
tmp_file.write chunk.force_encoding("UTF-8")
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
rescue => ex
|
193
|
+
if !(tries -= 1).zero?
|
194
|
+
sleep 3
|
195
|
+
retry
|
196
|
+
else
|
197
|
+
raise ex
|
198
|
+
end
|
199
|
+
else
|
200
|
+
return temp_file
|
201
|
+
end
|
202
|
+
|
203
|
+
def gzip_file(filePath)
|
204
|
+
require "zip"
|
205
|
+
Zlib::GzipWriter.open(filePath + ".gz") do |gzip|
|
206
|
+
open(filePath, "rb") do |f|
|
207
|
+
f.each_chunk() {|chunk| gzip.write chunk }
|
208
|
+
end
|
209
|
+
gzip.close
|
210
|
+
end
|
211
|
+
return filePath+ ".gz"
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
class File
|
218
|
+
def each_chunk(chunk_size=2**16)
|
219
|
+
yield read(chunk_size) until eof?
|
220
|
+
end
|
221
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module ZuoraAPI
|
2
|
+
module Exceptions
|
3
|
+
class Error < StandardError; end
|
4
|
+
class AuthorizationNotPerformed < Error; end
|
5
|
+
class ZuoraAPISessionError < Error
|
6
|
+
attr_reader :code, :response
|
7
|
+
attr_writer :default_message
|
8
|
+
|
9
|
+
def initialize(message = nil)
|
10
|
+
@message = message
|
11
|
+
@default_message = "Error with Zuora Session."
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
@message || @default_message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class ZuoraAPIError < Error
|
20
|
+
attr_reader :code, :response, :errors, :successes
|
21
|
+
attr_writer :default_message
|
22
|
+
|
23
|
+
def initialize(message = nil,response=nil, code =nil, errors = [], successes = [])
|
24
|
+
@code = code
|
25
|
+
@message = message
|
26
|
+
@response = response
|
27
|
+
@default_message = "Error communicating with Zuora."
|
28
|
+
@errors = errors
|
29
|
+
@successes = successes
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_s
|
33
|
+
@message || @default_message
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ZuoraAPIRequestLimit < Error
|
38
|
+
attr_reader :code, :response
|
39
|
+
attr_writer :default_message
|
40
|
+
|
41
|
+
def initialize(message = nil,response=nil, code =nil)
|
42
|
+
@code = code
|
43
|
+
@message = message
|
44
|
+
@response = response
|
45
|
+
@default_message = "Your request limit has been exceeded for zuora."
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_s
|
49
|
+
@message || @default_message
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class ZuoraAPILockCompetition < Error
|
54
|
+
attr_reader :code, :response
|
55
|
+
attr_writer :default_message
|
56
|
+
|
57
|
+
def initialize(message = nil,response=nil, code =nil)
|
58
|
+
@code = code
|
59
|
+
@message = message
|
60
|
+
@response = response
|
61
|
+
@default_message = "Operation failed due to lock competition. Please retry"
|
62
|
+
end
|
63
|
+
|
64
|
+
def to_s
|
65
|
+
@message || @default_message
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
class ZuoraAPIAuthenticationTypeError < Error
|
71
|
+
attr_reader :code, :response
|
72
|
+
attr_writer :default_message
|
73
|
+
|
74
|
+
def initialize(message = nil,response=nil, code =nil)
|
75
|
+
@code = code
|
76
|
+
@message = message
|
77
|
+
@response = response
|
78
|
+
@default_message = "Authentication type is not supported by this Login"
|
79
|
+
end
|
80
|
+
|
81
|
+
def to_s
|
82
|
+
@message || @default_message
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|