mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-09 02:54:13 +00:00
intermediate commit
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { Observable, throwError as observableThrowError } from 'rxjs';
|
||||
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 { ResponseCacheService } from '../cache/response-cache.service';
|
||||
@@ -8,7 +10,7 @@ import { GlobalConfig } from '../../../config/global-config.interface';
|
||||
import { isNotEmpty } from '../../shared/empty.util';
|
||||
import { AuthGetRequest, AuthPostRequest, PostRequest, RestRequest } from '../data/request.models';
|
||||
import { ResponseCacheEntry } from '../cache/response-cache.reducer';
|
||||
import { AuthStatusResponse, ErrorResponse, RestResponse } from '../cache/response-cache.models';
|
||||
import { AuthStatusResponse, ErrorResponse } from '../cache/response-cache.models';
|
||||
import { HttpOptions } from '../dspace-rest-v2/dspace-rest-v2.service';
|
||||
|
||||
@Injectable()
|
||||
@@ -23,18 +25,18 @@ export class AuthRequestService {
|
||||
}
|
||||
|
||||
protected fetchRequest(request: RestRequest): Observable<any> {
|
||||
const [successResponse, errorResponse] = this.responseCache.get(request.href)
|
||||
.map((entry: ResponseCacheEntry) => entry.response)
|
||||
return this.responseCache.get(request.href).pipe(
|
||||
map((entry: ResponseCacheEntry) => entry.response),
|
||||
// TODO to review when https://github.com/DSpace/dspace-angular/issues/217 will be fixed
|
||||
.do(() => this.responseCache.remove(request.href))
|
||||
.partition((response: RestResponse) => response.isSuccessful);
|
||||
return Observable.merge(
|
||||
errorResponse.flatMap((response: ErrorResponse) =>
|
||||
observableThrowError(new Error(response.errorMessage))),
|
||||
successResponse
|
||||
.filter((response: AuthStatusResponse) => isNotEmpty(response))
|
||||
.map((response: AuthStatusResponse) => response.response)
|
||||
.distinctUntilChanged());
|
||||
tap(() => this.responseCache.remove(request.href)),
|
||||
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 {
|
||||
@@ -42,24 +44,24 @@ export class AuthRequestService {
|
||||
}
|
||||
|
||||
public postToEndpoint(method: string, body: any, options?: HttpOptions): Observable<any> {
|
||||
return this.halService.getEndpoint(this.linkName)
|
||||
.filter((href: string) => isNotEmpty(href))
|
||||
.map((endpointURL) => this.getEndpointByMethod(endpointURL, method))
|
||||
.distinctUntilChanged()
|
||||
.map((endpointURL: string) => new AuthPostRequest(this.requestService.generateRequestId(), endpointURL, body, options))
|
||||
.do((request: PostRequest) => this.requestService.configure(request, true))
|
||||
.flatMap((request: PostRequest) => this.fetchRequest(request))
|
||||
.distinctUntilChanged();
|
||||
return this.halService.getEndpoint(this.linkName).pipe(
|
||||
filter((href: string) => isNotEmpty(href)),
|
||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||
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> {
|
||||
return this.halService.getEndpoint(this.linkName)
|
||||
.filter((href: string) => isNotEmpty(href))
|
||||
.map((endpointURL) => this.getEndpointByMethod(endpointURL, method))
|
||||
.distinctUntilChanged()
|
||||
.map((endpointURL: string) => new AuthGetRequest(this.requestService.generateRequestId(), endpointURL, options))
|
||||
.do((request: PostRequest) => this.requestService.configure(request, true))
|
||||
.flatMap((request: PostRequest) => this.fetchRequest(request))
|
||||
.distinctUntilChanged();
|
||||
return this.halService.getEndpoint(this.linkName).pipe(
|
||||
filter((href: string) => isNotEmpty(href)),
|
||||
map((endpointURL) => this.getEndpointByMethod(endpointURL, method)),
|
||||
distinctUntilChanged(),
|
||||
map((endpointURL: string) => new AuthGetRequest(this.requestService.generateRequestId(), endpointURL, options)),
|
||||
tap((request: PostRequest) => this.requestService.configure(request, true)),
|
||||
mergeMap((request: PostRequest) => this.fetchRequest(request)),
|
||||
distinctUntilChanged());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user