2020-07-21 15:19:27 +02:00
import { Injectable , Inject , PLATFORM_ID } from '@angular/core' ;
import { HttpInterceptor , HttpRequest , HttpHandler } from '@angular/common/http' ;
import { TransferState } from '@angular/platform-browser' ;
import { properties } from "../../environments/environment" ;
@Injectable ( {
providedIn : 'root'
} )
export class CacheInterceptorService implements HttpInterceptor {
cachingRequests = [
2020-07-24 13:21:22 +02:00
// Remove 2 following lines when bug with hidden refine filters is solved.
2020-11-02 18:11:31 +01:00
"/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=relfundinglevel0_id&fields=relproject&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright exact \"Open Access\"&type=results&page=0&size=0" ,
2020-07-24 13:21:22 +02:00
"/resources2/?format=json&refine=true&fields=funder&fields=fundinglevel0_id&fields=projectstartyear&fields=projectendyear&fields=projectecsc39&&type=projects&page=0&size=0" ,
2020-11-02 18:11:31 +01:00
"/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&type=results&page=0&size=0" ,
"/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=relfundinglevel0_id&fields=relproject&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright%20exact%20%22Open%20Access%22&fq=relfunder%20exact%20%22ec__________%3A%3AEC%7C%7CEuropean%20Commission%7C%7CEC%22&type=results&page=0&size=0" ,
2020-07-21 15:19:27 +02:00
2020-11-02 18:11:31 +01:00
"/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright%20exact%20%22Open%20Access%22&type=results&page=0&size=0" ,
"/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright exact \"Open Access\"&type=results&page=0&size=0" ,
2020-07-21 15:19:27 +02:00
"/resources2/?format=json&refine=true&fields=funder&fields=projectstartyear&fields=projectendyear&fields=projectecsc39&&type=projects&page=0&size=0" ,
"/resources2/?format=json&refine=true&fields=datasourcetypeuiname&fields=datasourceodlanguages&fields=datasourceodcontenttypes&fields=datasourcecompatibilityname&fields=country&fields=collectedfromname&&type=datasources&page=0&size=0" ,
"/resources2/?format=json&query= ( (reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or reldatasourcecompatibilityid exact openaire-cris_1.1 or reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*) ) &refine=true&fields=country&&type=organizations&page=0&size=0" ,
"/projects/?format=json&refine=true&page=1&size=0&fields=funder" ,
2021-03-30 13:50:15 +02:00
"/resources/?format=json&query=( oaftype exact datasource )&refine=true&fields=datasourcetypeuiname&fields=country&fields=datasourceodsubjects&fields=datasourceodcontenttypes&fields=datasourcecompatibilityname&&type=datasources&page=0&size=0" ,
"/search/v2/api/datasources/count?format=json" , "/search/v2/api/publications/count?format=json" , "/search/v2/api/datasets/count?format=json" , "/search/v2/api/software/count?format=json" , "/search/v2/api/other/count?format=json" ,
"relresulttype%3Dpublication"
2020-07-21 15:19:27 +02:00
] ;
constructor ( private transferState : TransferState , @Inject ( PLATFORM_ID ) private platformId : any ) { }
public intercept ( request : HttpRequest < any > , next : HttpHandler ) : any {
// console.debug(new Date());
if ( this . checkForCachedRequests ( request . url ) ) {
const dupReq = request . clone ( { url : properties.cacheUrl + encodeURIComponent ( request . url ) } ) ;
return next . handle ( dupReq ) ;
} else {
return next . handle ( request ) ;
}
}
public checkForCachedRequests ( url ) {
2021-03-30 13:50:15 +02:00
if ( url . indexOf ( "refine=true" ) !== - 1 || url . indexOf ( "/count?format=json" ) !== - 1 || url . indexOf ( "relresulttype%3Dpublication" ) !== - 1 ) {
2020-07-21 15:19:27 +02:00
return this . cachingRequests . some ( partUrl = > ( url . indexOf ( partUrl ) !== - 1 ) ) ;
}
return false ;
}
2020-11-02 18:11:31 +01:00
}