import path from "path" ; import { app } from "electron" ; import electronIsDev from "electron-is-dev" ; import apigClientFactory from "aws-api-gateway-client" ; import { getProfileCredentials } from "./electron-get-profile-credentials" ; import { PutLogDataRequest } from "../models/putLogDataRequest" ; import { PutMetricDataRequest } from "../models/putMetricDataRequest" ; import { localStore } from "../preload-localStore" ; export const config = require ( electronIsDev ? path . join ( __dirname , "../.." , "build-scripts" , "porting-assistant-config.dev.json" ) : path . join ( path . dirname ( app . getPath ( "exe" ) ) , "resources" , "config" , "porting-assistant-config.json" ) ) ; const createClient = ( profileName , region , invokeUrl ) => { let credentials = getProfileCredentials ( profileName ) ; const awsAccessKeyId = credentials ?. accessKeyId ; const awsSecretAccessKey = credentials ?. secretAccessKey ; const awsSessionToken = credentials ?. sessionToken ; if ( awsAccessKeyId === undefined || awsSecretAccessKey === undefined ) { console . error ( `Credentials are undefined for profile: ${ profileName }` ) ; return null ; } const apigClient = apigClientFactory . newClient ( { invokeUrl : invokeUrl , region : region , accessKey : awsAccessKeyId , secretKey : awsSecretAccessKey , sessionToken : awsSessionToken , } ) ; return apigClient ; } ; const createRequest = async ( pathParams , pathTemplate , method , additionalParams , body ) : Promise < boolean > => { var apigClient = createClient ( localStore . get ( "profile" ) , config . PortingAssistantMetrics . Region , config . PortingAssistantMetrics . InvokeUrl ) ; var metricsEnabled = localStore . get ( "share" ) ; if ( metricsEnabled && apigClient != null ) { try { await apigClient . invokeApi ( pathParams , pathTemplate , method , additionalParams , body ) ; return true ; } catch ( e ) { console . error ( e ) ; return false ; } } else { return true ; } } ; export const putLogData = async ( logName , logData ) => { const pathParams = { } ; const pathTemplate = "/put-log-data" ; const method = "POST" ; const additionalParams = { } ; const body = { requestMetadata : { version : "1.0" , service : config . PortingAssistantMetrics . ServiceName , token : "12345678" , description : "Porting Assistant for .NET Log" , } , log : { timeStamp : new Date ( ) . toLocaleString ( ) , logName : logName , logData : logData , } , } ; return await createRequest ( pathParams , pathTemplate , method , additionalParams , body ) ; } ; export const putMetricData = async ( namespace , metricName , unit , value , dimensions ) => { const pathParams = { } ; const pathTemplate = "/put-metric-data" ; const method = "POST" ; const additionalParams = { } ; const body = { requestMetadata : { version : "1.0" , service : config . PortingAssistantMetrics . ServiceName , token : "12345678" , description : "Porting Assistant for .NET Log" , created : new Date ( ) . toDateString ( ) , namespace : namespace , } , metrics : [ { metricName : metricName , unit : unit , value : value , timeStamp : new Date ( ) . toISOString ( ) , dimensions : dimensions , } , ] , } ; return await createRequest ( pathParams , pathTemplate , method , additionalParams , body ) ; } ; export const testProfile = async ( profile ) => { var apigClient = createClient ( profile , config . PortingAssistantMetrics . Region , config . PortingAssistantMetrics . InvokeUrl ) ; if ( apigClient == null ) { return false ; } try { const response = await apigClient . invokeApi ( { } , "/put-log-data" , "POST" , { } , { requestMetadata : { version : "1.0" , service : config . PortingAssistantMetrics . ServiceName , token : "12345678" , description : "PortingAssistant Log" , } , log : { timeStamp : new Date ( ) . toLocaleString ( ) , logName : "verify-user" , logData : "" , } , } ) ; if ( response . status === 200 ) { return true ; } return false ; } catch ( ex ) { console . error ( ex ) ; return false ; } } ;