|
1 | | -""" |
2 | | - * MIT License |
3 | | - * |
4 | | - * Copyright (c) 2012 - 2019 Contentstack |
5 | | - * |
6 | | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | - * of this software and associated documentation files (the "Software"), to deal |
8 | | - * in the Software without restriction, including without limitation the rights |
9 | | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | - * copies of the Software, and to permit persons to whom the Software is |
11 | | - * furnished to do so, subject to the following conditions: |
12 | | - * |
13 | | - * The above copyright notice and this permission notice shall be included in all |
14 | | - * copies or substantial portions of the Software. |
15 | | - * |
16 | | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | - * SOFTWARE. |
23 | | -
|
24 | | -""" |
| 1 | +# Config |
| 2 | +# contentstack |
| 3 | +# |
| 4 | +# Created by Shailesh Mishra on 22/06/19. |
| 5 | +# Copyright © 2019 Contentstack. All rights reserved. |
| 6 | + |
25 | 7 | import logging |
| 8 | +from enum import Enum |
26 | 9 |
|
27 | 10 | logging.basicConfig(filename='cs.log', format='%(asctime)s - %(message)s', level=logging.INFO) |
28 | 11 | logging.getLogger("Config") |
29 | 12 |
|
30 | 13 |
|
| 14 | +class ContentstackRegion(Enum): |
| 15 | + US = 'us' |
| 16 | + EU = 'eu' |
| 17 | + |
| 18 | + |
31 | 19 | class Config(object): |
| 20 | + |
32 | 21 | """ |
33 | 22 | All API paths are relative to this base URL, for example, /users actually means <scheme>://<host>/<basePath>/users. |
34 | | -
|
35 | 23 | """ |
36 | 24 |
|
37 | 25 | def __init__(self): |
| 26 | + self.default = dict(protocol="https", region=ContentstackRegion.US, host="cdn.contentstack.io", version="v3") |
| 27 | + |
| 28 | + def region(self, region=ContentstackRegion.US): |
| 29 | + |
| 30 | + """ |
| 31 | + The base URL for Content Delivery API is cdn.contentstack.io. |
| 32 | + default region is for ContentstackRegion is US |
| 33 | +
|
| 34 | + :param region: ContentstackRegion |
| 35 | + :return: self |
| 36 | +
|
| 37 | + Example: |
| 38 | + >>> config = Config().region(region=ContentstackRegion.US) |
| 39 | +
|
| 40 | + """ |
38 | 41 |
|
39 | | - # It initialises the Config with the default endpoint |
40 | | - self.default = dict(protocol="https", region="us", host="cdn.contentstack.io", port=443, version="v3") |
| 42 | + if region is not None and isinstance(region, ContentstackRegion): |
| 43 | + self.default['region'] = region |
| 44 | + else: |
| 45 | + raise ValueError('Kindly provide a valid argument') |
| 46 | + return self |
41 | 47 |
|
42 | 48 | def host(self, host): |
43 | 49 |
|
@@ -86,5 +92,19 @@ def version(self, version=None): |
86 | 92 |
|
87 | 93 | @property |
88 | 94 | def endpoint(self): |
89 | | - url = "{0}://{1}/{2}".format(self.default["protocol"], self.default["host"], self.default["version"]) |
90 | | - return url |
| 95 | + return self.__get_url() |
| 96 | + |
| 97 | + def __get_url(self): |
| 98 | + host = self.default["host"] |
| 99 | + if self.default['region'] is not ContentstackRegion.US: |
| 100 | + |
| 101 | + if self.default["host"] == 'cdn.contentstack.io': |
| 102 | + # update the host to .com |
| 103 | + self.default["host"] = 'cdn.contentstack.com' |
| 104 | + else: |
| 105 | + # Find the regional value |
| 106 | + regional_host = str(self.default['region'].value) |
| 107 | + # Attach region to the host |
| 108 | + host = '{}-{}'.format(regional_host, self.default["host"]) |
| 109 | + |
| 110 | + return "{0}://{1}/{2}".format(self.default["protocol"], host, self.default["version"]) |
0 commit comments