-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetLogin.py
149 lines (143 loc) · 6.19 KB
/
GetLogin.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# This is the GetLogin file to execute Zendesk code challenge
# This file is responsible for connecting to Zendesk API and getting the ticket data for the user
# This project was created for Student internship at Zendesk
# Created by: Abhishek Pahuja
# Email ID: abhishekpahuja@hotmail.com
# Created on: 5 June 2019
# Interpreter: Python 3.7.3
# Add all the files and methods
# Import requests to send and receive requests
import requests
# time.sleep method to add delays wherever required
from time import sleep
# import clear from user defined ClearScreen.py
from ClearScreen import clear
# Import getpass from getpass to read passwords
from getpass import getpass
# Import base 64 to decode test account credentials
import base64
# Import invalid attempts method to check if user has exceeded number of permitted attempts
from AttemptsExceeded import InvalidAttempts
# import the ErrorCodes method from ErrorCodes.py
from ErrorCodes import ErrorCodes
# Login method to allow user to log into their account and domain
def Login():
# Set counter to 0
counter = 0
# If user does not make 3 incorrect attempts continue
while counter < 3:
print('https://{DomainName}.zendesk.com')
# Get user's login details
domain = input("Please enter the domain name (without curly braces): ")
email = input("Please enter your email address: ")
password = getpass("Please enter your password: ")
credentials = email, password
# Create session
session = requests.Session()
# Authenticate user
session.auth = credentials
zendesk = 'https://' + domain + '.zendesk.com/api/v2/tickets.json?page='
url = zendesk + '1'
# Get response from the Zendesk server
response = session.get(url)
# Check if the response was received
if response.status_code != 200:
# Call the error code method from errorCode.py
ErrorCodes(response.status_code)
# Sleep for 5 seconds before clearing screen
sleep(5)
clear()
# Increment the counter
counter += 1
clear()
print("Please check the data you entered."
"\nDomain Name: " + domain +
"\nEmail ID: " + email +
"\nPassword: " + password +
"\nTotal attempts remaining: " + str(3-counter))
else:
clear()
# Check the number of pages
DATA = response.json()
total_records = DATA['count']
# Calculate the number of pages
Total_Pages = int(total_records / 100) + 1
# If more than 1 page(100 tickets)
DataArray = [DATA]*(total_records)
RecordArray = [DATA] * Total_Pages
PageCount = 1
recordcounter = 0
# Put all the data in arrays
while recordcounter < total_records:
for records in RecordArray:
zendesk = 'https://' + domain + '.zendesk.com/api/v2/tickets.json?page='
url = zendesk + str(PageCount)
response = session.get(url)
# get all the ticket records
records = response.json()
# Get individual ticket records
for data in records['tickets']:
DataArray[recordcounter] = data
# Increment the record counter
recordcounter += 1
# Increment the to next page
PageCount += 1
# Set counter to 4 to break while loop
counter = 4
# display a message if no tickets were found
if total_records == 0:
print('this account has zero tickets. requsting tickets dislay will result in blank output')
# Return the ticket data
return DataArray
# If the user made 3 invalid attempts, close the program.
InvalidAttempts(counter)
# AutoLogin method contains credentials for a test account
def AutoLogin():
# Decode the credentials
credentials = base64.b64decode(b'YWJoaXNoZWtwYWh1amFAaG90bWFpbC5jb20=').decode('utf-8'), base64.b64decode(b'UElSQVRFU29mVEhFY2FyaWJiZWFu').decode('utf-8')
# Create session
session = requests.Session()
# Authenticate test user
session.auth = credentials
zendesk = 'https://thedottedline.zendesk.com/api/v2/tickets.json?page='
url = zendesk + '1'
response = session.get(url)
if response.status_code != 200:
# Call the error code method from errorCode.py
ErrorCodes(response.status_code)
# Sleep for 5 seconds before clearing screen
sleep(5)
clear()
else:
clear()
# Check the number of pages
DATA = response.json()
total_records = DATA['count']
# Calculate the number of pages
Total_Pages = int(total_records / 100) + 1
# If more than 1 page(100 tickets)
DataArray = [DATA] * (total_records)
RecordArray = [DATA] * Total_Pages
# Initialise counters
PageCount = 1
recordcounter = 0
# Put all the data in arrays
while recordcounter < total_records:
for records in RecordArray:
zendesk = 'https://thedottedline.zendesk.com/api/v2/tickets.json?page='
url = zendesk + str(PageCount)
response = session.get(url)
# Get all the ticket records
records = response.json()
# Get individual ticket records
for data in records['tickets']:
DataArray[recordcounter] = data
# Increment the record counter
recordcounter += 1
# Increment to next page
PageCount += 1
# display a message if no tickets were found
if total_records == 0:
print('this account has zero tickets. requsting tickets dislay will result in blank output')
# Return the collected tickets
return DataArray