import * as httpm from 'typed-rest-client/HttpClient' ; import * as fs from 'fs' ; import * as path from 'path' ; import * as cm from './common' ; let sampleFilePath = path . join ( process . cwd ( ) , 'httpClientStreamSample.txt' ) ; let httpc = new httpm . HttpClient ( 'vsts-node-api' ) ; export async function run ( ) { try { cm . banner ( 'Http Samples' ) ; cm . heading ( 'get request output body' ) ; let res = await httpc . get ( 'https://httpbin.org/get' ) ; let body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; cm . heading ( 'get request in a single line' ) ; body = await ( await httpc . get ( 'https://httpbin.org/get' ) ) . readBody ( ) ; cm . outputHttpBinResponse ( body ) ; cm . heading ( 'get request using the proxy url set in the env variables' ) ; const proxySettings = { proxyUrl : cm . getEnv ( 'PROXY_URL' ) , proxyUsername : cm . getEnv ( 'PROXY_USERNAME' ) , proxyPassword : cm . getEnv ( 'PROXY_PASSWORD' ) , proxyBypassHosts : cm . getEnv ( 'PROXY_BYPASS_HOSTS' ) ? cm . getEnv ( 'PROXY_BYPASS_HOSTS' ) . split ( ', ' ) : null } ; if ( proxySettings . proxyUrl ) { httpc = new httpm . HttpClient ( 'vsts-node-api' , null , { proxy : proxySettings } ) ; body = await ( await httpc . get ( 'https://httpbin.org/get' ) ) . readBody ( ) ; cm . outputHttpBinResponse ( body ) ; } else { console . log ( "No proxy url set. To set a proxy url, set the PROXY_URL env variable (e.g. set PROXY_URL=proxy.com)" ) ; } cm . heading ( 'get request disabling redirects' ) ; httpc = new httpm . HttpClient ( 'vsts-node-api' , null , { allowRedirects : false } ) ; res = await httpc . get ( "http://httpbin.org/redirect-to?url=" + encodeURIComponent ( "http://httpbin.org/get" ) ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; httpc = new httpm . HttpClient ( 'vsts-node-api' ) ; cm . heading ( 'get request with implicitly allowed redirects' ) ; res = await httpc . get ( "http://httpbin.org/redirect-to?url=" + encodeURIComponent ( "http://httpbin.org/get" ) ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; cm . heading ( 'get request and pipe stream' ) ; let file = fs . createWriteStream ( sampleFilePath ) ; ( await httpc . get ( 'https://httpbin.org/get' ) ) . message . pipe ( file ) ; body = fs . readFileSync ( sampleFilePath ) . toString ( ) ; cm . outputHttpBinResponse ( body ) ; cm . heading ( 'head request' ) ; res = await httpc . head ( 'https://httpbin.org/get' ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; cm . heading ( 'delete request' ) ; res = await httpc . del ( 'https://httpbin.org/delete' ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; let b = 'Hello World!' ; cm . heading ( 'post request' ) ; res = await httpc . post ( 'https://httpbin.org/post' , b ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; cm . heading ( 'patch request' ) ; res = await httpc . patch ( 'https://httpbin.org/patch' , b ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; cm . heading ( 'options request' ) ; res = await httpc . options ( 'https://httpbin.org' ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; cm . heading ( 'get not found' ) ; res = await httpc . get ( 'https://httpbin.org/status/404' ) ; body = await res . readBody ( ) ; cm . outputHttpBinResponse ( body , res . message ) ; } catch ( err ) { console . error ( 'Failed: ' + err . message ) ; } }