Resolve "Read library account URLs from the backend"

This commit is contained in:
Jovan Krunić
2022-04-05 13:30:47 +00:00
parent 93c37b26cc
commit d2a23e581e

View File

@@ -1,53 +1,63 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
// import {ConfigProvider} from '../../config/config.provider';
import {PAIAAuthService} from '../../auth/paia/paia-auth.service';
import {JQueryRequestor, Requestor} from '@openid/appauth'; import {JQueryRequestor, Requestor} from '@openid/appauth';
import {SCFeatureConfigurationExtern, SCMap} from '@openstapps/core'; import {
SCAuthorizationProviderType,
SCFeatureConfiguration,
SCFeatureConfigurationExtern,
} from '@openstapps/core';
import {PAIADocumentStatus, PAIAFees, PAIAItems, PAIAPatron} from '../types'; import {PAIADocumentStatus, PAIAFees, PAIAItems, PAIAPatron} from '../types';
import {HebisDataProvider} from '../../hebis/hebis-data.provider'; import {HebisDataProvider} from '../../hebis/hebis-data.provider';
import {ConfigProvider} from '../../config/config.provider';
import {AuthHelperService} from '../../auth/auth-helper.service';
import {PAIATokenResponse} from '../../auth/paia/paia-token-response';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class LibraryAccountService { export class LibraryAccountService {
// TODO: must come from backend (not that stable) /**
private _config: SCMap<SCFeatureConfigurationExtern> = { * Base url of the external service
profile: { */
url: 'https://hds.hebis.de:8443/core/{patron}', baseUrl: string;
},
items: { /**
url: 'https://hds.hebis.de:8443/core/{patron}/items', * Authorization provider type
}, */
fees: { authType: SCAuthorizationProviderType;
url: 'https://hds.hebis.de:8443/core/{patron}/fees',
},
};
constructor( constructor(
protected requestor: Requestor = new JQueryRequestor(), protected requestor: Requestor = new JQueryRequestor(),
private readonly paiaAuth: PAIAAuthService,
private readonly hebisDataProvider: HebisDataProvider, private readonly hebisDataProvider: HebisDataProvider,
) {} private readonly authHelper: AuthHelperService,
readonly configProvider: ConfigProvider,
get config() { ) {
return this._config; const config: SCFeatureConfigurationExtern = (
configProvider.getValue('features') as SCFeatureConfiguration
).extern!.paia;
this.baseUrl = config.url;
this.authType = config.authProvider as SCAuthorizationProviderType;
} }
async getProfile() { async getProfile() {
return this.performRequest<PAIAPatron>(this.config.profile.url); return this.performRequest<PAIAPatron>(`${this.baseUrl}/{patron}`);
} }
async getItems() { async getItems() {
return this.performRequest<PAIAItems>(this.config.items.url); return this.performRequest<PAIAItems>(`${this.baseUrl}/{patron}/items`);
} }
async getFees() { async getFees() {
return this.performRequest<PAIAFees>(this.config.fees.url); return this.performRequest<PAIAFees>(`${this.baseUrl}/{patron}/fees`);
} }
private async performRequest<T>(urlTemplate: string): Promise<T> { private async performRequest<T>(urlTemplate: string): Promise<T> {
const token = await this.paiaAuth.getValidToken(); const token = await this.authHelper
const url = urlTemplate.replace('{patron}', token.patron); .getProvider(this.authType)
.getValidToken();
const url = urlTemplate.replace(
'{patron}',
(token as PAIATokenResponse).patron,
);
const settings: JQueryAjaxSettings = { const settings: JQueryAjaxSettings = {
url: url, url: url,
dataType: 'json', dataType: 'json',