import { CreateRepertoireRequest , CreateRepertoireResponse , DeleteRepertoireRequest , DeleteRepertoireResponse , GetPreferenceRequest , GetPreferenceResponse , LoadCumulatedStatisticsRequest , LoadCumulatedStatisticsResponse , LoadRepertoireRequest , LoadRepertoireResponse , MetadataRequest , MetadataResponse , PrivelegedCopyRequest , PrivelegedCopyResponse , RecordStatisticsRequest , RecordStatisticsResponse , SetPreferenceRequest , SetPreferenceResponse , UpdateRepertoireRequest , UpdateRepertoireResponse } from '../../../protocol/actions' ; import { Preference } from '../../../protocol/preference/preference' ; import { CumulatedStatistic } from '../../../protocol/statistic/cumulatedstatistic' ; import { Statistic } from '../../../protocol/statistic/statistic' ; import { Metadata , Repertoire } from '../../../protocol/storage' ; import { Toasts } from '../common/toasts' ; import { ServerWrapper } from './serverwrapper' ; export class AccessTokenServerWrapper implements ServerWrapper { private accessToken_ : string ; constructor ( accessToken ) { this . accessToken_ = accessToken ; } getAllRepertoireMetadata ( ) { return this . post_ < MetadataRequest , MetadataResponse > ( '/metadata' , { } ) . then ( r => r . metadataList ) ; } loadRepertoire ( repertoireId ) { return this . post_ < LoadRepertoireRequest , LoadRepertoireResponse > ( '/loadrepertoire' , { repertoireId } ) . then ( r => r . repertoire ) ; } updateRepertoire ( repertoireId , repertoire ) { return this . post_ < UpdateRepertoireRequest , UpdateRepertoireResponse > ( '/updaterepertoire' , { repertoireId , repertoire } ) . then ( ( ) => { } ) ; } createRepertoire ( ) { return this . post_ < CreateRepertoireRequest , CreateRepertoireResponse > ( '/createrepertoire' , { } ) . then ( r => r . newRepertoireId ) ; } deleteRepertoire ( repertoireId ) { return this . post_ < DeleteRepertoireRequest , DeleteRepertoireResponse > ( '/deleterepertoire' , { repertoireId } ) . then ( ( ) => { } ) ; } setPreference ( preference ) { return this . post_ < SetPreferenceRequest , SetPreferenceResponse > ( '/setpreference' , { preference } ) . then ( ( ) => { } ) ; } getPreference ( ) { return this . post_ < GetPreferenceRequest , GetPreferenceResponse > ( '/getpreference' , { } ) . then ( r => r . preference ) ; } recordStatistics ( statisticList ) { return this . post_ < RecordStatisticsRequest , RecordStatisticsResponse > ( '/recordstatistics' , { statisticList } ) . then ( ( ) => { } ) ; } loadCumulatedStatistics ( repertoireId ) { return this . post_ < LoadCumulatedStatisticsRequest , LoadCumulatedStatisticsResponse > ( '/loadcumulatedstatistics' , { repertoireId } ) . then ( r => r . cumulatedStatisticList ) ; } copyRepertoireAsPrivelegedUser ( repertoireId ) { return this . post_ < PrivelegedCopyRequest , PrivelegedCopyResponse > ( '/privelegedcopy' , { repertoireId } ) . then ( ( ) => { } ) ; } private post_ < REQUEST , RESPONSE > ( endpoint , body ) { const options = { method : 'POST' , headers : { 'Content-Type' : 'application/json' , 'Authorization' : 'Bearer ' + this . accessToken_ } , body : JSON . stringify ( body ) } ; return fetch ( endpoint , options ) . then ( res => { if ( res . status != 200 ) { this . showAuthError_ ( ) ; throw new Error ( 'Server returned status ' + res . status + '.' ) ; } return ( res . json ( ) as unknown ) as RESPONSE ; } ) . catch ( err => { this . showAuthError_ ( ) ; throw err ; } ) ; } private showAuthError_ ( ) { Toasts . error ( 'Something went wrong.' , 'There was a problem reaching the server. Please refresh the page and ' + 'try again.' ) ; } }