Skip to content

Commit

Permalink
api: custom headers
Browse files Browse the repository at this point in the history
* Adds a config to specify custom headers to api calls.

Co-Authored-by: Sébastion Délèze <sebastien-deleze@rero.ch>
  • Loading branch information
Sébastien Délèze and Sébastion Délèze committed Nov 15, 2019
1 parent e706d6c commit 2c878c4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
8 changes: 7 additions & 1 deletion projects/ng-core-tester/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,13 @@ const routes: Routes = [
canUpdate,
canDelete,
canRead,
aggregations
aggregations,
listHeaders: {
'Content-Type': 'application/rero+json'
},
itemHeaders: {
'Content-Type': 'application/rero+json'
},
},
{
key: 'institutions',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ export class DetailComponent implements OnInit {
const pid = this.route.snapshot.paramMap.get('pid');
const type = this.route.snapshot.paramMap.get('type');

this.record$ = this.recordService.getRecord(type, pid, 1);
const config = this.recordUiService.getResourceConfig(this.route.snapshot.data.types, type);

this.record$ = this.recordService.getRecord(type, pid, 1, config.itemHeaders || null);
this.record$.subscribe(
(record) => {
this.record = record;

const config = this.recordUiService.getResourceConfig(this.route.snapshot.data.types, type);

if (config.canDelete) {
config.canDelete(this.record).subscribe((result: ActionStatus) => {
this.deleteStatus = result;
Expand Down
24 changes: 19 additions & 5 deletions projects/rero/ng-core/src/lib/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpErrorResponse } from '@angular/common/http';
import { HttpClient, HttpParams, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, throwError, of } from 'rxjs';
import { catchError, map, debounceTime } from 'rxjs/operators';

Expand Down Expand Up @@ -52,7 +52,8 @@ export class RecordService {
page = 1,
itemsPerPage = RecordService.DEFAULT_REST_RESULTS_SIZE,
aggFilters: any[] = [],
preFilters: object = {}
preFilters: object = {},
headers: any = null
): Observable<Record> {
// Build query string
let httpParams = new HttpParams().set('q', query);
Expand All @@ -68,7 +69,10 @@ export class RecordService {
httpParams = httpParams.append(key, preFilters[key]);
}

return this.http.get<Record>(this.apiService.getEndpointByType(type, true) + '/', { params: httpParams })
return this.http.get<Record>(this.apiService.getEndpointByType(type, true) + '/', {
params: httpParams,
headers: this.createRequestHeaders(headers)
})
.pipe(
catchError(this.handleError)
);
Expand All @@ -91,8 +95,10 @@ export class RecordService {
* @param type - string, type of resource
* @param pid - string, record PID
*/
public getRecord(type: string, pid: string, resolve = 0): Observable<any> {
return this.http.get<Record>(`${this.apiService.getEndpointByType(type, true)}/${pid}?resolve=${resolve}`)
public getRecord(type: string, pid: string, resolve = 0, headers: any = {}): Observable<any> {
return this.http.get<Record>(`${this.apiService.getEndpointByType(type, true)}/${pid}?resolve=${resolve}`, {
headers: this.createRequestHeaders(headers)
})
.pipe(
catchError(this.handleError)
);
Expand Down Expand Up @@ -176,4 +182,12 @@ export class RecordService {
return throwError(
'Something bad happened; please try again later.');
}

/**
* Creates and returns a HttpHeader object to send to request.
* @param headers Object containing http headers to send to request.
*/
private createRequestHeaders(headers: any = {}) {
return headers ? new HttpHeaders(headers) : new HttpHeaders({ 'Content-Type': 'application/json' });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export class RecordSearchComponent implements OnInit {
canUpdate?: any,
canDelete?: any,
canRead?: any,
aggregations?: any
aggregations?: any,
listHeaders?: any,
itemHeaders?: any
}[] = [{ key: 'documents', label: 'Documents' }];

/**
Expand Down Expand Up @@ -230,7 +232,10 @@ export class RecordSearchComponent implements OnInit {
this.getRecords(this.inRouting === false);

for (const type of this.types) {
this.recordService.getRecords(type.key, '', 1, 1, [], this.config.preFilters || {}).subscribe(records => {
this.recordService.getRecords(
type.key, '', 1, 1, [],
this.config.preFilters || {},
this.config.listHeaders || null).subscribe(records => {
type.total = records.hits.total;
});
}
Expand Down Expand Up @@ -397,7 +402,8 @@ export class RecordSearchComponent implements OnInit {
this.page,
this.size,
this.aggFilters,
this.config.preFilters || {}
this.config.preFilters || {},
this.config.listHeaders || null
).subscribe(
records => {
this.records = records.hits.hits;
Expand Down

0 comments on commit 2c878c4

Please sign in to comment.