This is a Python library to interact with PrintNode's remote printing API. This client allows you to access the API's functions for quick use in Python scripts.
- Python 2.x or 3.x
- python-requests
- python-future
This is installed as a module by executing python3 setup.py install
with root permissions.
The default constructor for the Library is a Gateway, which is constructed with at minimum of one key-word argument, that being an api-key.
from printnodeapi.Gateway import Gateway
my_account_gateway = Gateway(apikey='my_api_key')
After creating a Gateway, you can access any requests as such:
my_account_id = my_account_gateway.account.id
new_tag = my_account_gateway.ModifyTag("Likes","PrintNode")
You can authenticate in 2 ways other than using an api-key:
Gateway(email='email',password='password')
Gateway(clientkey='ckey')
The three below will authenticate with access to child accounts of a specific user:
Gateway(apikey='api-key',child_email='c_email')
Gateway(apikey='api-key',child_ref='c_creator_ref')
Gateway(apikey='api-key',child_id='c_id')
All of these will have the associated API doc next to them. Any of the argument types will be exactly the same as the attributes used in a normal request. Any "Objects" under type are represented as a dict
, which will have the same representation as the JSON objects.
This handles anything that is associated with a computer, such as Printers, PrintJobs, States (of PrintJobs) and Scales.
https://www.printnode.com/docs/api/curl/#whoami
Returns an Account object of the currently authenticated account.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
print(gateway.account.firstname)
'''
Results:
Mr
'''
https://www.printnode.com/docs/api/curl/#computers
Given a set of computers, returns these computers. Given only one id, returns that computer.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
print(gateway.computers(computer=10027).name)
'''
Results:
5.2015-07-10 15:04:40.253763.TEST-COMPUTER
'''
https://www.printnode.com/docs/api/curl/#printers
There are four ways this can be run:
- printer & computer argument both None: Gives all printers attached to the account.
- printer str/int, computer None: Gives a printer found from either an id or the name of a printer, taken from all possible printers.
- printer None, computer int: Gives all printers attached to the computer given by an id.
- printer str/int, computer int: Gives a printer found from either an id or the name of a printer, taken only from printers attached to the computer specified by the computer's id.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
print(gateway.printers(printer=50120).name)
for printer in gateway.printers(computer=10027):
print(printer.name)
'''
Results:
10027.3.TEST-PRINTER
10027.1.TEST-PRINTER
10027.2.TEST-PRINTER
10027.3.TEST-PRINTER
10027.4.TEST-PRINTER
10027.5.TEST-PRINTER
'''
https://www.printnode.com/docs/api/curl/#printjob-viewing
There are five ways this can be run:
- No arguments : Returns all printjobs associated with the account.
- computer int : Returns all printjobs relative to printers associated with the computer specified by the argument computer.
- printer int : Returns all printjobs relative to the printer specificed by the argument printer.
- computer int, printer int : Returns all printjobs relative to the printer specified by the argument printer from printers with access to computer.
- printjob int : Returns specific printjob.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
printjob_id = gateway.printjobs(computer=10027)[0].id
print(gateway.printjobs(printer=50120)[0].id)
print(gateway.printjobs(printjob=printjob_id))
'''
Results:
251137
PrintJob(id=251127, printer=Printer(id=50118, computer=Computer(id=10027, name='5.2015-07-10 15:04:40.253763.TEST-COMPUTER', inet=None, inet6=None, hostname=None, version=None, create_timestamp='2015-07-10T15:04:40.253Z', state='created'), name='10027.1.TEST-PRINTER', description='description', capabilities={'capability_1': 'one', 'capability_2': 'two'}, default=True, create_timestamp='2015-07-10T15:04:40.253Z', state=None), title='50118.1.TEST-PRINTJOB', content_type='pdf_uri', source='API test endpoint', expire_at=None, create_timestamp='2015-07-10T15:04:40.253Z', state='new')
'''
https://www.printnode.com/docs/api/curl/#printjob-creating
PrintJob(self, computer=None, printer=None, job_type='pdf', title='PrintJob',options=None,authentication=None,uri=None,base64=None,binary=None)
Only one of uri, base64 and binary can be chosen.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
print(gateway.PrintJob(printer=50120,options={"copies":2},uri="a.pdf"))
'''
Results:
PrintJob(id=251153, printer=Printer(id=50120, computer=Computer(id=10027, name='5.2015-07-10 15:04:40.253763.TEST-COMPUTER', inet=None, inet6=None, hostname=None, version=None, create_timestamp='2015-07-10T15:04:40.253Z', state='created'), name='10027.3.TEST-PRINTER', description='description', capabilities={'capability_1': 'one', 'capability_2': 'two'}, default=False, create_timestamp='2015-07-10T15:04:40.253Z', state=None), title='PrintJob', content_type='pdf_uri', source='PythonApiClient', expire_at=None, create_timestamp='2015-07-10T15:05:27.087Z', state='new')
'''
https://www.printnode.com/docs/api/curl/#printjob-states
Given a set of printjobs as a string (check https://www.printnode.com/docs/api/curl/#parameters for examples), returns a list of object type State. As each PrintJob can have many states, states()
is a list of PrintJobs that each have a list of States.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
printjob_id = gateway.printjobs(computer=10027)[0].id
print(gateway.states(printjob_id)[0][0].state)
'''
Results:
new
'''
This handles anything to do with accounts, such as Account creation, deletion and modificaiton, api-key handling, tag handling and Client handling.
https://www.printnode.com/docs/api/curl/#account-tagging
Given a tagname, returns the value of that tag.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
print(gateway.tag("Likes"))
'''
Results:
Everything!
'''
Given a tagname and tagvalue, either creates a tag with specifed value if tagname doesn't exist, otherwise changes the value.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
gateway.ModifyTag("Likes","PrintNode")
print(gateway.tag("Likes"))
'''
Results:
PrintNode
'''
Given a tagname, deletes that tag. Returns True on successful deletion.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
gateway.ModifyTag("Likes","PrintNode")
gateway.DeleteTag("Likes")
print(gateway.account.tags)
'''
Results:
[]
'''
https://www.printnode.com/docs/api/curl/#account-creation
CreateAccount(self, firstname, lastname, email, password, creator_ref=None, api_keys=None, tags=None)
Creates an account with the specified values. The last three are optional.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"}
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
print(new_account_gateway.account.firstname)
new_account_gateway.DeleteAccount()
'''
Results:
A
'''
https://www.printnode.com/docs/api/curl/#account-deletion
Deletes the child account that is currently authenticated. Accounts can only be deleted if authenticated by a parent account's api-key and a reference to the child account being deleted (e.g the child account's id)
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"}
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
new_account_gateway.DeleteAccount()
print(new_account["Account"]["id"] in gateway.account.child_accounts)
'''
Results:
False
'''
https://www.printnode.com/docs/api/curl/#account-modification
Given one or more arguments, changes the account details specified by the arguments.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"}
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
new_account_gateway.ModifyAccount(firstname="B")
print(new_account_gateway.account.firstname)
new_account_gateway.DeleteAccount()
'''
Results:
B
'''
https://www.printnode.com/docs/api/curl/#account-apikeys
Returns value of api-key specified by the argument.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"},
api_keys=["Production"]
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
print(new_account_gateway.api_key("Production"))
new_account_gateway.DeleteAccount()
'''
Results:
5a272ed7406d351be86f25be388810dc83b8f52d
'''
Creates an api-key with the api-key's reference given by the argument.
from printnodeapi import Gateway
gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"},
api_keys=["Production"]
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
new_account_gateway.CreateApiKey("Development")
print(new_account_gateway.account.api_keys)
new_account_gateway.DeleteAccount()
'''
Results:
{'Development': '123SecretAPIKey', 'Production': '456SecretAPIKey'}
'''
Deletes api-key specified by the argument.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"},
api_keys=["Production"]
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
new_account_gateway.DeleteApiKey("Production")
print(new_account_gateway.account.api_keys)
new_account_gateway.DeleteAccount()
'''
Results:
[]
'''
https://www.printnode.com/docs/api/curl/#account-delegated-auth
Generates a clientkey for the account.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
new_account = gateway.CreateAccount(
firstname="A",
lastname="Person",
password="password",
email="[email protected]",
tags={"Likes":"Something"},
api_keys=["Production"]
)
new_account_gateway = Gateway(url='https://api.printnode.com',apikey='secretAPIKey',child_id=new_account["Account"]["id"])
new_clientkey = new_account_gateway.clientkey(
uuid="0a756864-602e-428f-a90b-842dee47f57e",
edition="printnode",
version="4.7.2")
print(new_clientkey)
new_account_gateway.DeleteAccount()
'''
Results:
ck-nwa2SSvSGl1YR5zrHDVVgfdpJ8JLfVCvwaCWj8dQXmZW
'''
https://www.printnode.com/docs/api/curl/#account-download-management
This has three different outcomes:
- os and client_ids both None: Returns all clients available for the current account.
- os str and client_ids None: Returns the most recent version for given OS ("windows" or "osx" only)
- os None and client_ids str: Given a set of ids (e.g "11-15"), return all clients in that set.
Having both set will default to showing the most recent version for the os argument.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
for client in gateway.clients(client_ids="11-12"):
print(client.id)
print(gateway.clients(os="windows").os)
'''
Results:
12
11
windows
'''
Given a set of ids and either True or False, sets whether the clients are enabled or not. Returns a list of modified clients.
from printnodeapi import Gateway
gateway=Gateway(url='https://api.printnode.com',apikey='secretAPIKey')
gateway.ModifyClientDownloads(11,False)
print(gateway.clients(client_ids=11)[0].enabled)
'''
Results:
False
'''