jellyfin-web/dashboard-ui/cordova/android/androidcredentials.js

149 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-05-28 16:37:43 -07:00
(function () {
function updateCredentials() {
2015-06-26 20:27:38 -07:00
Logger.log('sending updated credentials to ApiClientBridge');
2015-05-28 16:37:43 -07:00
var json = JSON.stringify(ConnectionManager.credentialProvider().credentials());
var credentials = JSON.parse(json);
for (var i = 0, length = credentials.Servers.length; i < length; i++) {
var server = credentials.Servers[i];
if (server.DateLastAccessed != null) {
server.DateLastAccessed = new Date(server.DateLastAccessed).toISOString();
}
}
json = JSON.stringify(credentials);
ApiClientBridge.updateCredentials(json);
}
function initNativeConnectionManager() {
2015-06-26 20:27:38 -07:00
Logger.log('initNativeConnectionManager');
2015-05-28 16:37:43 -07:00
var capabilities = ConnectionManager.capabilities();
ApiClientBridge.init(AppInfo.appName, AppInfo.appVersion, AppInfo.deviceId, AppInfo.deviceName, JSON.stringify(capabilities));
2015-06-27 16:18:09 -07:00
initAjax();
}
var baseAjaxMethod;
var currentId = 0;
function getNewRequestId() {
var id = currentId++;
return id.toString();
}
function initAjax() {
2015-06-30 10:21:20 -07:00
baseAjaxMethod = HttpClient.send;
HttpClient.send = sendRequest;
2015-06-27 16:18:09 -07:00
}
function sendRequest(request) {
// For now, we can only handle json responses
if (request.dataType) {
if (request.dataType != 'json') {
return baseAjaxMethod(request);
}
}
if (request.data) {
// For now, we can only handle request bodies that are strings
if (typeof (request.data) != 'string') {
return baseAjaxMethod(request);
}
}
var deferred = DeferredBuilder.Deferred();
var id = getNewRequestId();
request.headers = request.headers || {};
if (request.dataType == 'json') {
request.headers.accept = 'application/json';
}
var javaRequest = {
Method: request.type || "GET",
Url: request.url,
RequestHeaders: request.headers
};
if (request.timeout) {
javaRequest.Timeout = request.timeout;
}
if (request.data) {
javaRequest.RequestContent = request.data;
}
if (request.contentType) {
javaRequest.RequestContentType = request.contentType;
}
Logger.log("Sending request: " + JSON.stringify(javaRequest));
ApiClientBridge.sendRequest(JSON.stringify(javaRequest), request.dataType, id);
2015-06-29 11:45:42 -07:00
Events.on(AndroidAjax, 'response' + id, function (e, isSuccess, response) {
2015-06-27 16:18:09 -07:00
Events.off(AndroidAjax, 'response' + id);
if (isSuccess) {
if (response) {
deferred.resolveWith(null, [response]);
} else {
deferred.resolve();
}
}
else {
// Need to mimic the jquery ajax error response
deferred.rejectWith(request, [getErrorResponse(response)]);
}
});
return deferred.promise();
}
function getErrorResponse(response) {
var error = {};
2015-06-29 11:45:42 -07:00
if (response.StatusCode) {
error.status = response.StatusCode;
}
2015-06-27 16:18:09 -07:00
error.ResponseHeaders = response.ResponseHeaders || {};
error.getResponseHeader = function (name) {
return error.ResponseHeaders[name];
};
return error;
2015-05-28 16:37:43 -07:00
}
Events.on(ConnectionManager.credentialProvider(), 'credentialsupdated', updateCredentials);
updateCredentials();
initNativeConnectionManager();
window.AndroidAjax = {
2015-06-29 11:45:42 -07:00
onResponse: function (id, response) {
2015-05-28 16:37:43 -07:00
2015-06-29 11:45:42 -07:00
Events.trigger(AndroidAjax, 'response' + id, [true, response]);
2015-06-27 16:18:09 -07:00
},
2015-06-29 11:45:42 -07:00
onError: function (id, response) {
2015-06-27 16:18:09 -07:00
2015-06-29 11:45:42 -07:00
Events.trigger(AndroidAjax, 'response' + id, [false, response]);
2015-05-28 16:37:43 -07:00
}
};
})();