Class: ZuoraAPI::Login
- Inherits:
-
Object
- Object
- ZuoraAPI::Login
- Defined in:
- lib/zuora_api/login.rb
Constant Summary collapse
- ENVIRONMENTS =
[SANDBOX = 'Sandbox', PRODUCTION = 'Production', PREFORMANCE = 'Preformance', SERVICES = 'Services', UNKNOWN = 'Unknown' ]
- REGIONS =
[EU = 'EU', US = 'US' ]
Instance Attribute Summary collapse
-
#current_error ⇒ Object
Returns the value of attribute current_error.
-
#current_session ⇒ Object
Returns the value of attribute current_session.
-
#entity_id ⇒ Object
Returns the value of attribute entity_id.
-
#environment ⇒ Object
Returns the value of attribute environment.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#password ⇒ Object
Returns the value of attribute password.
-
#region ⇒ Object
Returns the value of attribute region.
-
#status ⇒ Object
Returns the value of attribute status.
-
#tenant_id ⇒ Object
Returns the value of attribute tenant_id.
-
#tenant_name ⇒ Object
Returns the value of attribute tenant_name.
-
#timeout_sleep ⇒ Object
Returns the value of attribute timeout_sleep.
-
#url ⇒ Object
Returns the value of attribute url.
-
#user_info ⇒ Object
Returns the value of attribute user_info.
-
#username ⇒ Object
Returns the value of attribute username.
-
#wsdl_number ⇒ Object
Returns the value of attribute wsdl_number.
Class Method Summary collapse
Instance Method Summary collapse
- #aqua_endpoint(url = "") ⇒ Object
- #aqua_query(queryName: '', query: '', version: '1.2', jobName: 'Aqua', partner: '', project: '') ⇒ Object
- #checkJRStatus(jrNumber) ⇒ Object
- #createJournalRun(call) ⇒ Object
- #dateFormat ⇒ Object
- #describe_call(object = nil) ⇒ Object
- #fileURL(url = "") ⇒ Object
- #get_catalog(page_size: 40) ⇒ Object
- #get_file(url: nil, headers: {}, count: 3, z_session: true, tempfile: true, file_path: defined?(Rails.root.join('tmp')) ? Rails.root.join('tmp') : Pathname.new(Dir.pwd), timeout_retries: 2, timeout: 120, **execute_params) ⇒ Object
- #get_session ⇒ Object
- #getDataSourceExport(query, extract: true, encrypted: false, zip: true) ⇒ Object
- #getFileById(id: "2c92c0f85e7f88ff015e86b8f8f4517f") ⇒ Object
-
#initialize(username: nil, password: nil, status: nil, url: nil, entity_id: nil, session: nil, **keyword_args) ⇒ Login
constructor
A new instance of Login.
- #new_session(debug: false) ⇒ Object
- #query(query, parse = false) ⇒ Object
- #raise_errors(type: :SOAP, body: nil, response: nil) ⇒ Object
- #rest_call(method: :get, body: {}, headers: {}, url: rest_endpoint("catalog/products?pageSize=4"), debug: true, errors: [ZuoraAPI::Exceptions::ZuoraAPISessionError, ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition], z_session: true, timeout_retry: false, timeout: 120, **keyword_args) ⇒ Object
- #rest_endpoint(url = "") ⇒ Object
- #soap_call(ns1: 'ns1', ns2: 'ns2', batch_size: nil, single_transaction: false, debug: true, errors: [ZuoraAPI::Exceptions::ZuoraAPISessionError, ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition], z_session: true, timeout_retry: false, timeout: 120, **keyword_args) ⇒ Object
- #update_create_tenant ⇒ Object
- #update_environment ⇒ Object
Constructor Details
#initialize(username: nil, password: nil, status: nil, url: nil, entity_id: nil, session: nil, **keyword_args) ⇒ Login
Returns a new instance of Login.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/zuora_api/login.rb', line 9 def initialize(username: nil, password: nil, status: nil, url: nil, entity_id: nil, session: nil, **keyword_args) @username = username @password = password @url = url @entity_id = entity_id @current_session = session @errors = Hash.new @status = status.blank? ? "Active" : status @user_info = Hash.new self.update_environment @timeout_sleep = 5 end |
Instance Attribute Details
#current_error ⇒ Object
Returns the value of attribute current_error.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def current_error @current_error end |
#current_session ⇒ Object
Returns the value of attribute current_session.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def current_session @current_session end |
#entity_id ⇒ Object
Returns the value of attribute entity_id.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def entity_id @entity_id end |
#environment ⇒ Object
Returns the value of attribute environment.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def environment @environment end |
#errors ⇒ Object
Returns the value of attribute errors.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def errors @errors end |
#password ⇒ Object
Returns the value of attribute password.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def password @password end |
#region ⇒ Object
Returns the value of attribute region.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def region @region end |
#status ⇒ Object
Returns the value of attribute status.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def status @status end |
#tenant_id ⇒ Object
Returns the value of attribute tenant_id.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def tenant_id @tenant_id end |
#tenant_name ⇒ Object
Returns the value of attribute tenant_name.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def tenant_name @tenant_name end |
#timeout_sleep ⇒ Object
Returns the value of attribute timeout_sleep.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def timeout_sleep @timeout_sleep end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def url @url end |
#user_info ⇒ Object
Returns the value of attribute user_info.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def user_info @user_info end |
#username ⇒ Object
Returns the value of attribute username.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def username @username end |
#wsdl_number ⇒ Object
Returns the value of attribute wsdl_number.
7 8 9 |
# File 'lib/zuora_api/login.rb', line 7 def wsdl_number @wsdl_number end |
Class Method Details
.endpoints ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/zuora_api/login.rb', line 31 def self.endpoints return {"US" => {"Sandbox" => "https://apisandbox.zuora.com/apps/services/a/", "Production" => "https://www.zuora.com/apps/services/a/", "Performance" => "https://pt1.zuora.com/apps/services/a/", "Services" => "https://services347.zuora.com/apps/services/a/"}, "EU" => {"Sandbox" => "https://sandbox.eu.zuora.com/apps/services/a/", "Production" => "https://eu.zuora.com/apps/services/a/", "Performance" => "https://pt1.eu.zuora.com/apps/services/a/", "Services" => "https://services347.eu.zuora.com/apps/services/a/"} } end |
.environments ⇒ Object
23 24 25 |
# File 'lib/zuora_api/login.rb', line 23 def self.environments %w(Sandbox Production Services Performance) end |
.regions ⇒ Object
27 28 29 |
# File 'lib/zuora_api/login.rb', line 27 def self.regions %w(US EU) end |
Instance Method Details
#aqua_endpoint(url = "") ⇒ Object
64 65 66 |
# File 'lib/zuora_api/login.rb', line 64 def aqua_endpoint(url="") return "#{self.url.split("/apps").first}/apps/api/".concat(url) end |
#aqua_query(queryName: '', query: '', version: '1.2', jobName: 'Aqua', partner: '', project: '') ⇒ Object
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/zuora_api/login.rb', line 404 def aqua_query(queryName: '', query: '', version: '1.2', jobName: 'Aqua',partner: '', project: '') params = { "format" => 'csv', "version" => version, "name" => jobName, "encrypted" => 'none', "useQueryLabels" => 'true', "partner" => partner, "project" => project, "queries" => [{ "name" => queryName, "query" => query, "type" => 'zoqlexport' }] } response = self.rest_call(method: :post, body: params.to_json, url: self.aqua_endpoint("batch-query/")) if(response[0]["id"].nil?) raise ZuoraAPI::Exceptions::ZuoraAPIError.new("Error in AQuA Process. #{response}") end return getFileById(id: response[0]["id"]) end |
#checkJRStatus(jrNumber) ⇒ Object
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 |
# File 'lib/zuora_api/login.rb', line 811 def checkJRStatus(jrNumber) Rails.logger.info('Journal Run') {"Check for completion"} url = rest_endpoint("/journal-runs/#{jrNumber}") uri = URI(url) req = Net::HTTP::Get.new(uri,initheader = {'Content-Type' =>'application/json'}) req.basic_auth self.username, self.password response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| http.request req end result = JSON.parse(response.body) if result["success"] if !(result["status"].eql? "Completed") sleep(20.seconds) end return result["status"] else = result["reasons"][0]["message"] Rails.logger.info('Journal Run') {"Checking status of journal run failed with message #{}"} end return "failure" end |
#createJournalRun(call) ⇒ Object
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/zuora_api/login.rb', line 788 def createJournalRun(call) url = rest_endpoint("/journal-runs") uri = URI(url) req = Net::HTTP::Post.new(uri,initheader = {'Content-Type' =>'application/json'}) req.basic_auth self.username, self.password req.body = call response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true) do |http| http.request req end Rails.logger.debug('Journal Run') {"Response #{response.code} #{response.}: #{response.body}"} result = JSON.parse(response.body) if result["success"] jrNumber = result["journalRunNumber"] return jrNumber else = result["reasons"][0]["message"] Rails.logger.debug('Journal Run') {"Journal Run failed with message #{}"} return result end end |
#dateFormat ⇒ Object
86 87 88 |
# File 'lib/zuora_api/login.rb', line 86 def dateFormat return self.wsdl_number > 68 ? '%Y-%m-%d' : '%Y-%m-%dT%H:%M:%S' end |
#describe_call(object = nil) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'lib/zuora_api/login.rb', line 442 def describe_call(object = nil) tries ||= 2 self.get_session base = self.url.include?(".com") ? self.url.split(".com")[0].concat(".com") : self.url.split(".eu")[0].concat(".eu") url = object ? "#{base}/apps/api/describe/#{object}" : "#{base}/apps/api/describe/" headers = !self.entity_id.blank? ? {"entityId" => self.entity_id, 'Content-Type' => "text/xml; charset=utf-8"} : {'Content-Type' => "text/xml; charset=utf-8"} response = HTTParty.get(url, :headers => headers , basic_auth: {:username => self.username, :password => self.password}, :timeout => 120) output_xml = Nokogiri::XML(response.body) des_hash = Hash.new if object == nil output_xml.xpath("//object").each do |object| temp = {:label => object.xpath(".//label").text, :url => object.attributes["href"].value } des_hash[object.xpath(".//name").text] = temp end else output_xml.xpath("//field").each do |object| temp = {:label => object.xpath(".//label").text,:selectable => object.xpath(".//selectable").text, :createable => object.xpath(".//label").text == "ID" ? "false" : object.xpath(".//createable").text, :filterable => object.xpath(".//filterable").text, :updateable => object.xpath(".//label").text == "ID" ? "false" : object.xpath(".//updateable").text, :custom => object.xpath(".//custom").text,:maxlength => object.xpath(".//maxlength").text, :required => object.xpath(".//required").text, :type => object.xpath(".//type").text, :context => object.xpath(".//context").collect{ |x| x.text } } temp[:options] = object.xpath(".//option").collect{ |x| x.text } if object.xpath(".//option").size > 0 des_hash[object.xpath(".//name").text.to_sym] = temp des_hash[:fieldsToNull] = {:label => "FieldsToNull",:selectable => "false", :createable => "false",:filterable => "false", :updateable => "true",:custom => "false", :required => "false",:type => "picklist", :maxlength => "" ,:context => ["soap"], :options => des_hash.map {|k,v| k if v[:updateable] == "true" && v[:required] == "false"}.compact.uniq } end des_hash[:related_objects] = output_xml.xpath(".//related-objects").xpath(".//object").map{ |x| [x.xpath(".//name").text.to_sym, [ [:url, x.attributes["href"].value], [:label, x.xpath(".//name").text ] ].to_h] }.to_h end rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex if !(tries -= 1).zero? Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"} sleep(self.timeout_sleep) retry else raise ex end rescue => ex raise ex else return des_hash end |
#fileURL(url = "") ⇒ Object
82 83 84 |
# File 'lib/zuora_api/login.rb', line 82 def fileURL(url="") return self.url.split(".com").first.concat(".com/apps/api/file/").concat(url) end |
#get_catalog(page_size: 40) ⇒ Object
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/zuora_api/login.rb', line 554 def get_catalog(page_size: 40) products, catalog_map, response = [{}, {}, {'nextPage' => self.rest_endpoint("catalog/products?pageSize=#{page_size}") }] while !response["nextPage"].blank? url = self.rest_endpoint(response["nextPage"].split('/v1/').last) Rails.logger.debug("Fetch Catalog URL #{url}") output_json, response = self.rest_call(:debug => false, :url => url, :errors => [ZuoraAPI::Exceptions::ZuoraAPISessionError], :timeout_retry => true ) if !output_json['success'] =~ (/(true|t|yes|y|1)$/i) || output_json['success'].class != TrueClass raise ZuoraAPI::Exceptions::ZuoraAPIError.new("Error Getting Catalog: #{output_json}") end output_json["products"].each do |product| catalog_map[product["id"]] = {"productId" => product["id"]} rateplans = {} product["productRatePlans"].each do |rateplan| catalog_map[rateplan["id"]] = {"productId" => product["id"], "productRatePlanId" => rateplan["id"]} charges = {} rateplan["productRatePlanCharges"].each do |charge| catalog_map[charge["id"]] = {"productId" => product["id"], "productRatePlanId" => rateplan["id"], "productRatePlanChargeId" => charge["id"]} charges[charge["id"]] = charge.merge({"productId" => product["id"], "productName" => product["name"], "productRatePlanId" => rateplan["id"], "productRatePlanName" => rateplan["name"] }) end rateplan["productRatePlanCharges"] = charges rateplans[rateplan["id"]] = rateplan.merge({"productId" => product["id"], "productName" => product["name"]}) end product["productRatePlans"] = rateplans products[product['id']] = product end end return products, catalog_map end |
#get_file(url: nil, headers: {}, count: 3, z_session: true, tempfile: true, file_path: defined?(Rails.root.join('tmp')) ? Rails.root.join('tmp') : Pathname.new(Dir.pwd), timeout_retries: 2, timeout: 120, **execute_params) ⇒ Object
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 |
# File 'lib/zuora_api/login.rb', line 586 def get_file(url: nil, headers: {}, count: 3, z_session: true, tempfile: true, file_path: defined?(Rails.root.join('tmp')) ? Rails.root.join('tmp') : Pathname.new(Dir.pwd), timeout_retries: 2, timeout: 120, **execute_params) raise "file_path must be of class Pathname" if file_path.class != Pathname #Make sure directory exists require 'fileutils' FileUtils.mkdir_p(file_path) unless File.exists?(file_path) begin uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.read_timeout = timeout #Seconds http.use_ssl = true if uri.scheme.downcase == 'https' headers = headers.merge({"Authorization" => "ZSession #{self.get_session}"}) if z_session response_save = nil http.request_get(uri.path, headers) do |response| response_save = response case response when Net::HTTPNotFound Rails.logger.fatal("404 - Not Found") raise response when Net::HTTPUnauthorized raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new(self.current_error) if count <= 0 Rails.logger.fatal("Unauthorized: Retry") self.new_session return get_file(:url => url, :headers => headers, :count => count - 1, :z_session => z_session, :tempfile => tempfile, :file_path => file_path, :timeout_retries => timeout_retries, :timeout => timeout) when Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError Rails.logger.fatal("#{response.class} timeout - retry") return get_file(:url => url, :headers => headers, :count => count, :z_session => z_session, :tempfile => tempfile, :file_path => file_path, :timeout_retries => timeout_retries - 1, :timeout => timeout) when Net::HTTPClientError Rails.logger.fatal("Login: #{self.username} Export") raise response when Net::HTTPOK headers = {} response.each_header do |k,v| headers[k] = v end Rails.logger.debug("Headers: #{headers.to_s}") size, export_progress = [0, 0] encoding, type, full_filename = [nil, nil, nil] if response.header["Content-Disposition"].present? case response.header["Content-Disposition"] when /.*; filename\*=.*/ full_filename = /.*; filename\*=(.*)''(.*)/.match(response.header["Content-Disposition"])[2].strip encoding = /.*; filename\*=(.*)''(.*)/.match(response.header["Content-Disposition"])[1].strip when /.*; filename=/ full_filename = /.*; filename=(.*)/.match(response.header["Content-Disposition"])[1].strip end file_ending = ".#{full_filename.partition(".").last}" end #If user supplied a filename use it, else default to content header filename, else default to uri pattern filename = full_filename.present? ? full_filename.split(file_ending).first : File.basename(uri.path).rpartition('.').first if response.header["Content-Type"].present? case response.header["Content-Type"] when /.*;charset=.*/ type = /(.*);charset=(.*)/.match(response.header["Content-Type"])[1] encoding = /(.*);charset=(.*)/.match(response.header["Content-Type"])[2] else type = response.header["Content-Type"] encoding ||= 'UTF-8' end end Rails.logger.info("File: #{filename}#{file_ending} #{encoding} #{type}") if response.header["Content-Length"].present? export_size = response.header["Content-Length"].to_i elsif response.header["ContentLength"].present? export_size = response.header["ContentLength"].to_i end file_handle = nil = Time.now.to_i if tempfile require 'tempfile' file_handle = ::Tempfile.new(["#{filename}_#{}", "#{file_ending}"], file_path) file_handle.binmode else file_handle = File.new(file_path.join("#{filename}_#{}#{file_ending}"), "w+") file_handle.binmode end response.read_body do |chunk| file_handle << chunk.force_encoding(encoding) if defined?(export_size) && export_size != 0 && export_size.class == Integer size += chunk.size new_progress = (size * 100) / export_size unless new_progress == export_progress Rails.logger.debug("Login: #{self.username} Export Downloading %s (%3d%%)" % [filename, new_progress]) end export_progress = new_progress end end file_handle.close Rails.logger.info("Filepath: #{file_handle.path} Size: #{File.size(file_handle.path).to_f/1000000} mb") return file_handle end end rescue Exception => e Rails.logger.fatal('GetFile') {"Download Failed: #{response_save} - #{e.class} : #{e.}"} Rails.logger.fatal('GetFile') {"Download Failed: #{e.backtrace.join("\n")}"} raise end end |
#get_session ⇒ Object
202 203 204 205 206 207 |
# File 'lib/zuora_api/login.rb', line 202 def get_session Rails.logger.debug("Create new session") if self.current_session.blank? self.new_session if self.current_session.blank? raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new(self.current_error) if self.status != 'Active' return self.current_session end |
#getDataSourceExport(query, extract: true, encrypted: false, zip: true) ⇒ Object
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
# File 'lib/zuora_api/login.rb', line 700 def getDataSourceExport(query, extract: true, encrypted: false, zip: true) Rails.logger.info('Export') {"Build export"} Rails.logger.debug('Export query') {"#{query}"} request = Nokogiri::XML::Builder.new do |xml| xml['SOAP-ENV'].Envelope('xmlns:SOAP-ENV' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:ns2' => "http://object.api.zuora.com/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:ns1' => "http://api.zuora.com/") do xml['SOAP-ENV'].Header do xml['ns1'].SessionHeader do xml['ns1'].session self.current_session end end xml['SOAP-ENV'].Body do xml['ns1'].create do xml['ns1'].zObjects('xsi:type' => "ns2:Export") do xml['ns2'].Format 'csv' xml['ns2'].Zip zip xml['ns2'].Name 'googman' xml['ns2'].Query query xml['ns2'].Encrypted encrypted end end end end end response_query = HTTParty.post(self.url, body: request.to_xml, headers: {'Content-Type' => "application/json; charset=utf-8"}, :timeout => 120) output_xml = Nokogiri::XML(response_query.body) raise 'Export Creation Unsuccessful : ' + output_xml.xpath('//ns1:Message', 'ns1' =>'http://api.zuora.com/').text if output_xml.xpath('//ns1:Success', 'ns1' =>'http://api.zuora.com/').text != "true" id = output_xml.xpath('//ns1:Id', 'ns1' =>'http://api.zuora.com/').text confirmRequest = Nokogiri::XML::Builder.new do |xml| xml['SOAP-ENV'].Envelope('xmlns:SOAP-ENV' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:ns2' => "http://object.api.zuora.com/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:ns1' => "http://api.zuora.com/") do xml['SOAP-ENV'].Header do xml['ns1'].SessionHeader do xml['ns1'].session self.current_session end end xml['SOAP-ENV'].Body do xml['ns1'].query do xml['ns1'].queryString "SELECT Id, CreatedById, CreatedDate, Encrypted, FileId, Format, Name, Query, Size, Status, StatusReason, UpdatedById, UpdatedDate, Zip From Export where Id = '#{id}'" end end end end result = 'Waiting' while result != "Completed" sleep 3 response_query = HTTParty.post(self.url, body: confirmRequest.to_xml, headers: {'Content-Type' => "application/json; charset=utf-8"}, :timeout => 120) output_xml = Nokogiri::XML(response_query.body) result = output_xml.xpath('//ns2:Status', 'ns2' =>'http://object.api.zuora.com/').text raise "Export Creation Unsuccessful : #{output_xml.xpath('//ns1:Message', 'ns1' =>'http://api.zuora.com/').text}" if result.blank? || result == "Failed" end file_id = output_xml.xpath('//ns2:FileId', 'ns2' =>'http://object.api.zuora.com/').text Rails.logger.info('Export') {'=====> Export finished'} export_file = get_file(:url => self.fileURL(file_id)) export_file_path = export_file.path Rails.logger.info('Export') {"=====> Export path #{export_file.path}"} if extract && zip require "zip" new_path = export_file_path.partition('.zip').first zipped = Zip::File.open(export_file_path) file_handle = zipped.entries.first file_handle.extract(new_path) File.delete(export_file_path) return new_path else return export_file_path end end |
#getFileById(id: "2c92c0f85e7f88ff015e86b8f8f4517f") ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/zuora_api/login.rb', line 426 def getFileById(id: "2c92c0f85e7f88ff015e86b8f8f4517f") response = nil result = "new" while result != "completed" do sleep(2)#sleep 2 seconds response, fullResponse = self.rest_call(method: :get, body: {}, url: self.aqua_endpoint("batch-query/jobs/#{id}")) result = response["batches"][0]["status"] if result == "error" raise ZuoraAPI::Exceptions::ZuoraAPIError.new("Aqua Error: #{response}") break end end fileId = response["batches"][0]["fileId"] return self.get_file(url: self.aqua_endpoint("file/#{fileId}")) end |
#new_session(debug: false) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/zuora_api/login.rb', line 90 def new_session(debug: false) tries ||= 2 request = Nokogiri::XML::Builder.new do |xml| xml['SOAP-ENV'].Envelope('xmlns:SOAP-ENV' =>"http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:api' => "http://api.zuora.com/" ) do if (self.password.blank? && !self.current_session.blank?) Rails.logger.debug("Method [Session]") xml['SOAP-ENV'].Header do xml['api'].SessionHeader do xml['api'].session self.current_session end end xml['SOAP-ENV'].Body do xml['api'].getUserInfo end else xml['SOAP-ENV'].Header xml['SOAP-ENV'].Body do xml['api'].login do xml['api'].username self.username xml['api'].password self.password xml['api'].entityId self.entity_id if !self.entity_id.blank? end end end end end input_xml = Nokogiri::XML(request.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip) input_xml.xpath('//ns1:session', 'ns1' =>'http://api.zuora.com/').children.remove Rails.logger.debug('Connect') {"Request SOAP XML: #{input_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip}"} if debug @response_query = HTTParty.post(self.url,:body => request.to_xml, :headers => {'Content-Type' => "text/xml; charset=utf-8"}, :timeout => 10) @output_xml = Nokogiri::XML(@response_query.body) Rails.logger.debug('Connect') {"Response SOAP XML: #{@output_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip}"} if debug if !@response_query.success? self.current_session = nil if @output_xml.namespaces.size > 0 && @output_xml.xpath('//soapenv:Fault').size > 0 self.current_error = @output_xml.xpath('//fns:FaultMessage', 'fns' =>'http://fault.api.zuora.com/').text if self.current_error.include?('deactivated') self.status = 'Deactivated' self.current_error = 'Deactivated user login, please check with Zuora tenant administrator' self.errors[:username] = self.current_error elsif self.current_error.include?('inactive') self.status = 'Inactive' self.current_error = 'Inactive user login, please check with Zuora tenant administrator' self.errors[:username] = self.current_error elsif self.current_error.include?("invalid username or password") || self.current_error.include?("Invalid login. User name and password do not match.") self.status = 'Invalid Login' self.current_error = 'Invalid login, please check username and password or URL endpoint' self.errors[:username] = self.current_error self.errors[:password] = self.current_error elsif self.current_error.include?('unsupported version') self.status = 'Unsupported API Version' self.current_error = 'Unsupported API version, please verify URL endpoint' self.errors[:url] = self.current_error elsif self.current_error.include?('invalid api version') self.status = 'Invalid API Version' self.current_error = 'Invalid API version, please verify URL endpoint' self.errors[:url] = self.current_error elsif self.current_error.include?('invalid session') self.status = 'Invalid Session' self.current_error = 'Session invalid, please update session and verify URL endpoint' self.errors[:session] = self.current_error elsif self.current_error.include?('Your IP address') self.status = 'Restricted IP' self.current_error = 'IP restricted, contact Zuora tenant administrator and remove IP restriction' self.errors[:base] = self.current_error elsif self.current_error.include?('This account has been locked') self.status = 'Locked' self.current_error = 'Locked user login, please wait or navigate to Zuora to unlock user' self.errors[:username] = self.current_error else self.status = 'Unknown' self.current_error = @output_xml.xpath('//faultstring').text if self.current_error.blank? self.errors[:base] = self.current_error end else self.current_error = "Code = #{@response_query.code} Message = #{@response_query.body.to_s}" self.status = 'No Service' end else #If Session only is used for Gem TODO Depercate if (self.password.blank? && self.current_session.present?) self.current_session = self.current_session self.username = @output_xml.xpath('//ns1:Username', 'ns1' =>'http://api.zuora.com/').text if self.username.blank? #Username & password combo elsif (self.password.present? && self.username.present?) retrieved_session = @output_xml.xpath('//ns1:Session', 'ns1' =>'http://api.zuora.com/').text raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("No session found for api call.") if retrieved_session.blank? self.current_session = retrieved_session end self.current_error = nil self.status = 'Active' end return self.status rescue Net::ReadTimeout, Net::OpenTimeout, Errno::EPIPE, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex if !(tries -= 1).zero? Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"} sleep(self.timeout_sleep) retry else self.current_error = "Request timed out. Try again" self.status = 'Timeout' return self.status end end |
#query(query, parse = false) ⇒ Object
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
# File 'lib/zuora_api/login.rb', line 772 def query(query, parse = false) Rails.logger.debug('query') {"Querying Zuora for #{query}"} output_xml, input_xml = self.soap_call({:debug => false, :timeout_retry => true}) do |xml| xml['ns1'].query do xml['ns1'].queryString query end end if parse return [] if output_xml.xpath('//ns1:size', 'ns1' =>'http://api.zuora.com/').text == '0' data = output_xml.xpath('//ns1:records', 'ns1' =>'http://api.zuora.com/').map {|record| record.children.map {|element| [element.name, element.text]}.to_h} return data else return output_xml end end |
#raise_errors(type: :SOAP, body: nil, response: nil) ⇒ Object
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/zuora_api/login.rb', line 279 def raise_errors(type: :SOAP, body: nil, response: nil) case type when :SOAP error = body.xpath('//fns:FaultCode', 'fns' =>'http://fault.api.zuora.com/').text = body.xpath('//fns:FaultMessage', 'fns' =>'http://fault.api.zuora.com/').text if error.blank? || .blank? error = body.xpath('//faultcode').text = body.xpath('//faultstring').text end if error.blank? || .blank? error = body.xpath('//ns1:Code', 'ns1' =>'http://api.zuora.com/').text = body.xpath('//ns1:Message', 'ns1' =>'http://api.zuora.com/').text end #Update/Create/Delete Calls with multiple requests and responses if body.xpath('//ns1:result', 'ns1' =>'http://api.zuora.com/').size > 0 && body.xpath('//ns1:Errors', 'ns1' =>'http://api.zuora.com/').size > 0 error = [] success = [] body.xpath('//ns1:result', 'ns1' =>'http://api.zuora.com/').each_with_index do |call, object_index| if call.xpath('./ns1:Success', 'ns1' =>'http://api.zuora.com/').text == 'false' = "#{call.xpath('./*/ns1:Code', 'ns1' =>'http://api.zuora.com/').text}::#{call.xpath('./*/ns1:Message', 'ns1' =>'http://api.zuora.com/').text}" error.push() else success.push(call.xpath('./ns1:Id', 'ns1' =>'http://api.zuora.com/').text) end end end #By default response if not passed in for SOAP as all SOAP is 200 if error.present? if error.class == String if error == "INVALID_SESSION" raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("#{error}::#{}", body, response.present? ? response.code : nil ) end if error == "REQUEST_EXCEEDED_LIMIT" raise ZuoraAPI::Exceptions::ZuoraAPIRequestLimit.new("#{error}::#{}", body, response.present? ? response.code : nil) end if error == "LOCK_COMPETITION" raise ZuoraAPI::Exceptions::ZuoraAPILockCompetition.new("#{error}::#{}", body, response.present? ? response.code : nil) end if error.present? raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{error}::#{}", body, response.present? ? response.code : nil) end elsif error.class == Array raise ZuoraAPI::Exceptions::ZuoraAPIError.new(error.group_by {|v| v}.map {|k,v| "(#{v.size}x) - #{k}"}.join(', '), body, response.present? ? response.code : nil, error, success) end end when :JSON if body.class == Hash && (!body["success"] || !body["Success"] || response.code != 200) = (body["reasons"] || []).map {|error| error['message']}.compact codes_array = (body["reasons"] || []).map {|error| error['code']}.compact #Authentication failed if codes_array.map{|code| code.to_s.slice(6,7).to_i}.include?(11) || response.code == 401 raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("#{.join(', ')}", body, response.code) end #Zuora REST Create Amendment error #Authentication failed if body["faultcode"].present? && body["faultcode"] == "fns:INVALID_SESSION" raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("#{body['faultstring']}", body, response.code) end #Request exceeded limit if codes_array.map{|code| code.to_s.slice(6,7).to_i}.include?(70) raise ZuoraAPI::Exceptions::ZuoraAPIRequestLimit.new("#{.join(', ')}", body, response.code) end #Locking contention if codes_array.map{|code| code.to_s.slice(6,7).to_i}.include?(50) raise ZuoraAPI::Exceptions::ZuoraAPILockCompetition.new("#{.join(', ')}", body, response.code) end #All Errors catch if codes_array.size > 0 raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{.join(', ')}", body, response.code) end #Zuora REST Query Errors if body["faultcode"].present? case body["faultcode"] when "fns:MALFORMED_QUERY" raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{body["faultcode"]}::#{body["faultstring"]}", body, response.code) when "fns:REQUEST_EXCEEDED_LIMIT" raise ZuoraAPI::Exceptions::ZuoraAPIRequestLimit.new("#{body["faultcode"]}::#{body["faultstring"]}", body, response.code) when "fns:LOCK_COMPETITION" raise ZuoraAPI::Exceptions::ZuoraAPILockCompetition.new("#{body["faultcode"]}::#{body["faultstring"]}", body, response.code) when "INVALID_SESSION" raise ZuoraAPI::Exceptions::ZuoraAPISessionError.new("#{body["faultcode"]}::#{body["faultstring"]}", body, response.code) else raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{body["faultcode"]}::#{body["faultstring"]}", body, response.code) end end if body["Errors"].present? || body["errors"].present? errors = [] (body["Errors"] || []).select { |obj| errors.push(obj["Message"]) }.compact (body["errors"] || []).select { |obj| errors.push(obj["Message"]) }.compact if errors.size > 0 raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{errors.join(", ")}", body, response.code, errors) end end end #Zuora REST Actions error (Create, Update, Delete) if body.class == Array all_errors = body.select {|obj| !obj['Success'] || !obj['success'] }.map {|obj| obj['Errors'] || obj['errors'] }.compact all_success = body.select {|obj| obj['Success'] || obj['success']}.compact if all_errors.size > 0 raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{all_errors.flatten.group_by {|error| error['Message']}.keys.uniq.join(' ')}", body, response.code, all_errors, all_success ) end end #All other errors if response.code != 200 raise ZuoraAPI::Exceptions::ZuoraAPIError.new("#{response.}", body, response.code) end end end |
#rest_call(method: :get, body: {}, headers: {}, url: rest_endpoint("catalog/products?pageSize=4"), debug: true, errors: [ZuoraAPI::Exceptions::ZuoraAPISessionError, ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition], z_session: true, timeout_retry: false, timeout: 120, **keyword_args) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 |
# File 'lib/zuora_api/login.rb', line 492 def rest_call(method: :get, body: {},headers: {}, url: rest_endpoint("catalog/products?pageSize=4"), debug: true, errors: [ZuoraAPI::Exceptions::ZuoraAPISessionError, ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition], z_session: true, timeout_retry: false, timeout: 120, **keyword_args) tries ||= 2 headers["entityId"] = self.entity_id if !self.entity_id.blank? raise "Method not supported, supported methods include: :get, :post, :put, :delete, :patch, :head, :options" if ![:get, :post, :put, :delete, :patch, :head, :options].include?(method) response = HTTParty::Request.new("Net::HTTP::#{method.to_s.capitalize}".constantize, url, body: body, headers: {'Content-Type' => "application/json; charset=utf-8"}.merge(z_session ? {"Authorization" => "ZSession #{self.get_session}"} : {}).merge(headers), timeout: timeout).perform Rails.logger.debug('Connect') {"Response Code: #{response.code}" } if debug begin output_json = JSON.parse(response.body) rescue JSON::ParserError => ex output_json = {} end Rails.logger.debug('Connect') {"Response JSON: #{output_json}"} if debug && output_json.present? raise_errors(type: :JSON, body: output_json, response: response) rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex if !(tries -= 1).zero? && z_session Rails.logger.debug {"Session Invalid"} self.new_session retry else if errors.include?(ex.class) raise ex else return [output_json, response] end end rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition => ex if errors.include?(ex.class) raise ex else return [output_json, response] end rescue Net::OpenTimeout, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex if !(tries -= 1).zero? && timeout_retry Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"} sleep(self.timeout_sleep) retry else raise ex end rescue => ex raise ex else return [output_json, response] end |
#rest_endpoint(url = "") ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/zuora_api/login.rb', line 68 def rest_endpoint(url="") if self.environment == 'Sandbox' return self.region == "US" ? "https://rest.apisandbox.zuora.com/v1/".concat(url) : "https://rest.sandbox.eu.zuora.com/v1/".concat(url) elsif self.environment == 'Production' return self.region == "US" ? "https://rest.zuora.com/v1/".concat(url) : "https://rest.eu.zuora.com/v1/".concat(url) elsif self.environment == 'Services' return self.url.split('/')[0..2].join('/').concat('/apps/v1/').concat(url) elsif self.environment == 'Performance' return self.url.split('/')[0..2].join('/').concat('/apps/v1/').concat(url) else self.environment == 'Unknown' return url end end |
#soap_call(ns1: 'ns1', ns2: 'ns2', batch_size: nil, single_transaction: false, debug: true, errors: [ZuoraAPI::Exceptions::ZuoraAPISessionError, ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition], z_session: true, timeout_retry: false, timeout: 120, **keyword_args) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/zuora_api/login.rb', line 209 def soap_call(ns1: 'ns1', ns2: 'ns2', batch_size: nil, single_transaction: false, debug: true, errors: [ZuoraAPI::Exceptions::ZuoraAPISessionError, ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition], z_session: true, timeout_retry: false, timeout: 120,**keyword_args) tries ||= 2 xml = Nokogiri::XML::Builder.new do |xml| xml['SOAP-ENV'].Envelope('xmlns:SOAP-ENV' => "http://schemas.xmlsoap.org/soap/envelope/", "xmlns:#{ns2}" => "http://object.api.zuora.com/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:api' => "http://api.zuora.com/", "xmlns:#{ns1}" => "http://api.zuora.com/") do xml['SOAP-ENV'].Header do xml["#{ns1}"].SessionHeader do xml["#{ns1}"].session self.get_session end if single_transaction xml["#{ns1}"].CallOptions do xml.useSingleTransaction single_transaction end end if batch_size xml["#{ns1}"].QueryOptions do xml.batchSize batch_size end end end xml['SOAP-ENV'].Body do yield xml, keyword_args end end end input_xml = Nokogiri::XML(xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip) input_xml.xpath('//ns1:session', 'ns1' =>'http://api.zuora.com/').children.remove Rails.logger.debug('Connect') {"Request SOAP XML: #{input_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip}"} if debug response = HTTParty.post(self.url,:body => xml.doc.to_xml, :headers => {'Content-Type' => "text/xml; charset=utf-8"}, :timeout => timeout) output_xml = Nokogiri::XML(response.body) Rails.logger.debug('Connect') {"Response SOAP XML: #{output_xml.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML | Nokogiri::XML::Node::SaveOptions::NO_DECLARATION).strip}"} if debug raise_errors(type: :SOAP, body: output_xml, response: response) rescue ZuoraAPI::Exceptions::ZuoraAPISessionError => ex if !(tries -= 1).zero? && z_session Rails.logger.debug {"Session Invalid"} self.new_session retry else if errors.include?(ex.class) raise ex else return [output_xml, input_xml] end end rescue ZuoraAPI::Exceptions::ZuoraAPIError, ZuoraAPI::Exceptions::ZuoraAPIRequestLimit, ZuoraAPI::Exceptions::ZuoraAPILockCompetition => ex if errors.include?(ex.class) raise ex else return [output_xml, input_xml] end rescue Net::OpenTimeout, Errno::ECONNRESET, Errno::ECONNREFUSED, SocketError => ex if !(tries -= 1).zero? && timeout_retry Rails.logger.info {"#{ex.class} Timed out will retry after 5 seconds"} sleep(self.timeout_sleep) retry else raise ex end rescue => ex raise ex else return [output_xml, input_xml] end |
#update_create_tenant ⇒ Object
540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/zuora_api/login.rb', line 540 def update_create_tenant Rails.logger.debug("Update and/or Create Tenant") output_xml, input_xml = soap_call() do |xml| xml['api'].getUserInfo end user_info = output_xml.xpath('//ns1:getUserInfoResponse', 'ns1' =>'http://api.zuora.com/') output_hash = Hash[user_info.children.map {|x| [x.name.to_sym, x.text] }] self.user_info = output_hash self.user_info['entities'] = self.rest_call(:url => self.rest_endpoint("user-access/user-profile/#{self.user_info['UserId']}/accessible-entities"))['entities'] self.tenant_name = output_hash[:TenantName] self.tenant_id = output_hash[:TenantId] return self end |
#update_environment ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/zuora_api/login.rb', line 44 def update_environment if !self.url.blank? env_path = self.url.split('https://').last.split('.zuora.com').first self.region = self.url.include?("eu.") ? "EU" : "US" if env_path == 'apisandbox' || self.url.include?('sandbox') self.environment = 'Sandbox' elsif env_path == 'www' || env_path == 'api' || self.url.include?('tls10.zuora.com') || self.url.include?('origin-www.zuora.com') || self.url.include?('zforsf.zuora.com') || self.url.include?('https://zuora.com') || self.url.include?('eu.zuora.com') self.environment = 'Production' elsif env_path.include?('service') || env_path.include?('ep-edge') self.environment = 'Services' elsif env_path.include?('pt') self.environment = 'Performance' elsif env_path.include?('app-0') self.environment = 'Staging' else self.environment = 'Unknown' end end end |