Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import csv
- import time
- import logging
- logging.basicConfig(filename='checkin_logging.log',
- level=logging.INFO,
- format='%(asctime)s.%(msecs)03d, %(levelname)s, %(module)s, %(funcName)s, %(message)s',
- datefmt='%Y-%m-%d %H:%M:%S',
- )
- # https://snipe-it.readme.io/reference#api-overview
- # Trottles to 120 requests a minute, so set a wait time of 1/2 second between requests
- apiKey = "yourapikey"
- url = "https://yoursnipeiturl.com/api/v1/hardware/"
- headers = {
- 'authorization' : "Bearer " + apiKey,
- 'accept' : "application/json",
- 'content-type' : "application/json"
- }
- def checkin():
- assetTag = ''
- while assetTag != 'exit':
- print("")
- print("==========================")
- assetTag = input("Check in Asset Tag: ")
- if assetTag == 'exit':
- break
- print("Checking in asset " + assetTag)
- # Need to find item's SNIPEID first
- searchURL = url + "bytag/" + str(assetTag)
- searchResponse = requests.request("GET", searchURL, headers = headers).json()
- time.sleep(.5)
- if 'status' in searchResponse:
- if searchResponse['status'] == 'error':
- print('Cannot find asset or other error')
- logging.error('Cannot find asset or other error')
- elif 'id' in searchResponse:
- print(searchResponse)
- # Then check in that ITEM ID
- checkinURL = url + str(searchResponse['id']) + "/checkin"
- checkinResponse = requests.post(checkinURL, headers = headers).json()
- print(checkinResponse)
- logging.info("Checked in asset %s" % str(assetTag))
- else:
- print('Exiting...')
- def main():
- checkin()
- if __name__ == '__main__':
- main()
Comments
-
- Copied from k12sysadmin reddit user Zypherside discussion thread.
- https://www.reddit.com/r/k12sysadmin/comments/ms6hcc/snipe_it_bulk_check_in/
Add Comment
Please, Sign In to add comment