OctoPrintClient.util
Note
All methods here require that the used API token or the existing browser session has admin rights.
- OctoPrintClient.util.test(command, parameters, opts)
Execute a test command.
See below for the more specialized versions of this.
- Arguments
command (
string()
) – The command to execute (currently eitherpath
orurl
)parameters (
object()
) – The parameters for the commandopts (
object()
) – Additional options for the request
- Returns Promise
A jQuery Promise for the request’s response
- OctoPrintClient.util.testPath(path, additional, opts)
Test the provided
path
for existence. More test criteria supported by the path test command can be provided via theadditional
object.Example 1
Test if
/some/path/to/a/file
exists.OctoPrint.util.testPath("/some/path/to/a/file") .done(function(response) { if (response.result) { // check passed } else { // check failed } });
Example 2
Test if
/some/path/to/a/file
exists, is a file and OctoPrint has read and executable rights on it.OctoPrint.util.testPath("/some/path/to/a/file", {"check_type": "file", "check_access": ["r", "x"]}) .done(function(response) { if (response.result) { // check passed } else { // check failed } });
- Arguments
path (
string()
) – Path to testadditional (
object()
) – Additional parameters for the test commandopts (
object()
) – Additional options for the request
- Returns Promise
A jQuery Promise for the request’s response
- OctoPrintClient.util.testExecutable(path, opts)
Shortcut to test if a provided
path
exists and is executable by OctoPrint.Example
Test if
/some/path/to/a/file
exists and can be executed by OctoPrint.OctoPrint.util.testExecutable("/some/path/to/a/file") .done(function(response) { if (response.result) { // check passed } else { // check failed } });
This is equivalent to calling
OctoPrint.util.testPath()
like this:OctoPrint.util.testPath("/some/path/to/a/file", {"access": "x"}) .done(function(response) { if (response.result) { // check passed } else { // check failed } });
- Arguments
path (
string()
) – Path to testopts (
object()
) – Additional options for the request
- Returns Promise
A jQuery Promise for the request’s response
- OctoPrintClient.util.testUrl(url, additional, opts)
Test if a URL can be accessed. More test criteria supported by the URL test command can be provided via the
additional
object.Example 1
Test if
http://octopi.local/online.gif
can be accessed and returns a non-error status code within the default timeout.OctoPrint.util.testUrl("http://octopi.local/online.gif") .done(function(response) { if (response.result) { // check passed } else { // check failed } });
Example 2
Test if
http://octopi.local/webcam/?action=snapshot
can be accessed and returns a non-error status code. Return the raw response data and headers from the check as well.OctoPrint.util.testUrl("http://octopi.local/webcam/?action=snapshot", {"response": "bytes", "method": "GET"}) .done(function(response) { if (response.result) { // check passed var content = response.response.content; var mimeType = "image/jpeg"; var headers = response.response.headers; if (headers && headers["content-type"]) { mimeType = headers["content-type"].split(";")[0]; } var image = $("#someimage"); image.src = "data:" + mimeType + ";base64," + content; } else { // check failed } });
Example 3
Test if a “GET” request against
http://example.com/idonotexist
returns either a 404 Not Found or a 400 Bad Request.OctoPrint.util.testUrl("http://example.com/idonotexist", {"status": [400, 404], "method": "GET"}) .done(function(response) { if (response.result) { // check passed } else { // check failed } });
- Arguments
url (
string()
) – URL to testadditional (
object()
) – Additional parameters for the test commandopts (
object()
) – Additional options for the request
- Returns Promise
A jQuery Promise for the request’s response
- OctoPrintClient.util.testServer(host, port, additional, opts)
Test if a server is reachable. More options supported by the server test command can be provided via the
additional
object.Example 1
Test if
8.8.8.8
is reachable on port 53 within the default timeout.OctoPrint.util.testServer("8.8.8.8", 53) .done(function(response) { if (response.result) { // check passed } else { // check failed } });
Example 2
Test if
127.0.0.1
is reachable on port 1234 and UDP.OctoPrint.util.testServer("127.0.0.1", 1234, {"protocol": "udp"}) .done(function(response) { if (response.result) { // check passed } else { // check failed } });
- Arguments
url (
string()
) – Host to testport (
int()
) – Port to testadditional (
object()
) – Additional parameters for the test commandopts (
object()
) – Additional options for the request
- Returns Promise
A jQuery Promise for the request’s response
- OctoPrintClient.util.testResolution(name, additional, opts)
Test if a host name can be resolved.
Example
Test if
octoprint.org
can be resolved.OctoPrint.util.testResolution("octoprint.org") .done(function(response) { if (response.result) { // check passed } else { // check failed } });
- Arguments
name (
string()
) – Host name to testadditional (
object()
) – Additional parameters for the test commandopts (
object()
) – Additional options for the request
- Returns Promise
A jQuery Promise for the request’s response
See also
- Util API
Documentation of the underlying util API.