'use strict' ; import * as request from 'request' ; import * as fs from 'fs' ; export function getJson ( url ) { return new Promise < any > ( ( resolve , reject ) => { request . get ( url , { json : true } , ( error , httpResponse , body ) => { if ( error ) { reject ( error ) ; } else { if ( httpResponse && httpResponse . statusCode !== 200 ) { reject ( "HTTP status code " + httpResponse . statusCode ) ; } else { resolve ( body ) ; } } } ) ; } ) ; } export function getFile ( url , localFilePath ) { const localFile = fs . createWriteStream ( localFilePath ) ; return new Promise < void > ( ( resolve , reject ) => { request . get ( url ) . on ( 'response' , function ( response ) { console . log ( "Downloading %s. Content-type: %s, Status code: %d" , url , response . headers [ 'content-type' ] , response . statusCode ) ; if ( response . statusCode >= 400 ) { reject ( new Error ( "Downloading VAL binaries failed with HTTP status code " + response . statusCode ) ) ; } } ) . on ( 'error' , function ( err ) { reject ( err ) ; } ) . pipe ( localFile ) . on ( "close" , ( ) => resolve ( ) ) ; } ) ; } export function getText ( url ) { return new Promise < string > ( ( resolve , reject ) => { request . get ( url , ( error , httpResponse , body ) => { if ( error ) { reject ( error ) ; } else { if ( httpResponse && httpResponse . statusCode !== 200 ) { reject ( "HTTP status code " + httpResponse . statusCode ) ; } else { resolve ( body ) ; } } } ) ; } ) ; } export function postJson ( url , content ) { return new Promise < string > ( ( resolve , reject ) => { request . post ( url , { body : content , json : true } , ( error , httpResponse , body ) => { if ( error ) { reject ( error ) ; } else { if ( httpResponse && httpResponse . statusCode > 204 ) { reject ( "HTTP status code " + httpResponse . statusCode ) ; } else { resolve ( body ) ; } } } ) ; } ) ; }