var childproc = require('child_process');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const querystring = require('querystring');
var settings=require("../../config/settings");
class RestClient{
    constructor(){
      this.cmdDownLoadFilePattern="curl -G -o {fileName} {url}";
    }
    
    FetchDownLoadCmd(outfname,url) {
  		// console.log(this.cmdPattern);
  		var cmd = this.cmdDownLoadFilePattern.replace(/\{fileName\}/g, outfname).replace(
  				/\{url\}/g, url);
  		return cmd;
    }
    async exec(cmd) {
      //await后面表达式返回的promise对象，是then的语法糖，await返回then函数的返回值
      //异常需要try/catch自己捕获或外部catch捕获
      const { stdout, stderr } = await exec(cmd,{
        maxBuffer:1024*1024*15
      });
      return { stdout, stderr };
  }
    async execDownload(url,outfname){
      let cmd=this.FetchDownLoadCmd(outfname,url);
      var result=await this.exec(cmd);
      return result;
    }
}
module.exports=RestClient;
// var x=new RestClient();
// x.execGet("","http://www.163.com").then(function(r){
//   console.log(r.stdout);
//   console.log(r.stderr);
// });
