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

JellyfinApi static class for server information #92

Merged
merged 5 commits into from
Dec 12, 2020
Merged
Show file tree
Hide file tree
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
21 changes: 5 additions & 16 deletions src/components/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { getReportingParams } from '../helpers';

import {
displayItem,
displayUserInfo,
reportPlaybackProgress
reportPlaybackProgress,
startBackdropInterval
} from './jellyfinActions';

import {
Expand Down Expand Up @@ -92,13 +92,7 @@ export class commandHandler {

displayContentHandler(data: DataMessage): void {
if (!this.playbackManager.isPlaying()) {
displayItem(
$scope,
data.serverAddress,
data.accessToken,
data.userId,
data.options.ItemId
);
displayItem(data.options.ItemId);
}
}

Expand All @@ -122,7 +116,7 @@ export class commandHandler {
}

setSubtitleStreamIndexHandler(data: DataMessage): void {
setSubtitleStreamIndex($scope, data.options.index, data.serverAddress);
setSubtitleStreamIndex($scope, data.options.index);
}

// VolumeUp, VolumeDown and ToggleMute commands seem to be handled on the sender in the current implementation.
Expand All @@ -147,12 +141,7 @@ export class commandHandler {

IdentifyHandler(data: DataMessage): void {
if (!this.playbackManager.isPlaying()) {
displayUserInfo(
$scope,
data.serverAddress,
data.accessToken,
data.userId
);
startBackdropInterval();
} else {
// When a client connects send back the initial device state (volume etc) via a playbackstop message
reportPlaybackProgress(
Expand Down
132 changes: 65 additions & 67 deletions src/components/fetchhelper.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
/* eslint-disable */

export function getFetchPromise(request) {
var headers = request.headers || {};
'json' === request.dataType && (headers.accept = 'application/json');
var fetchRequest = {
headers: headers,
method: request.type,
credentials: 'same-origin'
},
contentType = request.contentType;
request.data &&
('string' == typeof request.data
? (fetchRequest.body = request.data)
: ((fetchRequest.body = paramsToString(request.data)),
(contentType =
contentType ||
'application/x-www-form-urlencoded; charset=UTF-8'))),
contentType && (headers['Content-Type'] = contentType);
headers: headers,
method: request.type,
credentials: 'same-origin'
};
var contentType = request.contentType;
if (request.data) {
if (typeof request.data == 'string') {
fetchRequest.body = request.data;
} else {
fetchRequest.body = paramsToString(request.data);
contentType =
contentType ||
'application/x-www-form-urlencoded; charset=UTF-8';
}
}
if (contentType) {
headers['Content-Type'] = contentType;
}
var url = request.url;
if (request.query) {
var paramString = paramsToString(request.query);
Expand All @@ -34,26 +37,24 @@ export function fetchWithTimeout(url, options, timeoutMs) {
),
new Promise(function (resolve, reject) {
var timeout = setTimeout(reject, timeoutMs);
(options = options || {}),
(options.credentials = 'same-origin'),
fetch(url, options).then(
function (response) {
clearTimeout(timeout),
console.log(
'fetchWithTimeout: succeeded connecting to url: ' +
url
),
resolve(response);
},
function (error) {
clearTimeout(timeout),
console.log(
'fetchWithTimeout: timed out connecting to url: ' +
url
),
reject();
}
);
options = options || {};
options.credentials = 'same-origin';
fetch(url, options).then(
function (response) {
clearTimeout(timeout);
console.log(
'fetchWithTimeout: succeeded connecting to url: ' + url
);
resolve(response);
},
function () {
clearTimeout(timeout);
console.log(
'fetchWithTimeout: timed out connecting to url: ' + url
);
reject();
}
);
})
);
}
Expand All @@ -74,42 +75,39 @@ export function paramsToString(params) {

export function ajax(request) {
if (!request) throw new Error('Request cannot be null');
return (
(request.headers = request.headers || {}),
console.log('requesting url: ' + request.url),
getFetchPromise(request).then(
function (response) {
return (
console.log(
'response status: ' +
response.status +
', url: ' +
request.url
),
response.status < 400
? 'json' === request.dataType ||
'application/json' === request.headers.accept
? response.json()
: 'text' === request.dataType ||
0 ===
(response.headers.get('Content-Type') || '')
.toLowerCase()
.indexOf('text/')
? response.text()
: response
: Promise.reject(response)
);
},
function (err) {
throw (
(console.log('request failed to url: ' + request.url), err)
);
request.headers = request.headers || {};
console.log('requesting url: ' + request.url);

return getFetchPromise(request).then(
function (response) {
console.log(
'response status: ' + response.status + ', url: ' + request.url
);
if (response.status >= 400) {
return Promise.reject(response);
} else if (
request.dataType === 'json' ||
request.headers.accept === 'application/json'
) {
return response.json();
} else if (
request.dataType === 'text' ||
(response.headers.get('Content-Type') || '')
.toLowerCase()
.indexOf('text/') === 0
) {
return response.text();
} else {
return response;
}
)
},
function (err) {
console.log('request failed to url: ' + request.url);
throw err;
}
);
}

export default {
getFetchPromise,
ajax
};
Loading