ZCRMSDK 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ZCRMSDK.rb +14 -0
- data/lib/ZCRMSDK/handler.rb +2944 -0
- data/lib/ZCRMSDK/oauth_client.rb +250 -0
- data/lib/ZCRMSDK/oauth_utility.rb +160 -0
- data/lib/ZCRMSDK/operations.rb +1043 -0
- data/lib/ZCRMSDK/org.rb +153 -0
- data/lib/ZCRMSDK/persistence.rb +127 -0
- data/lib/ZCRMSDK/request.rb +83 -0
- data/lib/ZCRMSDK/response.rb +216 -0
- data/lib/ZCRMSDK/restclient.rb +65 -0
- data/lib/ZCRMSDK/test.rb +5400 -0
- data/lib/ZCRMSDK/utility.rb +307 -0
- data/lib/ZCRMSDK/version.rb +5 -0
- metadata +156 -0
data/lib/ZCRMSDK/org.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'handler'
|
4
|
+
require_relative 'utility'
|
5
|
+
require_relative 'operations'
|
6
|
+
module ZCRMSDK
|
7
|
+
module Org
|
8
|
+
# THIS CLASS IS USED TO STORE AND EXECUTE ORGANIZATION RELATED FUNCTIONALITIES
|
9
|
+
class ZCRMOrganization
|
10
|
+
attr_accessor :users_license_purchased, :photo_id, :privacy_settings, :zia_portal_id, :currency, :company_name, :org_id, :alias_aka, :primary_zuid, :zgid, :primary_email, :website, :mobile, :phone, :employee_count, :description, :time_zone, :iso_code, :currency_locale, :currency_symbol, :street, :state, :city, :country, :zip_code, :country_code, :fax, :mc_status, :is_gapps_enabled, :paid_expiry, :trial_type, :trial_expiry, :is_paid_account, :paid_type
|
11
|
+
def initialize(org_name = nil, org_id = nil)
|
12
|
+
@currency = nil
|
13
|
+
@zia_portal_id = nil
|
14
|
+
@privacy_settings = nil
|
15
|
+
@photo_id = nil
|
16
|
+
@company_name = org_name
|
17
|
+
@org_id = org_id
|
18
|
+
@alias_aka = nil
|
19
|
+
@primary_zuid = nil
|
20
|
+
@zgid = nil
|
21
|
+
@primary_email = nil
|
22
|
+
@website = nil
|
23
|
+
@mobile = nil
|
24
|
+
@phone = nil
|
25
|
+
@employee_count = nil
|
26
|
+
@description = nil
|
27
|
+
@time_zone = nil
|
28
|
+
@iso_code = nil
|
29
|
+
@currency_locale = nil
|
30
|
+
@currency_symbol = nil
|
31
|
+
@street = nil
|
32
|
+
@state = nil
|
33
|
+
@city = nil
|
34
|
+
@country = nil
|
35
|
+
@zip_code = nil
|
36
|
+
@country_code = nil
|
37
|
+
@fax = nil
|
38
|
+
@mc_status = nil
|
39
|
+
@is_gapps_enabled = nil
|
40
|
+
@paid_expiry = nil
|
41
|
+
@trial_type = nil
|
42
|
+
@trial_expiry = nil
|
43
|
+
@is_paid_account = nil
|
44
|
+
@paid_type = nil
|
45
|
+
@users_license_purchased = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.get_instance(org_name = nil, org_id = nil)
|
49
|
+
ZCRMOrganization.new(org_name, org_id)
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_user(user_id)
|
53
|
+
Handler::OrganizationAPIHandler.get_instance.get_user(user_id)
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_current_user
|
57
|
+
Handler::OrganizationAPIHandler.get_instance.get_current_user
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_all_users
|
61
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_users
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_all_active_users
|
65
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_active_users
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_all_deactive_users
|
69
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_deactive_users
|
70
|
+
end
|
71
|
+
|
72
|
+
def get_all_confirmed_users
|
73
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_confirmed_users
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_all_not_confirmed_users
|
77
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_not_confirmed_users
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_all_deleted_users
|
81
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_deleted_users
|
82
|
+
end
|
83
|
+
|
84
|
+
def get_all_active_confirmed_users
|
85
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_active_confirmed_users
|
86
|
+
end
|
87
|
+
|
88
|
+
def get_all_admin_users
|
89
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_admin_users
|
90
|
+
end
|
91
|
+
|
92
|
+
def get_all_active_confirmed_admin_users
|
93
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_active_confirmed_admin_users
|
94
|
+
end
|
95
|
+
|
96
|
+
def get_all_profiles
|
97
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_profiles
|
98
|
+
end
|
99
|
+
|
100
|
+
def get_profile(profile_id)
|
101
|
+
Handler::OrganizationAPIHandler.get_instance.get_profile(profile_id)
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_all_roles
|
105
|
+
Handler::OrganizationAPIHandler.get_instance.get_all_roles
|
106
|
+
end
|
107
|
+
|
108
|
+
def get_role(role_id)
|
109
|
+
Handler::OrganizationAPIHandler.get_instance.get_role(role_id)
|
110
|
+
end
|
111
|
+
|
112
|
+
def create_user(user_instance)
|
113
|
+
Handler::OrganizationAPIHandler.get_instance.create_user(user_instance)
|
114
|
+
end
|
115
|
+
|
116
|
+
def update_user(user_instance)
|
117
|
+
Handler::OrganizationAPIHandler.get_instance.update_user(user_instance)
|
118
|
+
end
|
119
|
+
|
120
|
+
def delete_user(user_id)
|
121
|
+
Handler::OrganizationAPIHandler.get_instance.delete_user(user_id)
|
122
|
+
end
|
123
|
+
|
124
|
+
def get_organization_taxes
|
125
|
+
Handler::OrganizationAPIHandler.get_instance.get_organization_taxes
|
126
|
+
end
|
127
|
+
|
128
|
+
def get_organization_tax(org_tax_id)
|
129
|
+
Handler::OrganizationAPIHandler.get_instance.get_organization_tax(org_tax_id)
|
130
|
+
end
|
131
|
+
|
132
|
+
def search_users_by_criteria(criteria, type = nil)
|
133
|
+
Handler::OrganizationAPIHandler.get_instance.search_users_by_criteria(criteria, type)
|
134
|
+
end
|
135
|
+
|
136
|
+
def create_organization_taxes(orgtax_instances)
|
137
|
+
Handler::OrganizationAPIHandler.get_instance.create_organization_taxes(orgtax_instances)
|
138
|
+
end
|
139
|
+
|
140
|
+
def update_organization_taxes(orgtax_instances)
|
141
|
+
Handler::OrganizationAPIHandler.get_instance.update_organization_taxes(orgtax_instances)
|
142
|
+
end
|
143
|
+
|
144
|
+
def delete_organization_taxes(orgtax_ids)
|
145
|
+
Handler::OrganizationAPIHandler.get_instance.delete_organization_taxes(orgtax_ids)
|
146
|
+
end
|
147
|
+
|
148
|
+
def delete_organization_tax(orgtax_id)
|
149
|
+
Handler::OrganizationAPIHandler.get_instance.delete_organization_tax(orgtax_id)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'oauth_utility'
|
4
|
+
require_relative 'oauth_client'
|
5
|
+
module ZCRMSDK
|
6
|
+
module Persistence
|
7
|
+
# THIS CLASS IS USED TO STORE, RETRIEVE, DELETE TOKENS IN DEFAULT DB
|
8
|
+
class ZohoOAuthPersistenceHandler
|
9
|
+
def initialize; end
|
10
|
+
|
11
|
+
def self.get_instance
|
12
|
+
ZohoOAuthPersistenceHandler.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def save_oauth_tokens(oauth_tokens)
|
16
|
+
delete_oauth_tokens(oauth_tokens.user_identifier)
|
17
|
+
con = Mysql2::Client.new(host: $OAUTH_CONFIG_PROPERTIES['db_address'], username: $OAUTH_CONFIG_PROPERTIES['db_username'], password: $OAUTH_CONFIG_PROPERTIES['db_password'], database: 'zohooauth', port: $OAUTH_CONFIG_PROPERTIES['db_port'])
|
18
|
+
con.query("insert into oauthtokens(useridentifier,accesstoken,refreshtoken,expirytime) values('#{oauth_tokens.user_identifier}','#{oauth_tokens.access_token}','#{oauth_tokens.refresh_token}',#{oauth_tokens.expiry_time})")
|
19
|
+
con.close
|
20
|
+
rescue Mysql2::Error => e
|
21
|
+
Utility::SDKLogger.add_log(e.error, OAuthUtility::ZohoOAuthConstants::ERROR, e)
|
22
|
+
ensure
|
23
|
+
con.close
|
24
|
+
end
|
25
|
+
|
26
|
+
def get_oauth_tokens(user_identifier)
|
27
|
+
con = Mysql2::Client.new(host: $OAUTH_CONFIG_PROPERTIES['db_address'], username: $OAUTH_CONFIG_PROPERTIES['db_username'], password: $OAUTH_CONFIG_PROPERTIES['db_password'], database: 'zohooauth', port: $OAUTH_CONFIG_PROPERTIES['db_port'])
|
28
|
+
query = "select * from oauthtokens where useridentifier='#{user_identifier}'"
|
29
|
+
rs = con.query(query)
|
30
|
+
oauth_tokens = nil
|
31
|
+
rs.each do |row|
|
32
|
+
oauth_tokens = OAuthClient::ZohoOAuthTokens.get_instance(row['refreshtoken'], row['accesstoken'], row['expirytime'], user_identifier)
|
33
|
+
con.close
|
34
|
+
return oauth_tokens
|
35
|
+
end
|
36
|
+
raise OAuthUtility::ZohoOAuthException.get_instance('get_oauth_tokens', 'no such email id - ' + user_identifier + ' present in the DB', 'Exception occured while fetching accesstoken from Grant Token', OAuthUtility::ZohoOAuthConstants::ERROR)
|
37
|
+
rescue Mysql2::Error => e
|
38
|
+
Utility::SDKLogger.add_log(e.error, OAuthUtility::ZohoOAuthConstants::ERROR, e)
|
39
|
+
ensure
|
40
|
+
con.close
|
41
|
+
end
|
42
|
+
|
43
|
+
def delete_oauth_tokens(user_identifier)
|
44
|
+
con = Mysql2::Client.new(host: $OAUTH_CONFIG_PROPERTIES['db_address'], username: $OAUTH_CONFIG_PROPERTIES['db_username'], password: $OAUTH_CONFIG_PROPERTIES['db_password'], database: 'zohooauth', port: $OAUTH_CONFIG_PROPERTIES['db_port'])
|
45
|
+
delete_query = "delete from oauthtokens where useridentifier='#{user_identifier}'"
|
46
|
+
con.query(delete_query)
|
47
|
+
con.close
|
48
|
+
rescue Mysql2::Error => e
|
49
|
+
Utility::SDKLogger.add_log(e.error, OAuthUtility::ZohoOAuthConstants::ERROR, e)
|
50
|
+
ensure
|
51
|
+
con.close
|
52
|
+
end
|
53
|
+
end
|
54
|
+
# THIS CLASS IS USED TO STORE, RETRIEVE, DELETE TOKENS IN FILE
|
55
|
+
class ZohoOAuthFilePersistenceHandler
|
56
|
+
def initialize; end
|
57
|
+
|
58
|
+
def self.get_instance
|
59
|
+
ZohoOAuthFilePersistenceHandler.new
|
60
|
+
end
|
61
|
+
|
62
|
+
def save_oauth_tokens(oauth_tokens)
|
63
|
+
unless File.exist?($OAUTH_CONFIG_PROPERTIES[OAuthUtility::ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH].dup + '/zcrm_oauthtokens.txt')
|
64
|
+
file_obj = File.new($OAUTH_CONFIG_PROPERTIES[OAuthUtility::ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH].dup + '/zcrm_oauthtokens.txt', 'w')
|
65
|
+
file_obj.close
|
66
|
+
end
|
67
|
+
delete_oauth_tokens(oauth_tokens.user_identifier)
|
68
|
+
arr = []
|
69
|
+
path = $OAUTH_CONFIG_PROPERTIES[OAuthUtility::ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH].dup + '/zcrm_oauthtokens.txt'
|
70
|
+
file_obj = File.open(path, 'r')
|
71
|
+
serialized = file_obj.read
|
72
|
+
file_obj.close
|
73
|
+
arr = Marshal.load(serialized) unless serialized.nil? || serialized.empty?
|
74
|
+
arr.push(oauth_tokens)
|
75
|
+
file_obj = File.open(path, 'w+')
|
76
|
+
file_obj.write(Marshal.dump(arr))
|
77
|
+
file_obj.close
|
78
|
+
end
|
79
|
+
|
80
|
+
def get_oauth_tokens(user_identifier)
|
81
|
+
if !File.exist?($OAUTH_CONFIG_PROPERTIES[OAuthUtility::ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH].dup + '/zcrm_oauthtokens.txt')
|
82
|
+
raise OAuthUtility::ZohoOAuthException.get_instance('get_oauth_tokens', 'file does not exist!generate the access token!', 'Error occured while getting access token', OAuthUtility::ZohoOAuthConstants::ERROR)
|
83
|
+
end
|
84
|
+
|
85
|
+
path = $OAUTH_CONFIG_PROPERTIES[OAuthUtility::ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH].dup + '/zcrm_oauthtokens.txt'
|
86
|
+
file_obj = File.open(path, 'r')
|
87
|
+
serialized = file_obj.read
|
88
|
+
file_obj.close
|
89
|
+
if serialized.empty? || serialized.nil?
|
90
|
+
raise OAuthUtility::ZohoOAuthException.get_instance('get_oauth_tokens', 'no tokens found!generate the access token!', 'Error occured while getting access token', OAuthUtility::ZohoOAuthConstants::ERROR)
|
91
|
+
end
|
92
|
+
|
93
|
+
deserialized = Marshal.load(serialized)
|
94
|
+
deserialized.each do |token|
|
95
|
+
return token if token.user_identifier == user_identifier
|
96
|
+
end
|
97
|
+
raise OAuthUtility::ZohoOAuthException.get_instance('get_oauth_tokens', 'no such' + user_identifier.to_s + 'present in the File', 'Exception occured while fetching accesstoken from Grant Token', OAuthUtility::ZohoOAuthConstants::ERROR)
|
98
|
+
end
|
99
|
+
|
100
|
+
def delete_oauth_tokens(user_identifier)
|
101
|
+
path = $OAUTH_CONFIG_PROPERTIES[OAuthUtility::ZohoOAuthConstants::TOKEN_PERSISTENCE_PATH].dup + '/zcrm_oauthtokens.txt'
|
102
|
+
file_obj = File.open(path, 'r')
|
103
|
+
serialized = file_obj.read
|
104
|
+
file_obj.close
|
105
|
+
|
106
|
+
return if serialized.empty? || serialized.nil?
|
107
|
+
|
108
|
+
found = false
|
109
|
+
i = 0
|
110
|
+
deserialized = Marshal.load(serialized)
|
111
|
+
deserialized.each do |token|
|
112
|
+
if token.user_identifier == user_identifier
|
113
|
+
found = true
|
114
|
+
break
|
115
|
+
end
|
116
|
+
i += 1
|
117
|
+
end
|
118
|
+
if found
|
119
|
+
deserialized.delete_at(i)
|
120
|
+
file_obj = File.open(path, 'w+')
|
121
|
+
file_obj.write(Marshal.dump(deserialized))
|
122
|
+
file_obj.close
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'utility'
|
4
|
+
require_relative 'response'
|
5
|
+
module ZCRMSDK
|
6
|
+
module Request
|
7
|
+
# THIS CLASS IS USED TO AUTHENTICATE AND CONSTRUCT API REQUEST
|
8
|
+
class APIRequest
|
9
|
+
def initialize(api_handler_ins)
|
10
|
+
construct_api_url
|
11
|
+
@url += api_handler_ins.request_url_path
|
12
|
+
@url = 'https://' + @url unless @url.start_with?('http')
|
13
|
+
@request_body = api_handler_ins.request_body
|
14
|
+
@request_headers = api_handler_ins.request_headers
|
15
|
+
@request_params = api_handler_ins.request_params
|
16
|
+
@request_method = api_handler_ins.request_method
|
17
|
+
@request_api_key = api_handler_ins.request_api_key
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.get_instance(api_handler_ins)
|
21
|
+
APIRequest.new(api_handler_ins)
|
22
|
+
end
|
23
|
+
|
24
|
+
def construct_api_url
|
25
|
+
hit_sandbox = ZCRMSDK::Utility::ZCRMConfigUtil.get_config_value(ZCRMSDK::Utility::APIConstants::SANDBOX)
|
26
|
+
url = ZCRMSDK::Utility::ZCRMConfigUtil.get_api_base_url
|
27
|
+
if hit_sandbox == 'true'
|
28
|
+
url = url.sub('www.', 'sandbox.')
|
29
|
+
end
|
30
|
+
@url = url + '/crm/' + ZCRMSDK::Utility::ZCRMConfigUtil.get_api_version + '/'
|
31
|
+
end
|
32
|
+
|
33
|
+
def authenticate_request
|
34
|
+
access_token = ZCRMSDK::Utility::ZCRMConfigUtil.get_instance.get_access_token
|
35
|
+
if @request_headers.nil?
|
36
|
+
@request_headers = { ZCRMSDK::Utility::APIConstants::AUTHORIZATION => ZCRMSDK::Utility::APIConstants::OAUTH_HEADER_PREFIX.dup + access_token }
|
37
|
+
else
|
38
|
+
@request_headers[ZCRMSDK::Utility::APIConstants::AUTHORIZATION] = ZCRMSDK::Utility::APIConstants::OAUTH_HEADER_PREFIX.dup + access_token
|
39
|
+
end
|
40
|
+
@request_headers['User-Agent'] = 'ZohoCRM Ruby SDK'
|
41
|
+
end
|
42
|
+
|
43
|
+
def get_api_response
|
44
|
+
authenticate_request
|
45
|
+
connector = ZCRMSDK::Utility::ZohoHTTPConnector.get_instance(@url, @request_params, @request_headers, @request_body.to_json, @request_method, @request_api_key, false)
|
46
|
+
response = connector.trigger_request
|
47
|
+
Response::APIResponse.get_instance(response, response.code.to_i, @url, @request_api_key)
|
48
|
+
end
|
49
|
+
|
50
|
+
def get_bulk_api_response
|
51
|
+
authenticate_request
|
52
|
+
connector = ZCRMSDK::Utility::ZohoHTTPConnector.get_instance(@url, @request_params, @request_headers, @request_body.to_json, @request_method, @request_api_key, true)
|
53
|
+
response = connector.trigger_request
|
54
|
+
Response::BulkAPIResponse.get_instance(response, response.code.to_i, @url, @request_api_key)
|
55
|
+
end
|
56
|
+
|
57
|
+
def upload_file(file_path)
|
58
|
+
authenticate_request
|
59
|
+
form_data = [['file', File.open(file_path)]]
|
60
|
+
connector = ZCRMSDK::Utility::ZohoHTTPConnector.get_instance(@url, @request_params, @request_headers, @request_body, @request_method, @request_api_key, false, form_data)
|
61
|
+
response = connector.trigger_request
|
62
|
+
Response::APIResponse.get_instance(response, response.code.to_i, @url, @request_api_key)
|
63
|
+
end
|
64
|
+
|
65
|
+
def upload_link_as_attachment(link_url)
|
66
|
+
authenticate_request
|
67
|
+
form_data = [['attachmentUrl', link_url]]
|
68
|
+
connector = ZCRMSDK::Utility::ZohoHTTPConnector.get_instance(@url, @request_params, @request_headers, @request_body, @request_method, @request_api_key, false, form_data)
|
69
|
+
response = connector.trigger_request
|
70
|
+
Response::APIResponse.get_instance(response, response.code.to_i, @url, @request_api_key)
|
71
|
+
end
|
72
|
+
|
73
|
+
def download_file
|
74
|
+
authenticate_request
|
75
|
+
connector = ZCRMSDK::Utility::ZohoHTTPConnector.get_instance(@url, @request_params, @request_headers, @request_body, @request_method, @request_api_key, false)
|
76
|
+
response = connector.trigger_request
|
77
|
+
file_response = Response::FileAPIResponse.get_instance(response, response.code.to_i, @url)
|
78
|
+
file_response.set_file_content
|
79
|
+
file_response
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,216 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'utility'
|
4
|
+
require 'json'
|
5
|
+
module ZCRMSDK
|
6
|
+
module Response
|
7
|
+
# THIS CLASS IS USED TO STORE DETAILS ABOUT THE API RESPONSE , PROCESS JSON AND HANDLE FAULTY RESPONSES
|
8
|
+
class CommonAPIResponse
|
9
|
+
attr_accessor :response_json, :response_headers, :response, :status_code, :api_key, :url, :data, :status, :code, :message, :details
|
10
|
+
def initialize(response, status_code, url, api_key = nil)
|
11
|
+
@response_json = nil
|
12
|
+
@response_headers = nil
|
13
|
+
@response = response
|
14
|
+
@status_code = status_code
|
15
|
+
@api_key = api_key
|
16
|
+
@url = url
|
17
|
+
@data = nil
|
18
|
+
@status = nil
|
19
|
+
@code = nil
|
20
|
+
@message = nil
|
21
|
+
@details = nil
|
22
|
+
set_response_json
|
23
|
+
process_response
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.get_instance(response, status_code, url, api_key = nil)
|
27
|
+
CommonAPIResponse.new(response, status_code, url, api_key)
|
28
|
+
end
|
29
|
+
|
30
|
+
def set_response_json
|
31
|
+
if (@status_code != Utility::APIConstants::RESPONSECODE_NO_CONTENT) && (@status_code != Utility::APIConstants::RESPONSECODE_NOT_MODIFIED)
|
32
|
+
@response_json = JSON.parse(@response.body)
|
33
|
+
else
|
34
|
+
@response_json = {}
|
35
|
+
@response_headers = {}
|
36
|
+
return
|
37
|
+
end
|
38
|
+
@response_headers = @response.to_hash
|
39
|
+
end
|
40
|
+
|
41
|
+
def process_response
|
42
|
+
if Utility::APIConstants::FAULTY_RESPONSE_CODES.include?(@status_code)
|
43
|
+
handle_faulty_responses
|
44
|
+
elsif (@status_code == Utility::APIConstants::RESPONSECODE_ACCEPTED) || (@status_code == Utility::APIConstants::RESPONSECODE_OK) || (@status_code == Utility::APIConstants::RESPONSECODE_CREATED)
|
45
|
+
process_response_data
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def handle_faulty_responses
|
50
|
+
nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def process_response_data
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
# THIS CLASS IS USED TO HANDLE API RESPONSE
|
58
|
+
class APIResponse < CommonAPIResponse
|
59
|
+
def initialize(response, status_code, url, api_key)
|
60
|
+
super
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.get_instance(response, status_code, url, api_key)
|
64
|
+
APIResponse.new(response, status_code, url, api_key)
|
65
|
+
end
|
66
|
+
|
67
|
+
def handle_faulty_responses
|
68
|
+
if @status_code == Utility::APIConstants::RESPONSECODE_NO_CONTENT
|
69
|
+
error_msg = Utility::APIConstants::NO_CONTENT
|
70
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, error_msg, Utility::APIConstants::NO_CONTENT, nil, error_msg)
|
71
|
+
else
|
72
|
+
response_json = @response_json
|
73
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, response_json[Utility::APIConstants::MESSAGE], response_json[Utility::APIConstants::CODE], response_json[Utility::APIConstants::DETAILS], response_json[Utility::APIConstants::MESSAGE])
|
74
|
+
end
|
75
|
+
raise exception
|
76
|
+
end
|
77
|
+
|
78
|
+
def process_response_data
|
79
|
+
resp_json = @response_json
|
80
|
+
if resp_json.include?(api_key)
|
81
|
+
resp_json = @response_json[api_key]
|
82
|
+
resp_json = resp_json[0] if resp_json.instance_of?(Array)
|
83
|
+
end
|
84
|
+
if resp_json.include?(Utility::APIConstants::STATUS) && (resp_json[Utility::APIConstants::STATUS] == Utility::APIConstants::STATUS_ERROR)
|
85
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, resp_json[Utility::APIConstants::MESSAGE], resp_json[Utility::APIConstants::CODE], resp_json[Utility::APIConstants::DETAILS], resp_json[Utility::APIConstants::STATUS])
|
86
|
+
raise exception
|
87
|
+
elsif resp_json.include?(Utility::APIConstants::STATUS) && (resp_json[Utility::APIConstants::STATUS] == Utility::APIConstants::STATUS_SUCCESS)
|
88
|
+
@status = resp_json[Utility::APIConstants::STATUS]
|
89
|
+
@code = resp_json[Utility::APIConstants::CODE]
|
90
|
+
@message = resp_json[Utility::APIConstants::MESSAGE]
|
91
|
+
@details = resp_json[Utility::APIConstants::DETAILS]
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
# THIS CLASS IS USED TO BULK API RESPONSE
|
96
|
+
class BulkAPIResponse < CommonAPIResponse
|
97
|
+
attr_accessor :bulk_entity_response, :info
|
98
|
+
def initialize(response, status_code, url, api_key)
|
99
|
+
@bulk_entity_response = []
|
100
|
+
super
|
101
|
+
if @response_json.include?(Utility::APIConstants::INFO)
|
102
|
+
@info = ResponseInfo.get_instance(@response_json[Utility::APIConstants::INFO])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.get_instance(response, status_code, url, api_key)
|
107
|
+
BulkAPIResponse.new(response, status_code, url, api_key)
|
108
|
+
end
|
109
|
+
|
110
|
+
def handle_faulty_responses
|
111
|
+
if @status_code == Utility::APIConstants::RESPONSECODE_NO_CONTENT
|
112
|
+
error_msg = Utility::APIConstants::NO_CONTENT
|
113
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, error_msg, Utility::APIConstants::NO_CONTENT, nil, error_msg)
|
114
|
+
else
|
115
|
+
response_json = @response_json
|
116
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, response_json[Utility::APIConstants::MESSAGE], response_json[Utility::APIConstants::CODE], response_json[Utility::APIConstants::DETAILS], response_json[Utility::APIConstants::MESSAGE])
|
117
|
+
end
|
118
|
+
raise exception
|
119
|
+
end
|
120
|
+
|
121
|
+
def process_response_data
|
122
|
+
responses_json = @response_json
|
123
|
+
if responses_json.include?(@api_key)
|
124
|
+
records_data = @response_json[api_key]
|
125
|
+
records_data.each do |record_data|
|
126
|
+
if !record_data.nil? && record_data.key?(Utility::APIConstants::STATUS)
|
127
|
+
@bulk_entity_response.push(EntityResponse.get_instance(record_data))
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
# THIS CLASS IS USED TO HANDLE FILE API RESPONSE
|
134
|
+
class FileAPIResponse
|
135
|
+
attr_accessor :filename, :response_json, :response_headers, :response, :status_code, :url, :data, :status, :code, :message, :details
|
136
|
+
def initialize(response, status_code, url)
|
137
|
+
@response_json = nil
|
138
|
+
@response_headers = nil
|
139
|
+
@response = response
|
140
|
+
@status_code = status_code
|
141
|
+
@url = url
|
142
|
+
@data = nil
|
143
|
+
@status = nil
|
144
|
+
@code = nil
|
145
|
+
@message = nil
|
146
|
+
@details = nil
|
147
|
+
@filename = nil
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.get_instance(response, status_code, url)
|
151
|
+
FileAPIResponse.new(response, status_code, url)
|
152
|
+
end
|
153
|
+
|
154
|
+
def set_file_content
|
155
|
+
if @status_code == Utility::APIConstants::RESPONSECODE_NO_CONTENT
|
156
|
+
error_msg = Utility::APIConstants::NO_CONTENT
|
157
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, error_msg, Utility::APIConstants::NO_CONTENT, nil, error_msg)
|
158
|
+
raise exception
|
159
|
+
end
|
160
|
+
if Utility::APIConstants::FAULTY_RESPONSE_CODES.include?(@status_code)
|
161
|
+
content = JSON.parse(@response.body)
|
162
|
+
exception = Utility::ZCRMException.get_instance(@url, @status_code, content[Utility::APIConstants::MESSAGE], content[Utility::APIConstants::CODE], content[Utility::APIConstants::DETAILS], content[Utility::APIConstants::MESSAGE])
|
163
|
+
raise exception
|
164
|
+
elsif @status_code == Utility::APIConstants::RESPONSECODE_OK
|
165
|
+
@response_headers = @response.to_hash
|
166
|
+
@status = Utility::APIConstants::STATUS_SUCCESS
|
167
|
+
@filename = @response_headers['content-disposition'][0]
|
168
|
+
@filename = @filename[@filename.rindex("'") + 1..@filename.length]
|
169
|
+
@response = @response.body
|
170
|
+
end
|
171
|
+
@response_headers = @response.to_hash if @response_headers.nil?
|
172
|
+
end
|
173
|
+
end
|
174
|
+
# THIS CLASS IS USED TO HANDLE ENTITY API RESPONSE
|
175
|
+
class EntityResponse
|
176
|
+
attr_accessor :response_json, :code, :message, :status, :details, :data, :upsert_action, :upsert_duplicate_field
|
177
|
+
def initialize(entity_response)
|
178
|
+
@response_json = entity_response
|
179
|
+
@code = entity_response[Utility::APIConstants::CODE]
|
180
|
+
@message = entity_response[Utility::APIConstants::MESSAGE]
|
181
|
+
@status = entity_response[Utility::APIConstants::STATUS]
|
182
|
+
@details = nil
|
183
|
+
@data = nil
|
184
|
+
@upsert_action = nil
|
185
|
+
@upsert_duplicate_field = nil
|
186
|
+
if entity_response.key?(Utility::APIConstants::DETAILS)
|
187
|
+
@details = entity_response[Utility::APIConstants::DETAILS]
|
188
|
+
end
|
189
|
+
if entity_response.key?(Utility::APIConstants::ACTION)
|
190
|
+
@upsert_action = entity_response[Utility::APIConstants::ACTION]
|
191
|
+
end
|
192
|
+
if entity_response.key?(Utility::APIConstants::DUPLICATE_FIELD)
|
193
|
+
@upsert_duplicate_field = entity_response[Utility::APIConstants::DUPLICATE_FIELD]
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def self.get_instance(entity_response)
|
198
|
+
EntityResponse.new(entity_response)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
# THIS CLASS IS USED TO STORE RESPONSE INFO
|
202
|
+
class ResponseInfo
|
203
|
+
attr_reader :is_more_records, :page, :per_page, :count
|
204
|
+
def initialize(response_info_json)
|
205
|
+
@is_more_records = (response_info_json['more_records']) == true
|
206
|
+
@page = (response_info_json['page']).to_i
|
207
|
+
@per_page = (response_info_json['per_page']).to_i
|
208
|
+
@count = (response_info_json['count']).to_i
|
209
|
+
end
|
210
|
+
|
211
|
+
def self.get_instance(response_info_json)
|
212
|
+
ResponseInfo.new(response_info_json)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|