## Methods

### _headers

##### splunkjs.Context._headers

Appends Splunk-specific headers.

#### Syntax

_headers: function (headers)

#### Parameters

Name | Type | Description
--- | --- | ---
headers | Object | A dictionary of headers (optional).

#### Return

Object.

An augmented dictionary of headers.

#### Source (lib/context.js:137)

    _headers: function (headers) {
        headers = headers || {};
        headers["Authorization"] = this.authorization + " " + this.sessionKey;
        return headers;
    },

### del

##### splunkjs.Context.del

Performs a DELETE request.

#### Syntax

del: function(path, params, callback)

#### Parameters

Name | Type | Description
--- | --- | ---
path | String | The REST endpoint path of the DELETE request.
params | Object | The entity-specific parameters for this request.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/context.js:357)

    del: function(path, params, callback) {
        var that = this;
        var request = function(callback) {
            return that.http.del(
                that.urlify(path),
                that._headers(),
                params,
                0,
                callback
            );
        };
        return this._requestWrapper(request, callback);
    },

### fullpath

##### splunkjs.Context.fullpath

Converts a partial path to a fully-qualified path to a REST endpoint, and if necessary includes the namespace owner and app.

#### Syntax

fullpath: function(path, namespace)

#### Parameters

Name | Type | Description
--- | --- | ---
path | String | The partial path.
namespace | String | The namespace, in the format "owner/app".

#### Return

String.

The fully-qualified path.

#### Source (lib/context.js:242)

    fullpath: function(path, namespace) {
        namespace = namespace || {};
        if (utils.startsWith(path, "/")) {
            return path;
        }
        // If we don't have an app name (explicitly or implicitly), we default to /services/
        if (!namespace.app && !this.app && namespace.sharing !== root.Sharing.SYSTEM) {
            return "/services/" + path;
        }
        // Get the app and owner, first from the passed in namespace, then the service,
        // finally defaulting to wild cards
        var owner = namespace.owner || this.owner || "-";
        var app   = namespace.app || this.app || "-";
        namespace.sharing = (namespace.sharing || "").toLowerCase();
        // Modify the owner and app appropriately based on the sharing parameter
        if (namespace.sharing === root.Sharing.APP || namespace.sharing === root.Sharing.GLOBAL) {
            owner = "nobody";
        }
        else if (namespace.sharing === root.Sharing.SYSTEM) {
            owner = "nobody";
            app = "system";
        }
        return utils.trim("/servicesNS/" + owner + "/" + app + "/" + path);
    },

### get

##### splunkjs.Context.get

Performs a GET request.

#### Syntax

get: function(path, params, callback)

#### Parameters

Name | Type | Description
--- | --- | ---
path | String | The REST endpoint path of the GET request.
params | Object | The entity-specific parameters for this request.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/context.js:333)

    get: function(path, params, callback) {
        var that = this;
        var request = function(callback) {
            return that.http.get(
                that.urlify(path),
                that._headers(),
                params,
                0,
                callback
            );
        };
        return this._requestWrapper(request, callback);
    },

### init

##### splunkjs.Context.init

Constructor for `splunkjs.Context`.

#### Syntax

init: function(http, params)

#### Parameters

Name | Type | Description
--- | --- | ---
http | splunkjs.Http | An instance of a `splunkjs.Http` class.
params | Object | A dictionary of optional parameters: - `scheme` (string): The scheme ("http" or "https") for accessing Splunk. - `host` (string): The host name (the default is "localhost"). - `port` (integer): The port number (the default is 8089). - `username` (string): The Splunk account username, which is used to authenticate the Splunk instance. - `password` (string): The password, which is used to authenticate the Splunk instance. - `owner` (string): The owner (username) component of the namespace. - `app` (string): The app component of the namespace. - `sessionKey` (string): The current session token. - `autologin` (boolean): `true` to automatically try to log in again if the session terminates, `false` if not (`true` by default). - `version` (string): The version string for Splunk, for example "4.3.2" (the default is "5.0").

#### Return

splunkjs.Context.

A new `splunkjs.Context` instance.

#### Source (lib/context.js:62)

    init: function(http, params) {
        if (!(http instanceof Http) && !params) {
            // Move over the params
            params = http;
            http = null;
        }
        params = params || {};
        this.scheme        = params.scheme || "https";
        this.host          = params.host || "localhost";
        this.port          = params.port || 8089;
        this.username      = params.username || null;
        this.password      = params.password || null;
        this.owner         = params.owner;
        this.app           = params.app;
        this.sessionKey    = params.sessionKey || "";
        this.authorization = params.authorization || "Splunk";
        this.paths         = params.paths || Paths;
        this.version       = params.version || "default";
        this.autologin     = true;
        // Initialize autologin
        // The reason we explicitly check to see if 'autologin'
        // is actually set is because we need to distinguish the
        // case of it being set to 'false', and it not being set.
        // Unfortunately, in JavaScript, these are both false-y
        if (params.hasOwnProperty("autologin")) {
            this.autologin = params.autologin;
        }
        if (!http) {
            // If there is no HTTP implementation set, we check what platform
            // we're running on. If we're running in the browser, then complain,
            // else, we instantiate NodeHttp.
            if (typeof(window) !== 'undefined') {
                throw new Error("Http instance required when creating a Context within a browser.");
            }
            else {
                var NodeHttp = require('./platform/node/node_http').NodeHttp;
                http = new NodeHttp();
            }
        }
        // Store the HTTP implementation
        this.http = http;
        this.http._setSplunkVersion(this.version);
        // Store our full prefix, which is just combining together
        // the scheme with the host
        var versionPrefix = utils.getWithVersion(this.version, prefixMap);
        this.prefix = this.scheme + "://" + this.host + ":" + this.port + versionPrefix;
        // We perform the bindings so that every function works
        // properly when it is passed as a callback.
        this._headers         = utils.bind(this, this._headers);
        this.fullpath         = utils.bind(this, this.fullpath);
        this.urlify           = utils.bind(this, this.urlify);
        this.get              = utils.bind(this, this.get);
        this.del              = utils.bind(this, this.del);
        this.post             = utils.bind(this, this.post);
        this.login            = utils.bind(this, this.login);
        this._shouldAutoLogin = utils.bind(this, this._shouldAutoLogin);
        this._requestWrapper  = utils.bind(this, this._requestWrapper);
    },

