import { Injectable } from '@angular/core' ; import { ApiCallService , ApiService } from '../services/api-call.service' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { HttpHeaders } from '@angular/common/http' ; import { ImporterConfig } from './import-objects/import-object.models' ; declare type HttpObserve = 'body' | 'events' | 'response' ; export const resp = 'response' ; export const httpImportFileOptions = { headers : new HttpHeaders ( { } ) , params : { } , observe : resp } ; @ Injectable ( { providedIn : 'root' } ) export class ImportService implements ApiService { public servicePrefix : string = 'import' ; private objectPrefix : string = 'object' ; private typePrefix : string = 'type' ; constructor ( private api ) { } public importObjects ( file , fileFormat , parserConfig , importerConfig ) { const formData = new FormData ( ) ; formData . append ( 'file' , file , file . name ) ; formData . append ( 'file_format' , fileFormat ) ; formData . append ( 'parser_config' , JSON . stringify ( parserConfig ) ) ; formData . append ( 'importer_config' , JSON . stringify ( importerConfig ) ) ; return this . api . callPost < any > ( `${ this . servicePrefix }/${ this . objectPrefix }/` , formData , httpImportFileOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public postObjectParser ( file , fileFormat , parserConfig ) { const formData = new FormData ( ) ; formData . append ( 'file' , file , file . name ) ; formData . append ( 'file_format' , fileFormat ) ; formData . append ( 'parser_config' , JSON . stringify ( parserConfig ) ) ; return this . api . callPost < any > ( `${ this . servicePrefix }/${ this . objectPrefix }/parse/` , formData , httpImportFileOptions ) . pipe ( map ( ( apiResponse ) => { console . log ( apiResponse ) ; return apiResponse . body ; } ) ) ; } public getObjectImporterDefaultConfig ( fileType ) { return this . api . callGet < any > ( `${ this . servicePrefix }/${ this . objectPrefix }/importer/config/${ fileType }/` ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return { } ; } return apiResponse . body ; } ) ) ; } public getObjectParserDefaultConfig ( fileType ) { return this . api . callGet < any > ( `${ this . servicePrefix }/${ this . objectPrefix }/parser/default/${ fileType }/` ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return { } ; } return apiResponse . body ; } ) ) ; } public getObjectImporters ( ) { return this . api . callGet < string [ ] > ( `${ this . servicePrefix }/${ this . objectPrefix }/importer/` ) . pipe ( map ( ( apiResponse ) => { if ( apiResponse . status === 204 ) { return [ ] ; } return apiResponse . body ; } ) ) ; } public postUpdateTypeParser ( formData ) { return this . api . callPost < any > ( `${ this . servicePrefix }/${ this . typePrefix }/update/` , formData , httpImportFileOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } public postCreateTypeParser ( formData ) { return this . api . callPost < any > ( `${ this . servicePrefix }/${ this . typePrefix }/create/` , formData , httpImportFileOptions ) . pipe ( map ( ( apiResponse ) => { return apiResponse . body ; } ) ) ; } }