<commit_before>import couchOauthSign from "hospitalrun/utils/couch-oauth-sign";

export default function(configs) {
    function PouchOauthXHR(objParameters) {
        this.internalXHR = new XMLHttpRequest(objParameters);
        this.requestHeaders = {
        };
    }

    PouchOauthXHR.prototype = {
        _decodeParameters: function(param_string, current_params) {
            var return_params = current_params || {},
            params = decodeURIComponent(param_string).split('&'),
            param_parts,
            i;
            for (i=0;i<params.length;i++) {
                param_parts = params[i].split('=');
                return_params[param_parts[0]] =param_parts[1];            
            }
            return return_params;
        },

        abort: function() {
            this.internalXHR.abort();        
        },

        getAllResponseHeaders: function() {
            return this.internalXHR.getAllResponseHeaders();
        },

        getResponseHeader: function(header) {
            return this.internalXHR.getResponseHeader(header);
        },

        open: function(method, url, async, user, password) {
            this.method = method;
            this.url = url;
            if (async !== undefined) {
                this.async = async;
            } else {
                   this.async = true;
            }            
            this.user = user;
            this.password = password;
            if(url.indexOf("?") > 0) {
                var url_params = url.split("?");
                if (this.method === 'POST' || this.method === 'GET') {                
                    this.url = url_params[0];
                }
                this.params = this._decodeParameters(url_params[1]);            
            }
        },

        send: function(data) {
            if (this.signOauth !== undefined) {            
                this.params = this.signOauth(this.params);
                if (this.method === 'POST' || this.method === 'GET') {                
                    if (this.params !== undefined ) {                    
                        this.url = this.url + '?'+ decodeURIComponent($.param( this.params ));
                    }
                } else {
                    var oauth_header = 'OAuth realm=""';
                    for (var parameter in this.params) {
                        if (parameter.indexOf("oauth_") === 0) {                        
                            oauth_header += ', '+parameter+'="'+this.params[parameter]+'"';
                        }
                    }
                    this.requestHeaders['Authorization'] = oauth_header;
                }         
            }

            this.internalXHR.open(this.method, this.url, this.async, this.user, this.password);

            for (var header in this.requestHeaders) {
                this.internalXHR.setRequestHeader(header, this.requestHeaders[header]);
            }

            if (this.withCredentials !== undefined) {
                this.internalXHR.withCredentials = this.withCredentials;
            }
            if (this.responseType !== undefined) {
                this.internalXHR.responseType = this.responseType;
            } 

            if (this.timeout  !== undefined) {
                this.internalXHR.timeout = this.timeout;
            }
            if (this.ontimeout  !== undefined) {
                this.internalXHR.ontimeout = this.ontimeout;
            }

            if (this.upload  !== undefined) {
                this.internalXHR.upload = this.upload;
            }        

            if (this.onreadystatechange !== undefined) {
                var xhrwrapper = this;
                this.internalXHR.onreadystatechange = function() {
                    xhrwrapper.readyState = this.readyState;
                    xhrwrapper.response = this.response;
                    xhrwrapper.responseText = this.responseText;
                    xhrwrapper.responseType = this.responseType;
                    xhrwrapper.responseXML = this.responseXML;
                    xhrwrapper.status = this.status;
                    xhrwrapper.statusText = this.statusText;
                    xhrwrapper.onreadystatechange();
                };
            }
            this.internalXHR.send(data);
        },

        setRequestHeader: function(header, value) {
            this.requestHeaders[header] = value;
        },
        
        signOauth: function(params) {
            var signature_url = this.url,
                dblocation = this.url.indexOf('/db/');
            if (dblocation > -1) {            
                signature_url = 'http://localhost:5984/' + this.url.substring(dblocation+4);
            }

            params = couchOauthSign(signature_url, {
                    consumerKey: configs.config_consumer_key,
                    consumerSecret:  configs.config_consumer_secret,
                    token: configs.config_oauth_token,
                    tokenSecret: configs.config_token_secret,
                    type: this.method,
                    requestParams: params
            });
            return params;
        }
    };

    return PouchOauthXHR;
}<commit_msg>moved oauth configs to prototype.<commit_after>import couchOauthSign from "hospitalrun/utils/couch-oauth-sign";

