Skip to content
This repository was archived by the owner on Aug 21, 2018. It is now read-only.

Commit 5d98d22

Browse files
authored
Merge pull request #20 from robertcsapo/iss19
fixes #19
2 parents 8b174ac + 53b3497 commit 5d98d22

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

LM-4502/03-python/apic_em_code_1.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def get_token(url):
2424
# Combine URL, API call and parameters variables
2525
url += api_call
2626

27-
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False).json()
27+
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
28+
response.raise_for_status()
29+
response = response.json()
2830

2931
# Return authentication token from respond body
3032
return response["response"]["serviceTicket"]
@@ -40,16 +42,18 @@ def get_device_id(token, url):
4042

4143
# Combine URL, API call and parameters variables
4244
url += api_call
43-
44-
response = requests.get(url, headers=headers, verify=False).json()
45+
46+
response = requests.get(url, headers=headers, verify=False)
47+
response.raise_for_status()
48+
response = response.json()
4549

4650
# Iterate over the response and find first device with access role.
4751
# Return ID number of the first device matching the criteria
4852
for item in response['response']:
4953
if item['role'] == 'ACCESS':
5054
return item['id']
5155

52-
56+
5357
def get_config(token, url, id):
5458

5559
# Define API Call. To get specific device's configuration
@@ -62,7 +66,9 @@ def get_config(token, url, id):
6266
# Combine URL, API call variables
6367
url += api_call
6468

65-
response = requests.get(url, headers=headers, verify=False).json()
69+
response = requests.get(url, headers=headers, verify=False)
70+
response.raise_for_status()
71+
response = response.json()
6672

6773
# Create a file in present working directory
6874
file = open('access_host_1.txt', 'w')

LM-4502/03-python/apic_em_code_2.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ def get_token(url):
2424
# Combine URL, API call and parameters variables
2525
url += api_call
2626

27-
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False).json()
27+
response = requests.post(url, data=json.dumps(payload), headers=headers, verify=False)
28+
response.raise_for_status()
29+
response = response.json()
2830

2931
# Return authentication token from respond body
3032
return response["response"]["serviceTicket"]
3133

32-
34+
3335
def get_device_id(token, url):
3436

3537
# Define API Call
@@ -40,50 +42,54 @@ def get_device_id(token, url):
4042

4143
# Combine URL, API call and parameters variables
4244
url += api_call
43-
44-
response = requests.get(url, headers=headers, verify=False).json()
45+
46+
response = requests.get(url, headers=headers, verify=False)
47+
response.raise_for_status()
48+
response = response.json()
4549

4650
# Iterate over the response and find first device with access role.
4751
# Return ID number of the first device matching the criteria
4852
for item in response['response']:
4953
if item['role'] == 'ACCESS':
5054
return item['id']
5155

52-
56+
5357
def get_config(token, url, id):
5458
#Define API Call. To get specific device's configuration
5559
#we will need to add device's ID in the API call
5660
api_call = "/network-device/" + id +"/config"
57-
61+
5862
#Header information
5963
headers = {"X-AUTH-TOKEN" : token}
60-
64+
6165
# Combine URL, API call variables
6266
url +=api_call
63-
64-
response = requests.get(url, headers=headers, verify=False).json()
65-
67+
68+
response = requests.get(url, headers=headers, verify=False)
69+
response.raise_for_status()
70+
response = response.json()
71+
6672
#Find the hostname in the response body and save it to a hostname variable
6773
hostname = re.findall('hostname\s(.+?)\s', response['response'])[0]
68-
74+
6975
#Create a date_time variable which will hold current time
7076
date_time = datetime.datetime.now()
71-
77+
7278
#Create a variable which will hold the hostname combined with the date and time
7379
#The format will be hostname_year_month_day_hour.minute.second
7480
file_name = hostname + '_' + str(date_time.year) + '_' + str(date_time.month) + '_' + \
7581
str(date_time.day) + '_' + str(date_time.hour) + '.' + str(date_time.minute) + \
7682
'.' + str(date_time.second)
7783

7884
file = open(file_name+'.txt', 'w')
79-
85+
8086
#Write response body to the file
8187
file.write(response['response'])
82-
88+
8389
#Close the file when writing is complete
8490
file.close()
8591

86-
92+
8793
# Assign obtained authentication token to a variable. Provide APIC-EM's
8894
# URL address
8995
auth_token = get_token(apic_em_ip)

LM-4502/04-mission/apic_em_mission.py

+4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def getTicket():
4141

4242
# Performs a POST on the specified url to get the service ticket
4343
response = requests.post(url, data=json.dumps(payload), headers=header, verify=False)
44+
response.raise_for_status()
4445

4546
print(response)
4647

@@ -74,6 +75,7 @@ def getTopology(ticket):
7475

7576
# this statement performs a GET on the specified network device url
7677
response = requests.get(url, headers=header, verify=False)
78+
response.raise_for_status()
7779

7880
# convert data to json format.
7981
r_json = response.json()
@@ -127,6 +129,7 @@ def get_roomID():
127129

128130
# this statement performs a GET on the specified network device url
129131
response = requests.get(url, headers=header, verify=False)
132+
response.raise_for_status()
130133
r_json = response.json()
131134

132135
for item in r_json["items"]:
@@ -159,6 +162,7 @@ def post_spark(text, room_id):
159162
# this statement performs a GET on the specified network device url
160163
response = requests.post(url, data=json.dumps(
161164
payload), headers=header, verify=False)
165+
response.raise_for_status()
162166

163167
print("\nCheck the Spark Room. You've just posted a message!")
164168

0 commit comments

Comments
 (0)