### login

##### splunkjs.Context.login

Authenticates and logs in to a Splunk instance, then stores the resulting session key.

#### Syntax

login: function(callback)

#### Parameters

Name | Type | Description
--- | --- | ---
callback | Function | The function to call when login has finished: `(err, wasSuccessful)`.

#### Source (lib/context.js:295)

    login: function(callback) {
        var that = this;
        var url = this.paths.login;
        var params = { username: this.username, password: this.password };
        callback = callback || function() {};
        var wrappedCallback = function(err, response) {
            // Let's make sure that not only did the request succeed, but
            // we actually got a non-empty session key back.
            var hasSessionKey = !!(!err && response.data && response.data.sessionKey);
            if (err || !hasSessionKey) {
                callback(err || "No session key available", false);
            }
            else {
                that.sessionKey = response.data.sessionKey;
                callback(null, true);
            }
        };
        return this.http.post(
            this.urlify(url),
            this._headers(),
            params,
            0,
            wrappedCallback
        );
    },

### post

##### splunkjs.Context.post

Performs a POST request.

#### Syntax

post: function(path, params, callback)

#### Parameters

Name | Type | Description
--- | --- | ---
path | String | The REST endpoint path of the POST request.
params | Object | The entity-specific parameters for this request.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/context.js:381)

    post: function(path, params, callback) {
        var that = this;
        var request = function(callback) {
            return that.http.post(
                that.urlify(path),
                that._headers(),
                params,
                0,
                callback
            );
        };
        return this._requestWrapper(request, callback);
    },

### request

##### splunkjs.Context.request

Issues an arbitrary HTTP request to the REST endpoint path segment.

#### Syntax

request: function(path, method, query, post, body, headers, callback)

#### Parameters

Name | Type | Description
--- | --- | ---
path | String | The REST endpoint path segment (with any query parameters already appended and encoded).
method | String | The HTTP method (can be `GET`, `POST`, or `DELETE`).
query | Object | The entity-specific parameters for this request.
post | Object | A dictionary of POST argument that will get form encoded.
body | Object | The body of the request, mutually exclusive with `post`.
headers | Object | Headers for this request.
callback | Function | The function to call when the request is complete: `(err, response)`.

#### Source (lib/context.js:409)

    request: function(path, method, query, post, body, headers, callback) {
        var that = this;
        var request = function(callback) {
            return that.http.request(
                that.urlify(path),
                {
                    method: method,
                    headers: that._headers(headers),
                    query: query,
                    post: post,
                    body: body,
                    timeout: 0
                },
                callback
            );
        };
        return this._requestWrapper(request, callback);
    },

### urlify

##### splunkjs.Context.urlify

Converts a partial path to a fully-qualified URL.

#### Syntax

urlify: function(path)

#### Parameters

Name | Type | Description
--- | --- | ---
path | String | The partial path.

#### Return

String.

The fully-qualified URL.

#### Source (lib/context.js:282)

    urlify: function(path) {
        return this.prefix + this.fullpath(path);
    },

### versionCompare

##### splunkjs.Context.versionCompare

Compares the Splunk server's version to the specified version string. Returns -1 if (this.version < otherVersion), 0 if (this.version == otherVersion), 1 if (this.version > otherVersion).

#### Syntax

versionCompare: function(otherVersion)

#### Parameters

Name | Type | Description
--- | --- | ---
otherVersion | String | The other version string, for example "5.0".

#### Source (lib/context.js:439)

    versionCompare: function(otherVersion) {
            var thisVersion = this.version;
            if (thisVersion === "default") {
                thisVersion = "5.0";
            }
            var components1 = thisVersion.split(".");
            var components2 = otherVersion.split(".");
            var numComponents = Math.max(components1.length, components2.length);
            for (var i = 0; i < numComponents; i++) {
                var c1 = (i < components1.length) ? parseInt(components1[i], 10) : 0;
                var c2 = (i < components2.length) ? parseInt(components2[i], 10) : 0;
                if (c1 < c2) {
                    return -1;
                } else if (c1 > c2) {
                    return 1;
                }
            }
            return 0;
        }
    });