1/**
2 * WHMCS HTTP module
3 *
4 * @copyright Copyright (c) WHMCS Limited 2005-2018
5 * @license http://www.whmcs.com/license/ WHMCS Eula
6 */
7(function(module) {
8 if (!WHMCS.hasModule('http')) {
9 WHMCS.loadModule('http', module);
10 }
11})({
12jqClient: function () {
13 _getSettings = function (url, data, success, dataType)
14 {
15 if (typeof url === 'object') {
16 /*
17 Settings may be the only argument
18 */
19 return url;
20 }
21
22 if (typeof data === 'function') {
23 /*
24 If 'data' is omitted, 'success' will come in its place
25 */
26 success = data;
27 data = null;
28 }
29
30 return {
31 url: url,
32 data: data,
33 success: success,
34 dataType: dataType
35 };
36 };
37
38 /**
39 * @param url
40 * @param data
41 * @param success
42 * @param dataType
43 * @returns {*}
44 */
45 this.get = function (url, data, success, dataType)
46 {
47 return WHMCS.http.client.request(
48 jQuery.extend(
49 _getSettings(url, data, success, dataType),
50 {
51 type: 'GET'
52 }
53 )
54 );
55 };
56
57 /**
58 * @param url
59 * @param data
60 * @param success
61 * @param dataType
62 * @returns {*}
63 */
64 this.post = function (url, data, success, dataType)
65 {
66 return WHMCS.http.client.request(
67 jQuery.extend(
68 _getSettings(url, data, success, dataType),
69 {
70 type: 'POST'
71 }
72 )
73 );
74 };
75
76 /**
77 * @param options
78 * @returns {*}
79 */
80 this.jsonGet = function (options) {
81 options = options || {};
82 this.get(options.url, options.data, function(response) {
83 if (response.warning) {
84 console.log('[WHMCS] Warning: ' + response.warning);
85 if (typeof options.warning === 'function') {
86 options.warning(response.warning);
87 }
88 } else if (response.error) {
89 console.log('[WHMCS] Error: ' + response.error);
90 if (typeof options.error === 'function') {
91 options.error(response.error);
92 }
93 } else {
94 if (typeof options.success === 'function') {
95 options.success(response);
96 }
97 }
98 }, 'json').error(function(xhr, errorMsg){
99 console.log('[WHMCS] Error: ' + errorMsg);
100 if (typeof options.fail === 'function') {
101 options.fail(errorMsg);
102 }
103 }).always(function() {
104 if (typeof options.always === 'function') {
105 options.always();
106 }
107 });
108 };
109
110 /**
111 * @param options
112 * @returns {*}
113 */
114 this.jsonPost = function (options) {
115 options = options || {};
116 this.post(options.url, options.data, function(response) {
117 if (response.warning) {
118 console.log('[WHMCS] Warning: ' + response.warning);
119 if (typeof options.warning === 'function') {
120 options.warning(response.warning);
121 }
122 } else if (response.error) {
123 console.log('[WHMCS] Error: ' + response.error);
124 if (typeof options.error === 'function') {
125 options.error(response.error);
126 }
127 } else {
128 if (typeof options.success === 'function') {
129 options.success(response);
130 }
131 }
132 }, 'json').fail(function(xhr, errorMsg){
133 console.log('[WHMCS] Fail: ' + errorMsg);
134 if (typeof options.fail === 'function') {
135 options.fail(errorMsg, xhr);
136 }
137 }).always(function() {
138 if (typeof options.always === 'function') {
139 options.always();
140 }
141 });
142 };
143
144 return this;
145},
146
147client: function () {
148 var methods = ['get', 'post', 'put', 'delete'];
149 var client = this;
150
151 _beforeRequest = function (settings)
152 {
153 /*
154 Enforcing dataType was found to break many invocations expecting HTML back.
155 If/when those are refactored, this may be uncommented to enforce a safer
156 data transit.
157 */
158 /*if (typeof settings.dataType === 'undefined') {
159 settings.dataType = 'json';
160 }*/
161
162 if (typeof settings.type === 'undefined') {
163 // default request type is GET
164 settings.type = 'GET';
165 }
166
167 /*
168 Add other preprocessing here if required
169 */
170
171 return settings;
172 };
173
174 this.request = function (settings)
175 {
176 settings = _beforeRequest(settings || {});
177 return jQuery.ajax(settings);
178 };
179
180 /*
181 Create shortcut methods for methods[] array above
182 */
183 jQuery.each(methods, function(index, method) {
184 client[method] = (function(method, client) {
185 return function (settings)
186 {
187 settings = settings || {};
188
189 settings.type = method.toUpperCase();
190
191 return client.request(settings);
192 }
193 })(method, client);
194 });
195
196 return this;
197}
198
199});
200