Skip to content

Commit

Permalink
feat(kaltura-clients): remove configuration objects, assign dynamic d…
Browse files Browse the repository at this point in the history
…ata directly on the clients

BREAKING CHANGE: the 'KalturaClientBaseConfiguration' and 'KalturaHttpClientConfiguration' objects were removed.

Any dynamic data assigned on them should be done directly on the client instance.
  • Loading branch information
esakal committed May 17, 2017
1 parent 0a17158 commit 8a30a72
Show file tree
Hide file tree
Showing 11 changed files with 221 additions and 135 deletions.
2 changes: 0 additions & 2 deletions src/kaltura-clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { KalturaHttpClientConfiguration } from './kaltura-http-client-configuration';
export { KalturaClientBaseConfiguration} from './kaltura-client-base-configuration';
export { KalturaBrowserHttpClient } from './kaltura-browser-http-client';
export { KalturaClientBase } from './kaltura-client-base';
export { KalturaHttpClientBase } from './kaltura-http-client-base';
8 changes: 4 additions & 4 deletions src/kaltura-clients/kaltura-browser-http-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { KalturaHttpClientConfiguration } from "./kaltura-http-client-configuration";
import { KalturaRequest } from '../kaltura-request';
import { KalturaMultiRequest } from '../kaltura-multi-request';
import { KalturaMultiResponse } from '../kaltura-multi-response';
Expand All @@ -7,11 +6,12 @@ import { KalturaHttpClientBase } from './kaltura-http-client-base';
import { KalturaAPIException } from '../kaltura-api-exception';

