Files
dspace-angular/src/app/core/auth/auth-request.service.ts
2019-08-02 10:39:58 +02:00

71 lines
3.3 KiB
TypeScript

import {Observable, of as observableOf, throwError as observableThrowError} from 'rxjs';
import {distinctUntilChanged, filter, map, mergeMap, tap} from 'rxjs/operators';
import {Inject, Injectable} from '@angular/core';
import {HALEndpointService} from '../shared/hal-endpoint.service';
import {RequestService} from '../data/request.service';
import {GLOBAL_CONFIG} from '../../../config';
import {GlobalConfig} from '../../../config/global-config.interface';
import {isNotEmpty} from '../../shared/empty.util';
import {AuthGetRequest, AuthPostRequest, GetRequest, PostRequest, RestRequest} from '../data/request.models';
import {AuthStatusResponse, ErrorResponse} from '../cache/response.models';
import {HttpOptions} from '../dspace-rest-v2/dspace-rest-v2.service';
import {getResponseFromEntry} from '../shared/operators';
import {HttpClient} from '@angular/common/http';
@Injectable()
export class AuthRequestService {
protected linkName = 'authn';
protected browseEndpoint = '';
constructor(@Inject(GLOBAL_CONFIG) protected EnvConfig: GlobalConfig,
protected halService: HALEndpointService,
protected requestService: RequestService,
private http: HttpClient) {
}
protected fetchRequest(request: RestRequest): Observable<any> {
return this.requestService.getByUUID(request.uuid).pipe(
getResponseFromEntry(),
mergeMap((response) => {
if (response.isSuccessful && isNotEmpty(response)) {
return observableOf((response as AuthStatusResponse).response);
} else if (!response.isSuccessful) {
return observableThrowError(new Error((response as ErrorResponse).errorMessage));
}
})
);
}
protected getEndpointByMethod(endpoint: string, method: string): string {
return isNotEmpty(method) ? `${endpoint}/${method}` : `${endpoint}`;
}
public postToEndpoint(method: string, body?: any, options?: HttpOptions): Observable<any> {
return this.halService.getEndpoint(this.linkName).pipe(
filter((href: string) => isNotEmpty(href)),
tap((href: string) => console.log('This is href in postToEndpoint(): ' , href)),
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
tap((href2) => {console.log('href2', href2)}),
distinctUntilChanged(),
map((endpointURL: string) => new AuthPostRequest(this.requestService.generateRequestId(), endpointURL, body, options)),
tap((request: PostRequest) => this.requestService.configure(request, true)),
mergeMap((request: PostRequest) => this.fetchRequest(request)),
distinctUntilChanged());
}
public getRequest(method: string, options?: HttpOptions): Observable<any> {
console.log('auth.request getRequest() was called');
return this.halService.getEndpoint(this.linkName).pipe(
filter((href: string) => isNotEmpty(href)),
tap((href) => console.log('auth-request.service getRequest()', href)),
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
tap((whatsThis) => console.log('whatsThis: ', whatsThis)),
distinctUntilChanged(),
map((endpointURL: string) => new AuthGetRequest(this.requestService.generateRequestId(), endpointURL, options)),
tap((request: GetRequest) => this.requestService.configure(request, true)),
mergeMap((request: GetRequest) => this.fetchRequest(request)),
distinctUntilChanged());
}
}