-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtickets.py
More file actions
64 lines (58 loc) · 2.44 KB
/
tickets.py
File metadata and controls
64 lines (58 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import pandas as pd
import requests
from beautifultable import BeautifulTable
from dotenv import load_dotenv
from urllib.parse import urlparse, parse_qs
load_dotenv()
data_to_display = ['id', 'subject', 'status']
class Tickets:
def __init__(self, username, password, root_url):
self.username = username
self.password = password
self.root_url = root_url
def view_list(self, page, page_size):
next_page_url = '{}.json?page={}&per_page={}'.format(self.root_url, page, page_size)
try:
table = BeautifulTable()
table.column_headers = data_to_display
r = requests.get(next_page_url, auth=(self.username, self.password))
data = r.json()
for ticket in data['tickets']:
table.append_row([ticket.get(key) for key in data_to_display])
table.append_row(['', '<<< End of page {} >>>'.format(str(page)), ''])
print(table)
next_page = data['next_page']
if next_page:
url = urlparse(data['next_page'])
return int(parse_qs(url.query)['page'][0])
else:
return None
except KeyError:
print("URL/Authentication error")
except requests.exceptions.HTTPError as errh:
print("Http Error: ", errh)
except requests.exceptions.ConnectionError as errc:
print("Error Connecting: ", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error: ", errt)
except requests.exceptions.RequestException as err:
print("Request Error: ", err)
def view_ticket(self, ticket_id):
try:
ticket_url = '{}/{}.json'.format(self.root_url, ticket_id)
r = requests.get(ticket_url, auth=(self.username, self.password))
data = r.json()
print(pd.DataFrame(data))
return data
except KeyError:
print("URL/Authentication error")
except ValueError:
print("Invalid ticket/Authentication error")
except requests.exceptions.HTTPError as errh:
print("Http Error: ", errh)
except requests.exceptions.ConnectionError as errc:
print("Error Connecting: ", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error: ", errt)
except requests.exceptions.RequestException as err:
print("Request Error ", err)