Forum Discussion
Stuart_Weenig
Mastermind
3 years agoOn 7/22/2022 at 8:52 AM, Lewis Beard said:it makes the code so ugly
Yep, it does. Squirrel that away into a library you import? I just had to do that because i needed to make some v3 API calls that don't exist in the SDK at all.
import requests, json, hashlib, base64, time, hmac def LM_API(AccessId, AccessKey, Company, httpVerb, resourcePath, data=""): url = 'https://'+ Company +'.logicmonitor.com/santaba/rest' + resourcePath epoch = str(int(time.time() * 1000)) requestVars = httpVerb + epoch + data + resourcePath digest = hmac.new( AccessKey.encode('utf-8'), msg=requestVars.encode('utf-8'), digestmod=hashlib.sha256 ).hexdigest() signature = base64.b64encode(digest.encode('utf-8')).decode('utf-8') auth = 'LMv1 ' + AccessId + ':' + str(signature) + ':' + epoch headers = { 'Content-Type':'application/json', 'Authorization':auth, 'X-Version': "3" } if httpVerb == "GET": response = requests.get(url, data=data, headers=headers) elif httpVerb == "POST": response = requests.post(url, data=data, headers=headers) elif httpVerbe == "PUT": response = requests.put(url, data=data, headers=headers) # print('Response Status:',response.status_code) # print('Response Body:',response.content) return json.loads(response.text)