-
Notifications
You must be signed in to change notification settings - Fork 6
/
prexview.py
126 lines (92 loc) · 3.89 KB
/
prexview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from os import getenv
from json import loads, JSONEncoder
from requests import post
class _Singleton(type):
""" A metaclass that creates a Singleton base class when called. """
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
else:
cls._instances[cls].__init__(*args, **kwargs)
return cls._instances[cls]
class Singleton(_Singleton('SingletonMeta', (object,), {})): pass
class PrexView(Singleton):
_URL = 'https://api.prexview.com/v1/'
def __init__(self, token = getenv('PXV_API_KEY')):
self.token = token
def __send(self, data):
headers = {
'Authorization': self.token
}
response = post(self._URL + 'transform', data = data, headers = headers)
if response.raise_for_status() is not None:
return None
result = {
'rateLimit': response.headers['x-ratelimit-limit'],
'rateLimitReset': response.headers['x-ratelimit-reset'],
'rateRemaining': response.headers['x-ratelimit-remaining'],
}
if response.status_code is 200:
result['id'] = response.headers['x-transaction-id']
result['file'] = response.content
result['responseTime'] = response.headers['x-response-time']
return result
def __isJson(self, str):
try:
json = loads(str)
except ValueError, e:
return False
return True
def __checkOptions(self, _format, options):
# JSON
if _format is 'json':
if type(options['json']) is str:
if self.__isJson(options['json']) is not True:
raise Exception('PrexView content must be a valid JSON string')
else:
if options['json'] is None or type(options['json']) is not dict:
raise Exception('PrexView content must be a dictionary object or a valid JSON string')
else:
options['json'] = JSONEncoder().encode(options['json'])
# XML
else:
if type(options['xml']) is not str:
raise Exception('PrexView content must be a valid XML string')
# TODO: design option is deprecated, this should be removed
if 'design' in options:
print('Prexview property "design" is deprecated, please use "template" property')
options['template'] = options['design']
del options['design']
if type(options['template']) is not str:
raise Exception('PrexView property "template" must be passed as a string option')
if type(options['output']) is not str:
raise Exception('PrexView property "output" must be passed as a string option')
if options['output'] not in ['html','pdf','png','jpg']:
raise Exception('PrexView property "output" must be one of these options: html, pdf, png or jpg')
# TODO: designBackup option is deprecated, this shuold be removed
if 'designBackup' in options:
print('Prexview property "designBackup" is deprecated, please use "templateBackup" property')
options['templateBackup'] = options['designBackup']
del options['designBackup']
if 'templateBackup' in options and type(options['templateBackup']) is not str:
raise Exception('PrexView property "templateBackup" must be a string')
if 'note' in options:
if type(options['note']) is not str:
raise Exception('PrexView property "note" must be a string')
if len(options['note']) > 500:
options['note'] = options['note'][0:500]
return options
def __checkToken(self):
if self.token is None or self.token is '':
raise Exception('PrexView environment variable PXV_API_KEY must be set')
def sendXML(self, content, options):
self.__checkToken()
options['xml'] = content
result = self.__checkOptions('xml', options)
return self.__send(result)
def sendJSON(self, content, options):
self.__checkToken()
options['json'] = content
result = self.__checkOptions('json', options)
return self.__send(result)