mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-15 14:03:06 +00:00
Merge branch 'master' into post-support
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { NgModule } from '@angular/core';
|
import { NgModule } from '@angular/core';
|
||||||
import { CommonModule, APP_BASE_HREF } from '@angular/common';
|
import { CommonModule, APP_BASE_HREF } from '@angular/common';
|
||||||
import { HttpModule } from '@angular/http';
|
import { HttpClientModule } from '@angular/common/http';
|
||||||
|
|
||||||
import { EffectsModule } from '@ngrx/effects';
|
import { EffectsModule } from '@ngrx/effects';
|
||||||
import { StoreModule, MetaReducer, META_REDUCERS } from '@ngrx/store';
|
import { StoreModule, MetaReducer, META_REDUCERS } from '@ngrx/store';
|
||||||
@@ -51,7 +51,7 @@ if (!ENV_CONFIG.production) {
|
|||||||
@NgModule({
|
@NgModule({
|
||||||
imports: [
|
imports: [
|
||||||
CommonModule,
|
CommonModule,
|
||||||
HttpModule,
|
HttpClientModule,
|
||||||
AppRoutingModule,
|
AppRoutingModule,
|
||||||
CoreModule.forRoot(),
|
CoreModule.forRoot(),
|
||||||
NgbModule.forRoot(),
|
NgbModule.forRoot(),
|
||||||
|
@@ -30,15 +30,12 @@ export class RequestEffects {
|
|||||||
})
|
})
|
||||||
.map((entry: RequestEntry) => entry.request)
|
.map((entry: RequestEntry) => entry.request)
|
||||||
.flatMap((request: RestRequest) => {
|
.flatMap((request: RestRequest) => {
|
||||||
const httpRequestConfig: RequestArgs = {
|
let body;
|
||||||
method: request.method,
|
|
||||||
url: request.href
|
|
||||||
};
|
|
||||||
if (isNotEmpty(request.body)) {
|
if (isNotEmpty(request.body)) {
|
||||||
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
||||||
httpRequestConfig.body = JSON.stringify(serializer.serialize(request.body));
|
body = JSON.stringify(serializer.serialize(request.body));
|
||||||
}
|
}
|
||||||
return this.restApi.request(new Request(httpRequestConfig))
|
return this.restApi.request(request.method, request.href, body)
|
||||||
.map((data: DSpaceRESTV2Response) =>
|
.map((data: DSpaceRESTV2Response) =>
|
||||||
this.injector.get(request.getResponseParser()).parse(request, data))
|
this.injector.get(request.getResponseParser()).parse(request, data))
|
||||||
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
|
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
|
||||||
|
68
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
Normal file
68
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
import { TestBed, inject } from '@angular/core/testing';
|
||||||
|
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
|
||||||
|
|
||||||
|
import { DSpaceRESTv2Service } from './dspace-rest-v2.service';
|
||||||
|
|
||||||
|
describe('DSpaceRESTv2Service', () => {
|
||||||
|
let dSpaceRESTv2Service: DSpaceRESTv2Service;
|
||||||
|
let httpMock: HttpTestingController;
|
||||||
|
const url = 'http://www.dspace.org/';
|
||||||
|
const mockError = new ErrorEvent('test error');
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [HttpClientTestingModule],
|
||||||
|
providers: [DSpaceRESTv2Service]
|
||||||
|
});
|
||||||
|
|
||||||
|
dSpaceRESTv2Service = TestBed.get(DSpaceRESTv2Service);
|
||||||
|
httpMock = TestBed.get(HttpTestingController);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => httpMock.verify());
|
||||||
|
|
||||||
|
it('should be created', inject([DSpaceRESTv2Service], (service: DSpaceRESTv2Service) => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('#get', () => {
|
||||||
|
it('should return an Observable<DSpaceRESTV2Response>', () => {
|
||||||
|
const mockPayload = {
|
||||||
|
page: 1
|
||||||
|
};
|
||||||
|
const mockStatusCode = 'GREAT';
|
||||||
|
|
||||||
|
dSpaceRESTv2Service.get(url).subscribe((response) => {
|
||||||
|
expect(response).toBeTruthy();
|
||||||
|
expect(response.statusCode).toEqual(mockStatusCode);
|
||||||
|
expect(response.payload.page).toEqual(mockPayload.page);
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = httpMock.expectOne(url);
|
||||||
|
expect(req.request.method).toBe('GET');
|
||||||
|
req.flush(mockPayload, { statusText: mockStatusCode});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error', () => {
|
||||||
|
dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => {
|
||||||
|
expect(err.error).toBe(mockError);
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = httpMock.expectOne(url);
|
||||||
|
expect(req.request.method).toBe('GET');
|
||||||
|
req.error(mockError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should log an error', () => {
|
||||||
|
spyOn(console, 'log');
|
||||||
|
|
||||||
|
dSpaceRESTv2Service.get(url).subscribe(() => undefined, (err) => {
|
||||||
|
expect(console.log).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = httpMock.expectOne(url);
|
||||||
|
expect(req.request.method).toBe('GET');
|
||||||
|
req.error(mockError);
|
||||||
|
});
|
||||||
|
});
|
@@ -1,20 +1,18 @@
|
|||||||
import { Inject, Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Http, RequestOptionsArgs, Request } from '@angular/http';
|
import { Request } from '@angular/http';
|
||||||
|
import { HttpClient, HttpResponse } from '@angular/common/http'
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
import { RestRequestMethod } from '../data/request.models';
|
||||||
|
|
||||||
import { RESTURLCombiner } from '../url-combiner/rest-url-combiner';
|
|
||||||
import { DSpaceRESTV2Response } from './dspace-rest-v2-response.model';
|
import { DSpaceRESTV2Response } from './dspace-rest-v2-response.model';
|
||||||
|
|
||||||
import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
|
||||||
import { RestRequest } from '../data/request.models';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service to access DSpace's REST API
|
* Service to access DSpace's REST API
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DSpaceRESTv2Service {
|
export class DSpaceRESTv2Service {
|
||||||
|
|
||||||
constructor(private http: Http, @Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig) {
|
constructor(private http: HttpClient) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,14 +21,12 @@ export class DSpaceRESTv2Service {
|
|||||||
*
|
*
|
||||||
* @param absoluteURL
|
* @param absoluteURL
|
||||||
* A URL
|
* A URL
|
||||||
* @param options
|
|
||||||
* A RequestOptionsArgs object, with options for the http call.
|
|
||||||
* @return {Observable<string>}
|
* @return {Observable<string>}
|
||||||
* An Observable<string> containing the response from the server
|
* An Observable<string> containing the response from the server
|
||||||
*/
|
*/
|
||||||
get(absoluteURL: string, options?: RequestOptionsArgs): Observable<DSpaceRESTV2Response> {
|
get(absoluteURL: string): Observable<DSpaceRESTV2Response> {
|
||||||
return this.http.get(absoluteURL, options)
|
return this.http.get(absoluteURL, { observe: 'response' })
|
||||||
.map((res) => ({ payload: res.json(), statusCode: res.statusText }))
|
.map((res: HttpResponse<any>) => ({ payload: res.body, statusCode: res.statusText }))
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.log('Error: ', err);
|
console.log('Error: ', err);
|
||||||
return Observable.throw(err);
|
return Observable.throw(err);
|
||||||
@@ -40,19 +36,18 @@ export class DSpaceRESTv2Service {
|
|||||||
/**
|
/**
|
||||||
* Performs a request to the REST API.
|
* Performs a request to the REST API.
|
||||||
*
|
*
|
||||||
* @param httpRequest
|
* @param method
|
||||||
* A Request object
|
* the HTTP method for the request
|
||||||
|
* @param url
|
||||||
|
* the URL for the request
|
||||||
|
* @param body
|
||||||
|
* an optional body for the request
|
||||||
* @return {Observable<string>}
|
* @return {Observable<string>}
|
||||||
* An Observable<string> containing the response from the server
|
* An Observable<string> containing the response from the server
|
||||||
*/
|
*/
|
||||||
request(httpRequest: Request): Observable<DSpaceRESTV2Response> {
|
request(method: RestRequestMethod, url: string, body?: any): Observable<DSpaceRESTV2Response> {
|
||||||
// const httpRequest = new Request({
|
return this.http.request(method, url, { body, observe: 'response' })
|
||||||
// method: request.method,
|
.map((res) => ({ payload: res.body, statusCode: res.statusText }))
|
||||||
// url: request.href,
|
|
||||||
// body: request.body
|
|
||||||
// });
|
|
||||||
return this.http.request(httpRequest)
|
|
||||||
.map((res) => ({ payload: res.json(), statusCode: res.statusText }))
|
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.log('Error: ', err);
|
console.log('Error: ', err);
|
||||||
return Observable.throw(err);
|
return Observable.throw(err);
|
||||||
|
@@ -1,14 +1,13 @@
|
|||||||
import 'rxjs/add/observable/throw';
|
import 'rxjs/add/observable/throw';
|
||||||
import 'rxjs/add/operator/map';
|
|
||||||
import 'rxjs/add/operator/catch';
|
import 'rxjs/add/operator/catch';
|
||||||
|
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Http } from '@angular/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ApiService {
|
export class ApiService {
|
||||||
constructor(public _http: Http) {
|
constructor(public _http: HttpClient) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,7 +16,6 @@ export class ApiService {
|
|||||||
*/
|
*/
|
||||||
get(url: string, options?: any) {
|
get(url: string, options?: any) {
|
||||||
return this._http.get(url, options)
|
return this._http.get(url, options)
|
||||||
.map((res) => res.json())
|
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.log('Error: ', err);
|
console.log('Error: ', err);
|
||||||
return Observable.throw(err);
|
return Observable.throw(err);
|
||||||
|
Reference in New Issue
Block a user