|
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:/", |
36 | | - host="cdn.contentstack.io", |
37 | | - port=443, |
38 | | - version="v3", |
39 | | - path={ |
40 | | - "stacks": "stacks", |
41 | | - "sync": "stacks/sync", |
42 | | - "content_types": "content_types", |
43 | | - "entries": "content_types", |
44 | | - "assets": "assets", |
45 | | - "environments": "environments" |
46 | | - }) |
47 | | - |
48 | | - def host(self, host_url=None): |
49 | | - if host_url is not None and isinstance(host_url, str): |
50 | | - self.defaultConfig['host'] = host_url |
51 | | - return self.defaultConfig['host'] |
| 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') |
| 60 | +
|
| 61 | + """ |
| 62 | + |
| 63 | + if host is not None and isinstance(host, str): |
| 64 | + self.default['host'] = host |
| 65 | + return self |
52 | 66 |
|
53 | 67 | def version(self, version=None): |
54 | | - if version is not None and isinstance(version, str): |
55 | | - self.defaultConfig['version'] = version |
56 | | - return self.defaultConfig['version'] |
57 | | - else: |
58 | | - return self.defaultConfig['version'] |
59 | | - |
60 | | - def path(self, path): |
61 | | - url_section = self.defaultConfig['path'] |
62 | | - if path in url_section: |
63 | | - return url_section[path] |
64 | | - else: |
65 | | - logging.error("{0} is invalid endpoint path".format(path)) |
66 | | - raise Exception('Invalid endpoint!!, {0} is invalid endpoint path, Path can be found among {1}' |
67 | | - .format(path, url_section.keys())) |
68 | 68 |
|
69 | | - @property |
70 | | - def default_endpoint(self): |
71 | | - endpoint_url = "{0}/{1}/{2}".format(self.defaultConfig["protocol"], self.host(), self.version()) |
72 | | - return endpoint_url |
| 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 |
73 | 76 |
|
74 | | - def endpoint(self, path): |
75 | | - url = self.path(path) |
76 | | - if url is not None and isinstance(url, str): |
77 | | - url = "{0}/{1}/{2}/{3}".format(self.defaultConfig["protocol"], self.host(), self.version(), url) |
78 | | - logging.info('endpoint is :: {0} '.format(url)) |
| 77 | + Example: The API version (in our case, 'v3') can be found in the URL, e.g. |
79 | 78 |
|
80 | | - return url |
| 79 | + >>> config = Config() |
| 80 | + >>> config.version = 'v3' |
81 | 81 |
|
| 82 | + """ |
| 83 | + if version is not None and isinstance(version, str): |
| 84 | + self.default['version'] = version |
82 | 85 |
|
83 | | -# config = Config() |
84 | | -# config.host("stag-cdn.contentstack.io") |
85 | | -# result_url = config.endpoint('assets') |
86 | | -# print(result_url) |
| 86 | + return self |
87 | 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