Skip to content

Commit

Permalink
add pagination support
Browse files Browse the repository at this point in the history
  • Loading branch information
m4us1ne committed May 11, 2023
1 parent bbe7fbf commit e335c2c
Showing 1 changed file with 34 additions and 24 deletions.
58 changes: 34 additions & 24 deletions kandji2snipe
Original file line number Diff line number Diff line change
Expand Up @@ -469,31 +469,41 @@ def search_snipe_asset(serial):

# Function to get all the asset models from Snipe-IT
def get_snipe_models():
api_url = '{}/api/v1/models'.format(snipe_base)
logging.debug('Calling against: {}'.format(api_url))
response = requests.get(api_url, headers=snipeheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
jsonresponse = response.json()
logging.info("Got a valid response that should have {} models.".format(jsonresponse['total']))
if jsonresponse['total'] <= len(jsonresponse['rows']) :
return jsonresponse
models = []
limit = 100
offset = 0
while True:
api_url = "{}/api/v1/models?limit={}&offset={}".format(
snipe_base, limit, offset
)

logging.debug(f"Calling against: {api_url}")

response = requests.get(
api_url,
headers=snipeheaders,
verify=user_args.do_not_verify_ssl,
hooks={"response": request_handler},
)
if response.status_code == 200:
jsonresponse = response.json()
logging.info(
"Got a valid response that should have {}/{} models.".format(
len(models), jsonresponse["total"]
)
)
models.extend(jsonresponse["rows"])
offset += limit
if 0 == len(jsonresponse["rows"]):
return {"rows": models}

else:
logging.info("We didn't get enough results so we need to get them again.")
api_url = '{}/api/v1/models?limit={}'.format(snipe_base, jsonresponse['total'])
newresponse = requests.get(api_url, headers=snipeheaders, verify=user_args.do_not_verify_ssl, hooks={'response': request_handler})
if response.status_code == 200:
newjsonresponse = newresponse.json()
if newjsonresponse['total'] == len(newjsonresponse['rows']) :
return newjsonresponse
else:
logging.error("Unable to get all models from Snipe-IT")
sys.exit(exit_error_message)
else:
logging.error('When we tried to retreive a list of models, Snipe-IT responded with error status code:{} - {}'.format(response.status_code, response.content))
sys.exit(exit_error_message)
else:
logging.error('When we tried to retreive a list of models, Snipe-IT responded with error status code:{} - {}'.format(response.status_code, response.content))
sys.exit(exit_error_message)
logging.error(
"When we tried to retreive a list of models, Snipe-IT responded with error status code:{} - {}".format(
response.status_code, response.content
)
)
sys.exit(exit_error_message)

# Recursive function returns all users in a Snipe-IT Instance, 100 at a time.
def get_snipe_users(previous=[]):
Expand Down

0 comments on commit e335c2c

Please sign in to comment.