Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GAPRINDASHVILI] API transformRequest support + http service #4005

Merged
merged 2 commits into from
Jun 1, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions app/assets/javascripts/miq_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,28 @@
(function() {
function API() {
}
function http() {
}

var urlOnly = function(method) {
var urlOnly = function(method, extra) {
return function(url, options) {
if (extra) {
options = Object.assign({}, extra, options || {});
}

return fetch(url, _.extend({
method: method,
}, process_options(options)))
.then(responseAndError(options));
};
};

var withData = function(method) {
var withData = function(method, extra) {
return function(url, data, options) {
if (extra) {
options = Object.assign({}, extra, options || {});
}

return fetch(url, _.extend({
method: method,
body: process_data(data),
Expand All @@ -49,6 +59,9 @@
API.post = withData('POST');
API.put = withData('PUT');

http.get = urlOnly('GET', { credentials: 'include', token: false, csrf: true });
http.post = withData('POST', { credentials: 'include', token: false, csrf: true });

API.login = function(login, password) {
API.logout();

Expand Down Expand Up @@ -132,6 +145,7 @@
};

window.vanillaJsAPI = API;
window.http = http;


function process_options(o) {
Expand All @@ -142,16 +156,24 @@
delete o.data;
delete o.body;
delete o.skipErrors;
delete o.transformResponse;

if (o.skipTokenRenewal) {
o.headers = o.headers || {};
o.headers['X-Auth-Skip-Token-Renewal'] = 'true';
}

if (localStorage.miq_token) {
if (localStorage.miq_token && (o.token !== false)) {
o.headers = o.headers || {};
o.headers['X-Auth-Token'] = localStorage.miq_token;
}
delete o.token;

if (o.csrf) {
o.headers = o.headers || {};
o.headers['X-CSRF-Token'] = $('meta[name=csrf-token]').attr('content');
}
delete o.csrf;

if (o.headers) {
o.headers = new Headers(o.headers);
Expand Down Expand Up @@ -203,6 +225,11 @@
return ret;
}

// apply a custom transformation
if (options.transformResponse) {
ret = ret.then(options.transformResponse);
}

// true means skip all of them - no error modal at all
if (options.skipErrors === true) {
return ret;
Expand Down Expand Up @@ -250,6 +277,9 @@
})(window);


// v2v
window.API = window.vanillaJsAPI;

angular.module('miq.api', [])
.factory('API', ['$q', function($q) {
var angularify = function(what) {
Expand Down