View difference between Paste ID: 1gv7P5wU and vDQWZmVX
SHOW: | | - or go back to the newest paste.
1
import requests
2
import csv
3
import time
4
import logging
5
6
logging.basicConfig(filename='checkin_logging.log',
7
    level=logging.INFO,
8
    format='%(asctime)s.%(msecs)03d, %(levelname)s, %(module)s, %(funcName)s, %(message)s',
9
    datefmt='%Y-%m-%d %H:%M:%S',
10
    )
11
12
# https://snipe-it.readme.io/reference#api-overview
13
# Trottles to 120 requests a minute, so set a wait time of 1/2 second between requests
14
15
apiKey = "yourapikey"
16
url = "https://yoursnipeiturl.com/api/v1/hardware/"
17
18
headers = {
19
    'authorization' : "Bearer " + apiKey,
20
    'accept' : "application/json",
21
    'content-type' : "application/json"
22
    }
23
24
def checkin():
25
    assetTag = ''
26
    while assetTag != 'exit':
27
        print("")
28
        print("==========================")
29
        assetTag = input("Check in Asset Tag: ")
30
        if assetTag == 'exit':
31
            break
32
        print("Checking in asset " + assetTag)
33
34
        # Need to find item's SNIPEID first
35
        searchURL = url + "bytag/" + str(assetTag)
36
        searchResponse = requests.request("GET", searchURL, headers = headers).json()
37
        time.sleep(.5)
38
        if 'status' in searchResponse:
39
            if searchResponse['status'] == 'error':
40
                print('Cannot find asset or other error')
41
                logging.error('Cannot find asset or other error')
42
        elif 'id' in searchResponse:
43
            print(searchResponse)
44
            # Then check in that ITEM ID
45
            checkinURL = url + str(searchResponse['id']) + "/checkin"
46
            checkinResponse = requests.post(checkinURL, headers = headers).json()
47
            print(checkinResponse)
48
            logging.info("Checked in asset %s" % str(assetTag))
49
50
    else:
51
        print('Exiting...')
52
53
def main():
54
    checkin()
55
56
if __name__ == '__main__':
57
    main()