Commit a88a4ae0 authored by laurent l's avatar laurent l

fix bundle

parent ebd3f9bb
import * as http from 'http';
import * as http from 'tns-core-modules/http';
export interface HTTPClient {
request(options: any): Promise<http.HttpResponse>;
request(options: any): Promise<http.HttpResponse>;
}
import { HTTPClient } from './http-client';
import { Cookies } from '../';
import * as http from 'http';
import * as http from 'tns-core-modules/http';
export class NSHTTPClient implements HTTPClient {
public request(options: any): Promise<http.HttpResponse> {
......
import * as http from 'http';
import {ImageSource} from 'tns-core-modules/image-source'
import {Cookies,
NSHTTPClient,
Account,
Database,
ItemOperation,
Loan,
Item,
Form,
Hold} from '../../models';
import * as http from 'tns-core-modules/http';
import { ImageSource } from 'tns-core-modules/image-source'
import {
Cookies,
NSHTTPClient,
Account,
Database,
ItemOperation,
Loan,
Item,
Form,
Hold
} from '../../models';
export abstract class PortalAdapter {
private _http: any;
......@@ -20,13 +22,13 @@ export abstract class PortalAdapter {
}
public setHTTP(http:any): PortalAdapter {
public setHTTP(http: any): PortalAdapter {
this._http = http;
return this;
}
public updateAdapterHTTPClient(adapter:PortalAdapter): PortalAdapter {
public updateAdapterHTTPClient(adapter: PortalAdapter): PortalAdapter {
adapter.setHTTP(this._http);
return this;
}
......@@ -34,64 +36,64 @@ export abstract class PortalAdapter {
public signIn(account): Promise<any> {
this._cookies.clearCookies();
return this
._signIn(account)
.then( () => {
.then(() => {
return this.updateAccountInfo(account);
} );
});
}
public refresh(account): Promise<any> {
if (!account.isConnected()) {
account.setCookies(new Array<any>());
return this._signIn(account).then( () => {
return this._signIn(account).then(() => {
return this.refresh(account);
});
}
let db: Database = Database.current();
return this
return this
._fetchLoans(account)
.then( (loans) => {
.then((loans) => {
account.deleteLoans(db);
loans.forEach( (loan) => {
loans.forEach((loan) => {
db.save(loan.setAccount(account));
});
db.save(account);
})
.then( () => {
.then(() => {
return this._fetchHolds(account);
})
.then( (holds) => {
.then((holds) => {
account.deleteHolds(db);
holds.forEach( (hold) => {
holds.forEach((hold) => {
db.save(hold.setAccount(account));
});
account.setNumberOfHolds(holds.length);
db.save(account);
db.save(account);
})
}
public request(account: Account, options: any): Promise<http.HttpResponse> {
this._cookies.setCookies(account.getCookies());;
return this._http
.request(options)
.then( (response) => {
.then((response) => {
account.setCookies(this._cookies.getCookies());
return response;
} );
});
}
......@@ -119,7 +121,7 @@ export abstract class PortalAdapter {
return Promise.resolve();
}
public updateAccountInfo(account): Promise<any> {
return (0 < account.label.length)
? Promise.resolve()
......@@ -139,31 +141,31 @@ export abstract class PortalAdapter {
return Promise.resolve(account.credentials['login']);
};
protected _fetchHolds(account: Account): Promise<Array<Hold>> {
return Promise.resolve(new Array<Hold>());
}
protected _fetchThumbnails<T extends ItemOperation>(account: Account,
items: Array<T>): Promise<Array<T>> {
return Promise.all( items.map( (item) => {
items: Array<T>): Promise<Array<T>> {
return Promise.all(items.map((item) => {
return this._fetchThumbnail(account, item);
}))
}
protected _fetchThumbnail<T extends ItemOperation>(account: Account, item: T): Promise<T> {
if ( !item.getRecordThumbnail() )
if (!item.getRecordThumbnail())
return Promise.resolve(item);
return this
.request(account,
{
url: item.getRecordThumbnail(),
headers: { Referer: account.getUrl() }
})
.then( (response) => {
{
url: item.getRecordThumbnail(),
headers: { Referer: account.getUrl() }
})
.then((response) => {
if (response.statusCode == 301 || response.statusCode == 302) {
let location = <any>response.headers['Location']
item.setRecordThumbnail(location)
......@@ -173,10 +175,10 @@ export abstract class PortalAdapter {
if (response.statusCode != 200) {
return Promise.resolve(item)
}
return response.content
.toImage()
.then( (image_source: ImageSource) => {
.then((image_source: ImageSource) => {
return item.setRecordThumbnail('data:image/jpg;base64,' + image_source.toBase64String('jpg'));
})
})
......
import * as http from 'http';
import {PortalAdapter} from './adapter';
import {Database, Account, Hold, Loan, Record, Item} from '../../models';
import {WrongLoginPassword} from './wrong-login-password';
import * as http from 'tns-core-modules/http';
import { PortalAdapter } from './adapter';
import { Database, Account, Hold, Loan, Record, Item } from '../../models';
import { WrongLoginPassword } from './wrong-login-password';
export class InMediaV2 extends PortalAdapter {
......@@ -9,7 +9,7 @@ export class InMediaV2 extends PortalAdapter {
return 'inmedia-v2';
}
public getLabel(): string {
return 'InMedia (V2)';
}
......@@ -26,20 +26,22 @@ export class InMediaV2 extends PortalAdapter {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/authenticate',
method: "POST",
content: "locale=fr&username=" + account.credentials['login'] + "&password=" + account.credentials['password'],
headers: {"Content-Type": "application/x-www-form-urlencoded"} })
.then( (response) => {
{
url: account.getUrl() + '/in/rest/api/authenticate',
method: "POST",
content: "locale=fr&username=" + account.credentials['login'] + "&password=" + account.credentials['password'],
headers: { "Content-Type": "application/x-www-form-urlencoded" }
})
.then((response) => {
return this._handleSignIn(account, response)
} )
})
}
protected _handleSignIn(account: Account, response: http.HttpResponse): Promise<any> {
if (response.statusCode == 401)
return Promise.reject(new WrongLoginPassword());
let datas = response.content.toJSON()
account.credentials['token'] = datas.token
account.beConnected()
......@@ -50,9 +52,11 @@ export class InMediaV2 extends PortalAdapter {
protected _fetchAccountSummary(account: Account): Promise<any> {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/accountSummary',
headers: { authorization: 'Bearer ' + account.credentials['token'] }})
.then( (response) => {
{
url: account.getUrl() + '/in/rest/api/accountSummary',
headers: { authorization: 'Bearer ' + account.credentials['token'] }
})
.then((response) => {
return response.content.toJSON()
})
}
......@@ -61,7 +65,7 @@ export class InMediaV2 extends PortalAdapter {
protected _fetchAccountLabel(account: Account): Promise<string> {
return this
._fetchAccountSummary(account)
.then( (datas) => {
.then((datas) => {
return datas['name'];
})
}
......@@ -70,7 +74,7 @@ export class InMediaV2 extends PortalAdapter {
public updateCard(account: Account): Promise<any> {
return this
._fetchAccountSummary(account)
.then( (datas) => {
.then((datas) => {
let card = account.getCard()
card.setNumber(datas['subscriberId'])
card.setExpirationDate(this._formatDate(datas['expiryDate']))
......@@ -80,20 +84,22 @@ export class InMediaV2 extends PortalAdapter {
public requestUser(account: Account, action: string): Promise<any> {
return this
.request(account,
{ url: account.getUrl() + '/in/rest/api/accountPage?type=' + action,
headers: { authorization: 'Bearer ' + account.credentials['token'] }})
.then( (response) => {
.request(account,
{
url: account.getUrl() + '/in/rest/api/accountPage?type=' + action,
headers: { authorization: 'Bearer ' + account.credentials['token'] }
})
.then((response) => {
return response.content.toJSON();
});
}
public _fetchLoans(account: Account): Promise<Array<Loan>> {
return this
.requestUser(account, 'loans&pageNo=1&pageSize=100')
.then( (datas) => {
return datas['items'].map( (loan) => {
.then((datas) => {
return datas['items'].map((loan) => {
return new Loan()
.setLoanId(loan['data']['seqNo'])
.setTitle(loan['data']['title'])
......@@ -109,9 +115,9 @@ export class InMediaV2 extends PortalAdapter {
public _fetchHolds(account: Account): Promise<Array<Hold>> {
return this
.requestUser(account, 'reservations&pageNo=1&pageSize=100')
.then( (datas) => {
return datas['items'].map( (loan) => {
.then((datas) => {
return datas['items'].map((loan) => {
return new Hold()
.setHoldId(loan['data']['seqNo'])
.setTitle(loan['data']['title'])
......@@ -125,15 +131,17 @@ export class InMediaV2 extends PortalAdapter {
protected _formatDate(date: string): string {
return [date.substr(0,4),
date.substr(4,2),
date.substr(6,2)].join('-')
return [date.substr(0, 4),
date.substr(4, 2),
date.substr(6, 2)].join('-')
}
protected _holdStatus(code: string): string {
let codes = {RESV_NOT_AVAILABLE: L('hold_not_available'),
RESV_AVAILABLE: L('hold_available')};
let codes = {
RESV_NOT_AVAILABLE: L('hold_not_available'),
RESV_AVAILABLE: L('hold_available')
};
return codes[code] ? codes[code] : L('unknown');
}
}
This diff is collapsed.
declare var unescape: any;
import * as http from 'http';
import {PortalAdapter} from './adapter';
import * as http from 'tns-core-modules/http';
import { PortalAdapter } from './adapter';
import * as cheerio from 'cheerio';
import {Account, Loan} from '../../models';
import {WrongLoginPassword} from './wrong-login-password';
import { Account, Loan } from '../../models';
import { WrongLoginPassword } from './wrong-login-password';
import * as UrlParse from 'url-parse';
export class Symphony extends PortalAdapter {
public getIdentifier():string {
return 'symphony';
}
public getIdentifier(): string {
return 'symphony';
}
public getLabel():string {
return 'Symphony';
}
public getLabel(): string {
return 'Symphony';
}
public canHandleWebsite($): boolean {
return $('form[action*="/web2/tramp2.exe"]').length;
return $('form[action*="/web2/tramp2.exe"]').length;
}
......@@ -31,7 +31,7 @@ export class Symphony extends PortalAdapter {
public request(account: Account, options: any): Promise<http.HttpResponse> {
if (!options.headers)
options.headers={};
options.headers = {};
options.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0';
return super.request(account, options);
}
......@@ -43,7 +43,7 @@ export class Symphony extends PortalAdapter {
return this
._openSession(account)
.then ( ($) => {
.then(($) => {
if ($('input[name="userid"]').length > 0)
return Promise.reject(new WrongLoginPassword());
......@@ -56,7 +56,7 @@ export class Symphony extends PortalAdapter {
protected _fetchAccountLabel(account: Account): Promise<any> {
return this
._openSession(account)
.then( ($) => {
.then(($) => {
return $('table table td h2').text().trim();
});
}
......@@ -64,16 +64,16 @@ export class Symphony extends PortalAdapter {
protected _openSession(account: Account): Promise<any> {
let base_url = new UrlParse(account.getUrl()).origin;
return this
.request(account, { url: account.getUrl() })
.then( (response) => {
.then((response) => {
let $ = cheerio.load(response.content.toString());
let account_link = $('a[href*="screen=MyAccount.html"]');
return this
.request(account, { url: base_url + account_link.attr('href') })
})
.then( (response) => {
.then((response) => {
let $ = cheerio.load(response.content.toString());
let form = $('form[action*=login_when_needed]');
let login_data = {
......@@ -88,13 +88,14 @@ export class Symphony extends PortalAdapter {
return this
.request(account,
{ url: base_url + form.attr('action'),
method: 'POST',
content: this._encodeForm(login_data),
headers: {"Content-Type": "application/x-www-form-urlencoded"}
})
{
url: base_url + form.attr('action'),
method: 'POST',
content: this._encodeForm(login_data),
headers: { "Content-Type": "application/x-www-form-urlencoded" }
})
})
.then( (response) => {
.then((response) => {
return cheerio.load(response.content.toString());
})
}
......@@ -104,25 +105,25 @@ export class Symphony extends PortalAdapter {
protected _fetchLoans(account: Account): Promise<Array<Loan>> {
return this
._openSession(account)
.then( ($) => {
.then(($) => {
let loans = new Array<Loan>();
let rows = $('div#panel2 table:first-child + table tr + tr');
rows.each( (i, element) => {
rows.each((i, element) => {
let node = $(element);
let title = node.find('td:nth-child(2) a').text().trim();
let author = node.find('td:nth-child(3)').text().trim();
let date_due = node.find('td:nth-child(6)').text().trim().split(',')[0];
let loan = (new Loan())
.setTitle(title)
.setAuthor(author)
.setDateDue(date_due
.split('/')
.reverse()
.map( (part) => {
return part.padStart(2, '0');
})
.join('-'));
.split('/')
.reverse()
.map((part) => {
return part.padStart(2, '0');
})
.join('-'));
loans.push(loan);
});
return loans;
......
declare var expect: any;
import * as http from 'http';
import {ImageSource} from 'tns-core-modules/image-source'
import {HTTPClient} from '../../models';
import * as http from 'tns-core-modules/http';
import { ImageSource } from 'tns-core-modules/image-source'
import { HTTPClient } from '../../models';
export abstract class HTTPScenario implements HTTPClient {
......@@ -9,24 +9,26 @@ export abstract class HTTPScenario implements HTTPClient {
public request(options: any): Promise<http.HttpResponse> {
let call = this._calls.shift();
if (!call)
return Promise.reject({ error: 'unexpected request',
received: options });
return Promise.reject({
error: 'unexpected request',
received: options
});
expect(call.request).to.deep.equal(options,
'\n ** expected:\n'
+ JSON.stringify(call.request)
+ '\n ** received: \n'
+ JSON.stringify(options));
'\n ** expected:\n'
+ JSON.stringify(call.request)
+ '\n ** received: \n'
+ JSON.stringify(options));
return new Promise( (resolve, reject) => {
return new Promise((resolve, reject) => {
resolve(this._newHttpResponse(call.response));
});
}
protected _newHttpResponse(response:any): any {
protected _newHttpResponse(response: any): any {
return {
statusCode: response.statusCode ? response.statusCode : 200,
......@@ -35,13 +37,13 @@ export abstract class HTTPScenario implements HTTPClient {
return response.content;
},
toJSON: () => {
toJSON: () => {
return JSON.parse(response.content);
},
toImage: () => {
let source = new ImageSource()
return source.fromBase64(response.content).then( () => {
return source.fromBase64(response.content).then(() => {
return source;
})
}
......@@ -50,11 +52,13 @@ export abstract class HTTPScenario implements HTTPClient {
headers: response.headers ? response.headers : {}
}
}
public expect(request, response): this {
this._calls.push({request: request,
response: response});
this._calls.push({
request: request,
response: response
});
return this;
}
......@@ -64,7 +68,7 @@ export abstract class HTTPScenario implements HTTPClient {
}
protected _encapsulate(html:string): string {
protected _encapsulate(html: string): string {
return '<html><head></head><body>' + html + '</body></html>';
}
}
......@@ -38,6 +38,7 @@
"tns-core-modules": "^5.2.2",
"tns-platform-declarations": "^3.4.0",
"url-parse": "^1.4.4",
"util": "^0.10.0",
"validator": "^7.0.0",
"webpack": "^4.29.6",
"xml-js": "^1.6.7"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment