|
20 | 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | 22 | * SOFTWARE. |
23 | | - * |
24 | | - """ |
25 | 23 |
|
| 24 | + """ |
26 | 25 | import logging |
27 | 26 |
|
28 | 27 |
|
29 | | -class Config(object): |
| 28 | +logging.basicConfig(filename='contentstack.log', format='%(asctime)s - %(message)s', level=logging.INFO) |
| 29 | +logging.getLogger("Config") |
| 30 | + |
| 31 | + |
| 32 | +class Config: |
| 33 | + |
| 34 | + def __init__(self): |
| 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": "content_types", |
| 40 | + "assets": "assets", |
| 41 | + "environments": "environments" |
| 42 | + }) |
| 43 | + |
| 44 | + def host(self, host_url=None): |
| 45 | + if host_url is not None: |
| 46 | + self.defaultConfig["host"] = host_url |
| 47 | + return self.defaultConfig["host"] |
30 | 48 |
|
31 | | - def __init__(self, host_url: str = 'cdn.contentstack.io'): |
32 | | - self.host_url = host_url |
33 | | - self.api_version: str = 'v3' |
34 | | - self.http_protocol: str = 'https://' |
| 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'] |
35 | 55 |
|
36 | | - def set_host(self, host_url=None): |
37 | | - logging.info("set host", host_url) |
38 | | - self.host_url = host_url |
39 | | - return self |
| 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())) |
40 | 65 |
|
41 | | - def get_host(self): |
42 | | - logging.info('getting host url', self.host_url) |
43 | | - return self.host_url |
| 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('endpoint is :: {0} '.format(url)) |
| 71 | + return url |
44 | 72 |
|
45 | | - def get_version(self): |
46 | | - logging.info('getting api version', self.api_version) |
47 | | - return self.api_version |
48 | 73 |
|
49 | | - def get_http_protocol(self): |
50 | | - logging.info('get http protocol', self.http_protocol) |
51 | | - return self.http_protocol |
| 74 | +config = Config() |
| 75 | +config.host("cdn.contentstack.io") |
| 76 | +result_url = config.endpoint('entries') |
| 77 | +print(result_url) |
52 | 78 |
|
53 | | - def get_endpoint(self, url_path): |
54 | | - api_version: str = self.get_version() |
55 | | - host_url = self.get_host() |
56 | | - http_protocol = self.get_http_protocol() |
57 | | - config_url = "{0}{1}/{2}/{3}".format(http_protocol, host_url, api_version, url_path) |
58 | | - return config_url |
|
0 commit comments