|
21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | 22 | * SOFTWARE. |
23 | 23 |
|
24 | | - """ |
| 24 | +""" |
25 | 25 | import logging |
26 | 26 |
|
27 | 27 |
|
28 | | -logging.basicConfig(filename='contentstack.log', format='%(asctime)s - %(message)s', level=logging.INFO) |
| 28 | +logging.basicConfig(filename='cs.log', format='%(asctime)s - %(message)s', level=logging.INFO) |
29 | 29 | logging.getLogger("Config") |
30 | 30 |
|
31 | 31 |
|
32 | | -class Config: |
| 32 | +class Config(object): |
| 33 | + """ |
| 34 | + All API paths are relative to this base URL, for example, /users actually means <scheme>://<host>/<basePath>/users. |
| 35 | +
|
| 36 | + """ |
33 | 37 |
|
34 | 38 | 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"] |
48 | | - |
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'] |
55 | | - |
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())) |
65 | | - |
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 |
72 | 39 |
|
| 40 | + # It initialises the Config with the default endpoint |
| 41 | + self.default = dict(protocol="https", host="cdn.contentstack.io", port=443, version="v3") |
| 42 | + |
| 43 | + def host(self, host): |
| 44 | + |
| 45 | + """ |
| 46 | + The base URL for Content Delivery API is cdn.contentstack.io. |
| 47 | + host is the domain name or IP address ( |
| 48 | + IPv4) of the host that serves the API. It may include the port number if different from the scheme's default |
| 49 | + port (443 for HTTPS). |
| 50 | +
|
| 51 | + Note: contentstack supports HTTPS only |
| 52 | + :param host: host is the domain name |
| 53 | + :type host: str |
| 54 | + :return: self |
| 55 | + :rtype: Config |
| 56 | +
|
| 57 | + Example: |
| 58 | +
|
| 59 | + >>> config = Config().host('api.contentstack.io') |
73 | 60 |
|
74 | | -config = Config() |
75 | | -config.host("cdn.contentstack.io") |
76 | | -result_url = config.endpoint('entries') |
77 | | -print(result_url) |
| 61 | + """ |
78 | 62 |
|
| 63 | + if host is not None and isinstance(host, str): |
| 64 | + self.default['host'] = host |
| 65 | + return self |
| 66 | + |
| 67 | + def version(self, version=None): |
| 68 | + |
| 69 | + """ |
| 70 | + Note: Only version 3 is supported on the CDN. If you're still using version 2 (which we recommend you should |
| 71 | + not), switch to the CDN version for even faster loading. |
| 72 | + :param version: The API version can be found in the URL that is basePath |
| 73 | + :type version: str |
| 74 | + :return: self |
| 75 | + :rtype: Config |
| 76 | +
|
| 77 | + Example: The API version (in our case, 'v3') can be found in the URL, e.g. |
| 78 | +
|
| 79 | + >>> config = Config() |
| 80 | + >>> config.version = 'v3' |
| 81 | +
|
| 82 | + """ |
| 83 | + if version is not None and isinstance(version, str): |
| 84 | + self.default['version'] = version |
| 85 | + |
| 86 | + return self |
| 87 | + |
| 88 | + @property |
| 89 | + def endpoint(self): |
| 90 | + url = "{0}://{1}/{2}".format(self.default["protocol"], self.default["host"], self.default["version"]) |
| 91 | + return url |
0 commit comments