Skip to content

Commit 651cce8

Browse files
committed
Raised exceptions instead of silent failing
1 parent 8accee0 commit 651cce8

1 file changed

Lines changed: 29 additions & 7 deletions

File tree

ncm/ncm/ncm.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100

101101
from requests import Session
102102
from requests.adapters import HTTPAdapter
103+
import requests
103104
from http import HTTPStatus
104105
from urllib3.util.retry import Retry
105106
from datetime import datetime, timedelta
@@ -171,7 +172,9 @@ def log(self, level, message):
171172

172173
def _return_handler(self, status_code, returntext, obj_type):
173174
"""
174-
Prints returned HTTP request information if self.logEvents is True.
175+
Handles HTTP response status codes. Raises exceptions for error
176+
status codes so callers can use try/except for error handling.
177+
Returns response data for success codes.
175178
"""
176179
if str(status_code) == '200':
177180
return f'{obj_type} operation successful.'
@@ -186,16 +189,28 @@ def _return_handler(self, status_code, returntext, obj_type):
186189
return returntext
187190
elif str(status_code) == '400':
188191
self.log('error', 'Bad Request')
189-
return f'ERROR: {status_code}: {returntext}'
192+
raise requests.exceptions.HTTPError(
193+
f'{status_code}: {returntext}',
194+
response=type('Response', (), {'status_code': status_code, 'text': str(returntext)})()
195+
)
190196
elif str(status_code) == '401':
191197
self.log('error', 'Unauthorized Access')
192-
return f'ERROR: {status_code}: {returntext}'
198+
raise requests.exceptions.HTTPError(
199+
f'{status_code}: {returntext}',
200+
response=type('Response', (), {'status_code': status_code, 'text': str(returntext)})()
201+
)
193202
elif str(status_code) == '404':
194203
self.log('error', 'Resource Not Found\n')
195-
return f'ERROR: {status_code}: {returntext}'
204+
raise requests.exceptions.HTTPError(
205+
f'{status_code}: {returntext}',
206+
response=type('Response', (), {'status_code': status_code, 'text': str(returntext)})()
207+
)
196208
elif str(status_code) == '500':
197209
self.log('error', 'HTTP 500 - Server Error\n')
198-
return f'ERROR: {status_code}: {returntext}'
210+
raise requests.exceptions.HTTPError(
211+
f'{status_code}: {returntext}',
212+
response=type('Response', (), {'status_code': status_code, 'text': str(returntext)})()
213+
)
199214
else:
200215
self.log('info', f'HTTP Status Code: {status_code} - {returntext}\n')
201216

@@ -2965,20 +2980,27 @@ def regrade(self, subscription_id, mac, action="UPGRADE"):
29652980
}
29662981
mac = mac if isinstance(mac, list) else [mac]
29672982
for smac in mac:
2983+
# Normalize MAC to bare uppercase hex (strip colons, dashes, dots)
2984+
normalized = smac.upper().replace(':', '').replace('-', '').replace('.', '')
29682985
data = {
29692986
"op": "add",
29702987
"data": {
29712988
"type": "regrades",
29722989
"attributes": {
29732990
"action": action,
29742991
"subscription_type": subscription_id,
2975-
"mac_address": smac.replace(':','') if len(smac) == 17 else smac
2992+
"mac_address": normalized
29762993
}
29772994
}
29782995
}
29792996
payload["atomic:operations"].append(data)
29802997

2981-
ncm = self.session.post(post_url, json=payload)
2998+
headers = {
2999+
'Content-Type': 'application/vnd.api+json;ext="https://jsonapi.org/ext/atomic"',
3000+
'Accept': 'application/vnd.api+json;ext="https://jsonapi.org/ext/atomic"'
3001+
}
3002+
3003+
ncm = self.session.post(post_url, json=payload, headers=headers)
29823004
result = self._return_handler(ncm.status_code, ncm.json(), call_type)
29833005
return result
29843006

0 commit comments

Comments
 (0)