UNPKG

aliyun-sdk

Version:
91 lines (72 loc) 2.32 kB
var ALY = require('./core'); var inherit = ALY.util.inherit; ALY.Endpoint = inherit({ constructor: function Endpoint(endpoint) { ALY.util.hideProperties(this, ['slashes', 'auth', 'hash', 'search', 'query']); if (typeof endpoint === 'undefined' || endpoint === null) { throw new Error('Invalid endpoint: ' + endpoint); } if (!endpoint.match(/^http/)) { throw new Error('错误的 endpoint 格式, 需要以 http 或者 https 开头'); } ALY.util.update(this, ALY.util.urlParse(endpoint)); // Ensure the port property is set as an integer if (this.port) { this.port = parseInt(this.port, 10); } else { this.port = this.protocol === 'https:' ? 443 : 80; } } }); ALY.HttpRequest = inherit({ constructor: function HttpRequest(endpoint, region) { this.method = 'POST'; this.path = endpoint.path || '/'; this.headers = {}; this.body = ''; this.endpoint = endpoint; this.region = region; this.setUserAgent(); }, setUserAgent: function setUserAgent() { //var prefix = ALY.util.isBrowser() ? 'X-Aly-' : ''; //this.headers[prefix + 'User-Agent'] = ALY.util.userAgent(); //this.headers['x-sdk-client'] = this.headers['User-Agent'] = ALY.util.userAgent(); // pop 现在不支持 x-sdk-client 在浏览器设置 this.headers['User-Agent'] = ALY.util.userAgent(); }, pathname: function pathname() { return this.path.split('?', 1)[0]; }, search: function search() { return this.path.split('?', 2)[1] || ''; }, debug: function () { if(process.env.DEBUG == 'aliyun') { console.log('-------- HttpRequest Start: --------'); console.log('method:', this.method); console.log('path:', this.path); console.log('headers:'); for(var i in this.headers) { if (i == 'constructor') continue; console.log(i, ':', this.headers[i]); }; } } }); ALY.HttpResponse = inherit({ constructor: function HttpResponse() { this.statusCode = undefined; this.headers = {}; this.body = undefined; } }); ALY.HttpClient = inherit({}); ALY.HttpClient.getInstance = function getInstance() { /*jshint newcap:false */ if (this.singleton === undefined) { this.singleton = new this(); } return this.singleton; };