mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-14 21:43:04 +00:00
Replaced Http with HttpClient
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(),
|
||||||
|
37
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
Normal file
37
src/app/core/dspace-rest-v2/dspace-rest-v2.service.spec.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
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 mockDSpaceRESTV2Response = {};
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
imports: [HttpClientTestingModule],
|
||||||
|
providers: [DSpaceRESTv2Service]
|
||||||
|
});
|
||||||
|
|
||||||
|
dSpaceRESTv2Service = TestBed.get(DSpaceRESTv2Service);
|
||||||
|
httpMock = TestBed.get(HttpTestingController);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', inject([DSpaceRESTv2Service], (service: DSpaceRESTv2Service) => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('#get', () => {
|
||||||
|
it('should return an Observable<DSpaceRESTV2Response>', () => {
|
||||||
|
const url = 'http://www.dspace.org/';
|
||||||
|
dSpaceRESTv2Service.get(url).subscribe((response) => {
|
||||||
|
expect(response).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = httpMock.expectOne(url);
|
||||||
|
expect(req.request.method).toBe('GET');
|
||||||
|
req.flush(mockDSpaceRESTV2Response);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@@ -1,8 +1,7 @@
|
|||||||
import { Inject, Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Http, RequestOptionsArgs } from '@angular/http';
|
import { HttpClient } from '@angular/common/http';
|
||||||
import { Observable } from 'rxjs/Observable';
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
|
||||||
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 { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
||||||
@@ -13,7 +12,7 @@ import { GLOBAL_CONFIG, GlobalConfig } from '../../../config';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class DSpaceRESTv2Service {
|
export class DSpaceRESTv2Service {
|
||||||
|
|
||||||
constructor(private http: Http, @Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig) {
|
constructor(private http: HttpClient) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,13 +22,12 @@ export class DSpaceRESTv2Service {
|
|||||||
* @param absoluteURL
|
* @param absoluteURL
|
||||||
* A URL
|
* A URL
|
||||||
* @param options
|
* @param options
|
||||||
* A RequestOptionsArgs object, with options for the http call.
|
* An object, with options for the http call.
|
||||||
* @return {Observable<string>}
|
* @return {Observable<DSpaceRESTV2Response>}
|
||||||
* An Observable<string> containing the response from the server
|
* An Observable<DSpaceRESTV2Response> containing the response from the server
|
||||||
*/
|
*/
|
||||||
get(absoluteURL: string, options?: RequestOptionsArgs): Observable<DSpaceRESTV2Response> {
|
get(absoluteURL: string, options?: {}): Observable<DSpaceRESTV2Response> {
|
||||||
return this.http.get(absoluteURL, options)
|
return this.http.get(absoluteURL, options)
|
||||||
.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