Skip to content

Instantly share code, notes, and snippets.

@boxpositron
Created November 16, 2019 12:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save boxpositron/6a3e8ca96f3e2ff9f24dd986316951ca to your computer and use it in GitHub Desktop.
XServer Python Integration
import requests
class LicenseValidationError(Exception):
'''This exception is raised when license validation fails'''
pass
class LicenseActivationError(Exception):
'''This exception is raised when license activation fails'''
pass
API_BASE = 'https://xserver.boxmarshall.com/api/v2/authorize/{0}'
def get_mac():
''' Compute a unique ID for the current machine what will not change whenever this function is run'''
pass
class XServerManager():
def __init__(self, product):
self.pid = product
self.uuid = get_mac()
self.serialkey = ''
def activate(self):
global API_BASE
url = API_BASE.format('activate')
response = requests.post(url=url, json={
"pid": self.pid,
"serialkey": self.serialkey,
"muid": self.uuid
})
result = response.json()
if response.status_code == 200 and result['success'] == True:
# Generate secure link after this step
return True, 'Your license key has been activated'
if not result['success']:
raise LicenseActivationError(result['error'])
if response.status_code == 401:
raise LicenseActivationError(result['error'])
def validate(self):
global API_BASE
url = API_BASE.format('validate')
response = requests.post(url=url, json={
"pid": self.pid,
"serialkey": self.serialkey,
"muid": self.uuid
})
result = response.json()
if not result['success']:
raise LicenseValidationError(result['error'])
def process_key(self, serial):
self.serialkey = serial
try:
self.activate()
except LicenseActivationError:
try:
self.validate()
return True, 'Your license key has been validated'
except LicenseValidationError as e:
return False, e
except Exception as e:
return False, e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment