/* APIRequest object */ var APIRequest = { method: 'get', endpoint: '', operation: '', query: '', headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin' : 'clubadmin.ardor.org.ro', 'Accept': 'application/json' }, body: [] }; /* APIResponse object */ var APIResponse = { error: { statusCode: 200, message: '' }, result: [] }; // TODO: handle them in a smarter way !! var resourceErrorCodes = [400, 417, 304]; /** * FIXME: document this method ! * @param apiRequest * @returns {Promise} */ function makeSyncAPICall(apiRequest) { return new Promise(function(resolve, reject) { var callUrl = apiUri['baseurl'] + apiUri[apiRequest.endpoint]; if(apiRequest.query != '') { callUrl += apiRequest.query; } $.ajax({ url: callUrl, type: apiRequest.method, data: apiRequest.body, headers: apiRequest.headers, contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, success: function(data, status, xhr) { console.log('APICall request successful [('+ apiRequest.method +')'+ callUrl +']: ' + apiRequest.body); resolve(data); }, error: function (xhr, status, error) { // FIXME: remove these after debugging ! console.log('XHR Error Response: ', xhr); console.log('APICall request failed [('+ apiRequest.method +')'+ callUrl +']: ' + apiRequest.body, Error(error)); if (xhr.status == 403 || xhr.status == 401) { // Not authorized LogOut(false); } else { var interceptor = new APIResponseInterceptor(); var requestInfo = { serviceId: apiRequest.endpoint, operation: apiRequest.operation }; var result = interceptor.execute(xhr, requestInfo); reject(result); } } }) }) } /** * FIXME: document this method ! * @param params * @param auth * @returns {{method: string, endpoint: string, query: string, headers: {Content-Type: string, Access-Control-Allow-Origin: string, Accept: string}, body: Array}|*} * @private */ function _initAPIRequest(params) { var apiRequest = APIRequest; apiRequest.method = params.method; apiRequest.endpoint = params.endpoint; apiRequest.operation = params.operation; if(params.method == 'get' && params.query_params) { apiRequest.query = '?'; $.each(params.query_params, function(idx, obj) { apiRequest.query += obj.key + '=' + obj.val + '&'; }); apiRequest.query = apiRequest.query.slice(0,-1); apiRequest.body = ''; } else if(params.method == 'post') { apiRequest.body = JSON.stringify(params.body); apiRequest.query = ''; } return apiRequest; } /** * FIXME: document this method ! * @param method * @param endpoint * @param operation * @param dto * @param query_params * @returns {*} * @private */ function initAPIRequest(method, endpoint, operation, dto, query_params) { var requestParams = {}; requestParams.method = method; requestParams.endpoint = endpoint; requestParams.operation = operation; if (method == 'post') { dto = (dto) ? dto : {}; operation = (operation) ? operation : ''; requestParams.body = { operation: operation, params: dto }; } if (method == 'get') { query_params = (query_params) ? query_params : {}; requestParams.query_params = query_params; } return _initAPIRequest(requestParams); }