export default function(configs) {
    function PouchOauthXHR(objParameters) {
        this.internalXHR = new XMLHttpRequest(objParameters);
        this.requestHeaders = {
        };
    }

    PouchOauthXHR.prototype = {
        _decodeParameters: function(param_string, current_params) {
            var return_params = current_params || {},
            params = decodeURIComponent(param_string).split('&'),
            param_parts,
            i;
            for (i=0;i<params.length;i++) {
                param_parts = params[i].split('=');
                return_params[param_parts[0]] =param_parts[1];            
            }
            return return_params;
        },

        abort: function() {
            this.internalXHR.abort();        
        },

        oauth: configs,

        getAllResponseHeaders: function() {
            return this.internalXHR.getAllResponseHeaders();
        },

        getResponseHeader: function(header) {
            return this.internalXHR.getResponseHeader(header);
        },

        open: function(method, url, async, user, password) {
            this.method = method;
            this.url = url;
            if (async !== undefined) {
                this.async = async;
            } else {
                   this.async = true;
            }            
            this.user = user;
            this.password = password;
            if(url.indexOf("?") > 0) {
                var url_params = url.split("?");
                if (this.method === 'POST' || this.method === 'GET') {                
                    this.url = url_params[0];
                }
                this.params = this._decodeParameters(url_params[1]);            
            }
        },

        send: function(data) {
            if (this.signOauth !== undefined) {            
                this.params = this.signOauth(this.params);
                if (this.method === 'POST' || this.method === 'GET') {                
                    if (this.params !== undefined ) {                    
                        this.url = this.url + '?'+ decodeURIComponent($.param( this.params ));
                    }
                } else {
                    var oauth_header = 'OAuth realm=""';
                    for (var parameter in this.params) {
                        if (parameter.indexOf("oauth_") === 0) {                        
                            oauth_header += ', '+parameter+'="'+this.params[parameter]+'"';
                        }
                    }
                    this.requestHeaders['Authorization'] = oauth_header;
                }         
            }

            this.internalXHR.open(this.method, this.url, this.async, this.user, this.password);

            for (var header in this.requestHeaders) {
                this.internalXHR.setRequestHeader(header, this.requestHeaders[header]);
            }

            if (this.withCredentials !== undefined) {
                this.internalXHR.withCredentials = this.withCredentials;
            }
            if (this.responseType !== undefined) {
                this.internalXHR.responseType = this.responseType;
            } 

            if (this.timeout  !== undefined) {
                this.internalXHR.timeout = this.timeout;
            }
            if (this.ontimeout  !== undefined) {
                this.internalXHR.ontimeout = this.ontimeout;
            }

            if (this.upload  !== undefined) {
                this.internalXHR.upload = this.upload;
            }        

            if (this.onreadystatechange !== undefined) {
                var xhrwrapper = this;
                this.internalXHR.onreadystatechange = function() {
                    xhrwrapper.readyState = this.readyState;
                    xhrwrapper.response = this.response;
                    xhrwrapper.responseText = this.responseText;
                    xhrwrapper.responseType = this.responseType;
                    xhrwrapper.responseXML = this.responseXML;
                    xhrwrapper.status = this.status;
                    xhrwrapper.statusText = this.statusText;
                    xhrwrapper.onreadystatechange();
                };
            }
            this.internalXHR.send(data);
        },

        setRequestHeader: function(header, value) {
            this.requestHeaders[header] = value;
        },
        
        signOauth: function(params) {
            var signature_url = this.url,
                dblocation = this.url.indexOf('/db/');
            if (dblocation > -1) {            
                signature_url = 'http://localhost:5984/' + this.url.substring(dblocation+4);
            }

            params = couchOauthSign(signature_url, {
                    consumerKey: this.oauth.config_consumer_key,
                    consumerSecret:  this.oauth.config_consumer_secret,
                    token: this.oauth.config_oauth_token,
                    tokenSecret: this.oauth.config_token_secret,
                    type: this.method,
                    requestParams: params
            });
            return params;
        }
    };

    return PouchOauthXHR;
}
{'fraction_non_alphanumeric': 0.10525813531825556, 'fraction_numerical': 0.002481152781753984, 'mean_word_length': 1.3325172490540842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2460841', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2584, 'n_words': 430}