Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if (typeof Qt != "undefined") {
- Qt.include("common.js");
- Qt.include("http.js");
- Qt.include("eventemitter.js");
- var md5 = Qt.md5;
- } else if (typeof md5 != "function") {
- function md5(str) {
- console.error("MD5 encoding not supported!");
- }
- }
- var API_HOST = "api.vk.com";
- var API_PATH = "/method/";
- var API_DELAY = 334;
- function Api(options) {
- options = options || {};
- this.accessToken = options.accessToken;
- this.version = options.version;
- this.delay = options.delay || API_DELAY;
- }
- Api.prototype = {
- request: null,
- _processing: false,
- _requestQueue: [],
- _requestTime: 0,
- // public
- call: function(method, params, options) {
- var request = new ApiRequest(this, method, params, options);
- if (this._processing) {
- //
- }
- if (this._locked) {
- console.log("Add request to queue");
- this._requestQueue.push(request);
- } else {
- // Запоминает запрос, т.к. он может еще понадобиться
- this.request = request;
- request.send();
- }
- },
- execute: function(code, options) {
- this.call('execute', {code: code}, options);
- },
- cancelAllRequests: function() {
- console.log("Cancel all requests");
- this._requestQueue = [];
- this._unlock();
- },
- _processRequestQueue: function() {
- var request = this._requestQueue.shift();
- if (request) {
- request.send();
- } else {
- console.log("Requests queue empty");
- this._unlock();
- }
- },
- _lock: function() {
- this._locked = true;
- },
- _unlock: function() {
- this._locked = false;
- },
- };
- EventEmitter.mixin(Api);
- function ApiRequest(api, method, params, options) {
- this.api = api;
- this.method = method;
- this.params = extend({}, params);
- this.done = options.done;
- this.fail = options.fail;
- this.context = options.context;
- this.delay = options.delay || this.api.delay;
- }
- ApiRequest.prototype = {
- _processing: true,
- send: function() {
- console.log("Send request");
- this.api._lock();
- var params = extend({}, this.api.extraParams, this.params);
- if (this.api.version) {
- params.v = this.api.version;
- }
- var scheme = "https";
- if (this.api.accessToken) {
- params.access_token = this.api.accessToken.accessToken;
- if (this.api.accessToken.secret) {
- // https://vk.com/dev/api_nohttps
- scheme = "http";
- if (params.sig) {
- delete params.sig;
- }
- var str = formatStr(
- "{0}{1}?{2}{3}",
- API_PATH,
- this.method,
- encodeQueryParams(params),
- this.api.accessToken.secret
- );
- params.sig = md5(str);
- }
- }
- var format = "{0}://{1}{2}{3}";
- var endpoint = formatStr(format, scheme, API_HOST, API_PATH, this.method);
- var nextRequestTime = this.api._requestTime + this.delay;
- var now = Date.now();
- var timeout = nextRequestTime > now ? nextRequestTime - now : 0;
- console.log("timeout = %sms", timeout);
- var self = this;
- setTimeout(function() {
- post(endpoint, params, function(data) {
- self.api._requestTime = Date.now();
- var error = data.error;
- var response = data.response;
- if (error) {
- console.error(self._formatError(error));
- self.api.emit('error', error);
- }
- if (self._processing) {
- if (error && typeof self.fail == "function") {
- self.fail.call(self.context, error);
- }
- if (response && typeof self.done == "function") {
- self.done.call(self.context, response);
- }
- self.next();
- }
- });
- }, timeout);
- },
- // Переходит к следующему запросу в очереди
- next: function() {
- this.api._processRequestQueue();
- },
- // Останавливает обработку всех запросов
- stopProcessing: function() {
- console.log("Stop all requests processing");
- this._processing = false;
- },
- // Повторить запрос через delay миллисекунд
- retry: function(delay) {
- this.processing = true;
- if (delay != null) {
- this.delay = delay;
- }
- this.send();
- },
- _formatError: function(error) {
- var params = {};
- for (var i = 0; i < error.request_params.length; ++i) {
- params[error.request_params[i].key] = error.request_params[i].value;
- }
- var method = params.method;
- delete params.method;
- return template(
- 'An error occurred while calling method "{method}"' +
- ' with parameters: {params}' +
- ' (Error Code: {code}, Message: {msg})',
- {
- code: error.error_code,
- msg: error.error_msg,
- method: method,
- params: this._formatParams(params)
- }
- );
- },
- _formatParams: function(params) {
- return JSON.stringify(params, function(k, v) {
- if (k == "access_token") {
- return "**CENSORED**";
- }
- return v;
- });
- },
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement