|
22 | 22 | * SOFTWARE. |
23 | 23 |
|
24 | 24 | """ |
| 25 | +import logging |
25 | 26 |
|
26 | 27 |
|
27 | | -class Config: |
| 28 | +logging.basicConfig(filename='contentstack.log', format='%(asctime)s - %(message)s', level=logging.INFO) |
| 29 | +logging.getLogger("Config") |
| 30 | + |
| 31 | + |
| 32 | +class Config(): |
28 | 33 |
|
29 | 34 | def __init__(self): |
30 | | - self.default_config = dict(protocol="https", host="cdn.contentstack.io", port=443, version="v3", urls={ |
31 | | - "sync": "/stacks/sync", |
32 | | - "content_types": "/content_types/", |
33 | | - "entries": "/entries/", |
34 | | - "assets": "/assets/", |
35 | | - "environments": "/environments/" |
| 35 | + self.defaultConfig = dict(protocol="https://", host="cdn.contentstack.io", port=443, version="v3", path={ |
| 36 | + "stacks": "stacks", |
| 37 | + "sync": "stacks/sync", |
| 38 | + "content_types": "content_types", |
| 39 | + "entries": "entries", |
| 40 | + "assets": "assets", |
| 41 | + "environments": "environments" |
36 | 42 | }) |
37 | 43 |
|
38 | 44 | def host(self, host_url=None): |
39 | 45 | if host_url is not None: |
40 | | - self.default_config["host"] = host_url |
41 | | - return self.default_config["host"] |
42 | | - |
43 | | - def version(self): |
44 | | - return self.default_config["host"] |
| 46 | + self.defaultConfig["host"] = host_url |
| 47 | + return self.defaultConfig["host"] |
45 | 48 |
|
46 | | - def protocol(self): |
47 | | - return self.default_config["protocol"] |
| 49 | + def version(self, version: str = None): |
| 50 | + if version is not None and isinstance(version, str): |
| 51 | + self.defaultConfig['version'] = version |
| 52 | + return self.defaultConfig['version'] |
| 53 | + else: |
| 54 | + return self.defaultConfig['version'] |
48 | 55 |
|
49 | | - def get_endpoint(self, url_path): |
50 | | - config_url = "{0}{1}/{2}/{3}".format(self.protocol(), self.host(), self.version(), url_path) |
51 | | - return config_url |
| 56 | + def path(self, path): |
| 57 | + url_section = self.defaultConfig['path'] |
| 58 | + if path in url_section: |
| 59 | + return url_section[path] |
| 60 | + else: |
| 61 | + logging.error("{0} is invalid endpoint path".format(path)) |
| 62 | + raise ValueError('Invalid endpoint!!, {0} is invalid endpoint path, ' |
| 63 | + 'Path can be found among {1}' |
| 64 | + .format(path, url_section.keys())) |
52 | 65 |
|
53 | | - def hiShailesh(self): |
54 | | - print("Something") |
| 66 | + def endpoint(self, path): |
| 67 | + url = self.path(path) |
| 68 | + if url is not None and isinstance(url, str): |
| 69 | + url = "{0}{1}/{2}/{3}".format(self.defaultConfig["protocol"], self.host(), self.version(), url) |
| 70 | + logging.info('{0} endpoint'.format(path)) |
| 71 | + return url |
55 | 72 |
|
56 | 73 |
|
57 | 74 | config = Config() |
58 | | -host = config.host("stag-cdn.contentstack.io") |
59 | | -print(host) |
| 75 | +config.host("cdn.contentstack.io") |
| 76 | +result_url = config.endpoint('assets') |
| 77 | +print(result_url) |
60 | 78 |
|
0 commit comments