I'm having trouble getting a formatted result in paged JSON. My code instead of returning the result of the bound, is bringing the result per page!
import requests
def __init__(self, usuario, token):
self.usuario = usuario
self.token = token
def get_json(self, url):
retorno = requests.get(url, auth=(self.usuario, self.token))
if retorno.status_code != 200:
print('Status:', retorno.status_code, 'Problem with the request. Exiting.')
exit()
return retorno.json()
def get_list_url(self, url):
lista_url = [url]
json_retorno = self.get_json(url)
while json_retorno['next_page'] is not None:
lista_url.append(json_retorno['next_page'])
json_retorno = self.get_json(json_retorno['next_page'])
return lista_url
def imprime_lista(self, url):
json_retorno = self.get_json(url)
resolvidos_30d = 0
limite = 30
data_atual = datetime.strptime('2018-06-06', '%Y-%m-%d')
for res in json_retorno['tickets']:
if res['dates']['solved_at'] is not None:
solved_at = res['dates']['solved_at'][:10]
data_resolvido = datetime.strptime(solved_at, '%Y-%m-%d')
quantidade_dias = abs((data_atual - data_resolvido).days)
if quantidade_dias <= limite:
resolvidos_30d = resolvidos_30d + 1
elif quantidade_dias > limite:
break
print(id, resolvidos_30d, sep=';')
tickets = ['2720018']
urlTrigger = 'exemplo.com.br'
api = login('[email protected] /token', 'xxx')
for id in tickets:
role_znd = id
lista = api.get_list_url(urlTrigger+str(role_znd+'/includes'))
for url in lista:
api.imprime_lista(url)
It returns me something like this ...
2720018;51
2720018;1
2720018;0
2720018;0
Process finished with exit code -1
Another basic detail is that the break is not respected, am I doing wrong?