-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagizer_client.py
48 lines (33 loc) · 1.27 KB
/
imagizer_client.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
import urllib
class ImagizerClient:
DEFAULT_DPR = 1.0
DEFAULT_QUALITY = 90
DEFAULT_IMAGIZER_HOST = "demo.imagizercdn.com"
def __init__(self, imagizer_host=DEFAULT_IMAGIZER_HOST, use_https=False):
self.dpr = self.DEFAULT_DPR
self.quality = self.DEFAULT_QUALITY
self.imagizer_host = imagizer_host
self.use_https = use_https
self.format = ""
self.imageOriginHost = ""
def build_url(self, path, params=None):
if not params:
params = {}
if not path.startswith("/"):
path = "/" + path
params = self.add_global_params(params)
scheme = "https" if self.use_https else "http"
url = scheme + "://" + self.imagizer_host + path
if len(params) > 0:
url += "?" + urllib.urlencode(params)
return url
def add_global_params(self, params):
if "dpr" not in params and self.dpr != self.DEFAULT_DPR:
params['dpr'] = self.dpr
if "quality" not in params and self.quality != self.DEFAULT_QUALITY:
params['quality'] = self.quality
if self.format:
params['format'] = self.format
if self.imageOriginHost:
params['hostname'] = self.imageOriginHost
return params