export class KalturaBrowserHttpClient extends KalturaHttpClientBase {
constructor(adapterConfiguration: KalturaHttpClientConfiguration) {
super(adapterConfiguration);
}


constructor(config : { endpointUrl : string, clientTag : string, ks? : string, partnerId? : number}) {
super(config);
}

protected _createCancelableAction(data : { endpoint : string, headers : any, body : {}, type : any} ) : CancelableAction
{

Expand Down
28 changes: 0 additions & 28 deletions src/kaltura-clients/kaltura-client-base-configuration.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/kaltura-clients/kaltura-client-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import { CancelableAction } from '../utils/cancelable-action';

export abstract class KalturaClientBase {

ks : string;
partnerId : number;
public clientTag : string;

constructor(config : { clientTag : string, ks? : string, partnerId? : number})
{
this.clientTag = config.clientTag;
this.ks = config.ks;
this.partnerId = config.partnerId;
}

protected abstract _transmitFileUploadRequest(request): CancelableAction;
protected abstract _transmitRequest(request): CancelableAction;

Expand Down Expand Up @@ -76,4 +87,23 @@ export abstract class KalturaClientBase {
throw new KalturaAPIException("client::request_type_error", 'unsupported request type requested');
}
}

protected _assignDefaultParameters(parameters : any) : any
{
if (parameters) {
if (this.ks && typeof parameters['ks'] === 'undefined') {
parameters.ks = this.ks;
}

if (this.partnerId && typeof parameters['partnerId'] === 'undefined') {
parameters.partnerId = this.partnerId;
}
}

if (this.clientTag) {
parameters.clientTag = this.clientTag;
}

return parameters;
}
}
185 changes: 109 additions & 76 deletions src/kaltura-clients/kaltura-http-client-base.ts
Original file line number Diff line number Diff line change
@@ -1,109 +1,142 @@
import { KalturaHttpClientConfiguration } from './kaltura-http-client-configuration';
import { CancelableAction } from '../utils/cancelable-action';
import { KalturaClientBase } from './kaltura-client-base';
import { KalturaAPIException } from '../kaltura-api-exception';

export abstract class KalturaHttpClientBase extends KalturaClientBase {
constructor(public adapterConfiguration : KalturaHttpClientConfiguration) {
super();

public endpointUrl: string;


constructor(config : { endpointUrl : string, clientTag : string, ks? : string, partnerId? : number}) {
super(config);
this.endpointUrl = config.endpointUrl;
}

protected _transmitFileUploadRequest(request): CancelableAction
{
const result = new CancelableAction((resolve, reject) => {
let isComplete = false
const parameters: any = Object.assign(
{
format: 1
},
request.toRequestObject()
);
this.adapterConfiguration.prepareRequestParameters(parameters);
private _getHeaders(): any {
return {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
}

const data : any = request.getFormData();
protected _transmitFileUploadRequest(request): CancelableAction {
const isReadyContext = this._isReady();

const {service,action} = parameters;
delete parameters.service;
delete parameters.action;
if (!isReadyContext.ready) {
return new CancelableAction((resolve, reject) => {
let isComplete = false
const parameters: any = Object.assign(
{
format: 1
},
request.toRequestObject()
);

// build endpoint
const querystring = this._buildQuerystring(parameters);
const endpoint = `${this.adapterConfiguration.endpointUrl}/service/${service}/action/${action}?${querystring}`;
this._assignDefaultParameters(parameters);

const data: any = request.getFormData();

const {service, action} = parameters;
delete parameters.service;
delete parameters.action;

// build endpoint
const querystring = this._buildQuerystring(parameters);
const endpoint = `${this.endpointUrl}/service/${service}/action/${action}?${querystring}`;

const xhr = new XMLHttpRequest();
const xhr = new XMLHttpRequest();

xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
let resp;
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let resp;

try {
if(xhr.status == 200) {
resp = JSON.parse(xhr.response);
}else {
try {
if (xhr.status == 200) {
resp = JSON.parse(xhr.response);
} else {
resp = new Error(xhr.responseText);
}
} catch (e) {
resp = new Error(xhr.responseText);
}
} catch (e){
resp = new Error(xhr.responseText);
}

if (resp instanceof Error)
{
reject(resp);
}else {
resolve(resp);
if (resp instanceof Error) {
reject(resp);
} else {
resolve(resp);
}
}
};

const progressCallback = request._getProgressCallback();
if (progressCallback) {
xhr.upload.addEventListener('progress', function (e) {
if (e.lengthComputable) {
progressCallback.apply(request, [e.loaded, e.total]);
} else {
// Unable to compute progress information since the total size is unknown
}
}, false);
}
};

const progressCallback = request._getProgressCallback();
if (progressCallback) {
xhr.upload.addEventListener('progress', function(e){
if (e.lengthComputable) {
progressCallback.apply(request, [e.loaded, e.total]);
} else {
// Unable to compute progress information since the total size is unknown
}
}, false);
}

xhr.open('POST', endpoint);
xhr.send(data);
xhr.open('POST', endpoint);
xhr.send(data);

return () =>
{
if (!isComplete) {
xhr.abort();
isComplete = true;
return () => {
if (!isComplete) {
xhr.abort();
isComplete = true;
}
}
}
});

return result;

});
}
else {
return new CancelableAction((resolve, reject) => {
reject(isReadyContext.error);
});
}
}

protected abstract _createCancelableAction(data : { endpoint : string, headers : any, body : {}} ) : CancelableAction;

protected _transmitRequest(request): CancelableAction
private _isReady() : { ready : boolean, error : Error }
{
const parameters: any = Object.assign(
{
format: 1
},
request.toRequestObject()
);
if (!!this.endpointUrl) {
return {ready:false, error : new Error(`property 'endpoint' is required'`) };

this.adapterConfiguration.prepareRequestParameters(parameters);
}else {
return { ready : true, error : null };
}
}

// build endpoint
const endpoint = `${this.adapterConfiguration.endpointUrl}/service/${parameters.service}/action/${parameters.action}`;
protected _transmitRequest(request): CancelableAction {
const isReadyContext = this._isReady();

delete parameters.service;
delete parameters.action;
if (!isReadyContext.ready) {

const headers = this.adapterConfiguration.getHeaders();
const parameters: any = Object.assign(
{
format: 1
},
request.toRequestObject()
);

this._assignDefaultParameters(parameters);

return this._createCancelableAction({endpoint, headers, body : parameters});
// build endpoint
const endpoint = `${this.endpointUrl}/service/${parameters.service}/action/${parameters.action}`;

delete parameters.service;
delete parameters.action;

const headers = this._getHeaders();

return this._createCancelableAction({endpoint, headers, body: parameters});
}
else {
return new CancelableAction((resolve, reject) => {
reject(isReadyContext.error);
});
}
}

private _buildQuerystring(data : {}, prefix? : string)
Expand Down
13 changes: 0 additions & 13 deletions src/kaltura-clients/kaltura-http-client-configuration.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/tests/kaltura-multi-request.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
// import {KalturaResponse} from "../kaltura-response";
// import {KalturaMultiRequest} from "../kaltura-multi-request";
// import {KalturaHttpClientConfiguration} from "../kaltura-clients/kaltura-http-client-configuration";
// import {KalturaClientBase} from "../kaltura-clients/kaltura-server-client";
// import {KalturaHttpPostClient} from "../kaltura-clients/kaltura-http-post-client.service";
// import {UserLoginByLoginIdAction, UserGetByLoginIdAction} from "../services/user";
Expand Down
1 change: 0 additions & 1 deletion src/tests/kaltura-request.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import { KalturaHttpClientConfiguration} from "../kaltura-clients/kaltura-http-client-configuration";
// import { KalturaBrowserHttpClient } from "../kaltura-clients/kaltura-browser-http-client";
// import {
// KalturaBaseEntryFilter, KalturaMediaEntry, KalturaPlaylist, KalturaMediaEntryFilterForPlaylist,
Expand Down
Loading

0 comments on commit 8a30a72

Please sign in to comment.