Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: custom headers #48

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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