Created
November 16, 2019 12:53
Star
You must be signed in to star a gist
XServer Python Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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