|
| 1 | +import json |
| 2 | +import requests |
| 3 | + |
| 4 | +__all__ = ['NLIC_API_URL', 'NetLicensing'] |
| 5 | + |
| 6 | +NLIC_API_URL = 'https://go.netlicensing.io/core/v2/rest/' |
| 7 | + |
| 8 | +class NetLicensing: |
| 9 | + def __init__(self, imp_url=NLIC_API_URL): |
| 10 | + self.imp_url = imp_url |
| 11 | + requests_session = requests.Session() |
| 12 | + requests_adapters = requests.adapters.HTTPAdapter(max_retries=3) |
| 13 | + requests_session.mount('https://', requests_adapters) |
| 14 | + self.requests_session = requests_session |
| 15 | + |
| 16 | + def about(self): |
| 17 | + return (u'Labs64 NetLicensing is a first-class solution in the Licensing-as-a-Service (LaaS) sector.' |
| 18 | + u'Based on open standards, it provides a cost-effective, integrated and scalable platform for software vendors and developers' |
| 19 | + u'who want to concentrate on their product’s core functionality instead of spending resources on developing an own license management software.') |
| 20 | + |
| 21 | + class ResponseError(Exception): |
| 22 | + def __init__(self, code=None, message=None): |
| 23 | + self.code = code |
| 24 | + self.message = message |
| 25 | + |
| 26 | + class HttpError(Exception): |
| 27 | + def __init__(self, code=None, reason=None): |
| 28 | + self.code = code |
| 29 | + self.reason = reason |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def get_response(response): |
| 33 | + if response.status_code != requests.codes.ok: |
| 34 | + raise NetLicensing.HttpError(response.status_code, response.reason) |
| 35 | + result = response.json() |
| 36 | + if result['code'] != 0: |
| 37 | + raise NetLicensing.ResponseError( |
| 38 | + result.get('code'), result.get('message') |
| 39 | + ) |
| 40 | + return result.get('response') |
| 41 | + |
| 42 | + def get_headers(self): |
| 43 | + return {'Accept': 'application/json'} |
| 44 | + |
| 45 | + def _post(self, url, payload=None): |
| 46 | + headers = self.get_headers() |
| 47 | + response = self.requests_session.post(url, headers=headers, data=payload) |
| 48 | + return self.get_response(response) |
| 49 | + |
| 50 | + def validate(self, licensee_uid): |
| 51 | + url = f'{self.imp_url}licensee/{licensee_uid}/validate' |
| 52 | + payload = {'licensee_uid': licensee_uid, 'amount': licensee_uid} |
| 53 | + return self._post(url, payload) |
0 commit comments