PrintNode / PrintNode-JS

Silently print from within a browser using javascript and PrintNode remote printing service.
https://www.printnode.com
MIT License
68 stars 27 forks source link

TypeError: api.printjob is not a function #4

Open younas-bangash opened 7 years ago

younas-bangash commented 7 years ago

I am using the following code in order to print (Thermal Printer Connected With USB)

<!DOCTYPE html>
    <html>
    <head></head>
    <body>
        <h3>Websocket Client</h3>
        <div id='content'></div>

        <script type='text/javascript' src="src/printnode-ws-client-0.1.0.js"></script>
        <script type="text/javascript" src="src/printnode-api-client-0.1.0.js"></script>
        <script type='text/javascript'>

        var API_KEY = '4b550896b4eca762ab946c5ae38ba9f08da4d874';

        // got websocket
        if (!PrintNode.WebSocket.isSupported()) {

            // once a websocket is authenticated you can register
            function authenticated (authData) {
                if (authData.error) {
                    // most likely not authenticated, see authData.error for more detail
                    return;
                }
                // authData will contain information about accountId, permissions, and maxSubscriptions
                // ok, now make some requests to the server to get data you're interested in
                // pass in optional second argument to have this called when server publishes a event
                // but you can also use the event emitter
                this.getScales({}, function (measurement) {
                    // this is only meaningful if the websocket is running on the same machine
                    // as the running PrintNode client
                    console.log("scales data by subscription callback", measurement);
                    console.log("scales latency %dms", measurement.getLatency());
                });

            }

            // error callback fired if anything goes wrong including
            //  - exceptions thrown by subscription callbacks
            //  - network issues which cause socket to fail or any timeouts
            //  - errors in printnode server or client libs
            function errorCallback (err, data) {
                console.log("Error!", err, data);
            }

            // instantiate a new
            var ws = new PrintNode.WebSocket(
                {apiKey: API_KEY},
                authenticated,
                errorCallback
            );

            // subscribe to all computer events
            ws.subscribe("computer", function computerEvent (something, info) {
                // lots of things could come in on this event
                // for now we're only interested in scalesMeasurements
                if (something instanceof PrintNode.ScalesMeasurement) {
                    console.log("scales data by computer subscription", something);
                }
            });

            // subscribe to all scales events
            ws.subscribe("scales", function computerEvent (measurement) {
                console.log("scales data by scales subscription", measurement);
            });

            // debugging
            ws.subscribe('system.state', function stateChange (newState) {
                console.log("newState", newState);
            });

        // TODO fallback to polling
        } else {

            var httpOptions = {
                success: function (responseBody, repsonse) {
                    console.log("success", response);
                },
                error: function (responseBody, repsonse) {
                    console.log(response);
                },
                complete: function (response) {
                    console.log(
                        "%d %s %s returned %db in %dms",
                        response.xhr.status,
                        response.reqMethod,
                        response.reqUrl,
                        response.xhr.responseText.length,
                        response.getDuration()
                    );
                },
                timeout: function (url, duration) {
                    console.log("%s timeout %d", url, duration);
                }
            };

            var http = new PrintNode.HTTP(
                new PrintNode.HTTP.ApiKey(API_KEY),
                httpOptions
            );

            http.scales(
                {success: function (response) {
                    console.log("success from callback", response);
                }},
                {computerId: 0}
            ).then(
                function (response, info ) {
                    console.log("success from promise", response);
                },
                function (err) {
                    throw "simulated exception in a promise callback";
                }
            // If your promise callbacks start throw[ing] you can 'catch' this in the returned
            // promise from .then() but this is getting pretty meta right here.
            // Callbacks really shouldn't throw exceptions in async code like this.
            ).then(null, console.log.bind(console, "promise callbacks threw error;"));

        }

        var options = {
    // changes the value of 'this' in the success, error, timeout and complete
    // handlers. The default value of 'this' is the instance of the PrintNodeApi
    // object used to make the api call
    context: null,
    // called if the api call was a 2xx success
    success: function (response, headers, xhrObject) {
        console.log(this);
        console.log("success", response, headers);
    },
    // called if the api call failed in any way
    error: function (response, headers, xhrObject) {
        console.log("error", response, headers);
    },
    // called afer the api call has completed after success or error callback
    complete: function (xhrObject) {
        console.log("complete");
    },
    // called if the api call timed out
    timeout: function (url, duration) {
        console.log("timeout", url, duration)
    },
    // the timeout duration in ms
    timeoutDuration: 3000
};

var api = new PrintNode.HTTP(
    new PrintNode.HTTP.ApiKey(API_KEY),
    options
);

// whoami - https://www.printnode.com/docs/api/curl/#whoami
api.whoami(options);

// computers - https://www.printnode.com/docs/api/curl/#computers
api.computers(options);
// with filtering
api.computers(options, {computerSet: '-400'});

// printers - https://www.printnode.com/docs/api/curl/#printers
api.printers(options);
// with filtering by computer
api.printers(options, {computerSet: '-400'});
// with filtering by computer and printer
api.printers(options, {computerSet: '-400', printerSet: '100-'});

// creating a printjob - http://website2.printnode.com/docs/api/curl/#printjob-creating
var printJobPayload = {
    "printerId": 8075,
    "title": "test printjob",
    "contentType": "pdf_uri",
    "content": "https://app.printnode.com/testpdfs/a4_portrait.pdf",
    "source": "javascript api client"
}
api.printjob(options, printJobPayload);

// scales HTTP REST - https://www.printnode.com/docs/api/curl/#scales-http
api.scales(options, {computerId: 12});
// with device name
api.scales(options, {computerId: 12, deviceName: 'foo_scales'});
// with device name and device id
api.scales(options, {computerId: 12, deviceName: 'foo_scales', deviceId: 34});
// generate fake output from the scales for debug - https://www.printnode.com/docs/test-scales-api/
// (default timeout is extended to 15,000ms)
api.scales(options, {computerId: 12, deviceName: 'foo_scales', deviceId: 34});

// example using a promise

api.whoami(options).then(
  function success (response, info) {
    console.log(response, info);
  },
  function error (err) {
    console.error(err);
  }
);

        </script>
    </body>
</html>
stevecarp commented 6 years ago

Use api.createPrintjob... I believe documentation is just not updated.