Skip to content

Commit 42c36a4

Browse files
Merge pull request #5 from contentstack/dev
Dev
2 parents cf75ff7 + 7b4815d commit 42c36a4

File tree

8 files changed

+181
-161
lines changed

8 files changed

+181
-161
lines changed

CHANGELOGS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
## v 0.0.1
2+
##v0.0.1
33
Initial release of the python Content Delivery API SDK.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

contentstack/__init__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
""" contentstack python sdk. """
21

2+
# __init__
3+
# contentstack
4+
#
5+
# Created by Shailesh Mishra on 22/06/19.
6+
# Copyright © 2019 Contentstack. All rights reserved.
7+
8+
import warnings
39
__author__ = 'Shailesh Mishra'
410
__status__ = 'debug'
511
__version__ = '1.0.0'
@@ -10,17 +16,15 @@
1016
from .asset import Asset
1117
from .config import Config
1218
from .content_type import ContentType
13-
from .errors import Error, ConfigError
19+
from .errors import Error, ConfigError, FileModeWarning
1420
from .http_connection import HTTPConnection
1521

1622
# Set a default logger to prevent "No handler found" warnings
23+
# Set default logging handler to avoid "No handler found" warnings.
1724
import logging
18-
19-
try:
20-
from logging import NullHandler
21-
except ImportError:
22-
class NullHandler(logging.Handler):
23-
def emit(self, record):
24-
pass
25+
from logging import NullHandler
2526

2627
logging.getLogger(__name__).addHandler(NullHandler())
28+
29+
# FileModeWarnings go off per the default.
30+
warnings.simplefilter('default', FileModeWarning, append=True)

contentstack/config.py

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,49 @@
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+
257
import logging
8+
from enum import Enum
269

2710
logging.basicConfig(filename='cs.log', format='%(asctime)s - %(message)s', level=logging.INFO)
2811
logging.getLogger("Config")
2912

3013

14+
class ContentstackRegion(Enum):
15+
US = 'us'
16+
EU = 'eu'
17+
18+
3119
class Config(object):
20+
3221
"""
3322
All API paths are relative to this base URL, for example, /users actually means <scheme>://<host>/<basePath>/users.
34-
3523
"""
3624

3725
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+
"""
3841

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
4147

4248
def host(self, host):
4349

@@ -86,5 +92,19 @@ def version(self, version=None):
8692

8793
@property
8894
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"])

contentstack/errors.py

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
1-
"""
2-
* MIT License
3-
* Copyright (c) 2012 - 2019 Contentstack
4-
*
5-
* Permission is hereby granted, free of charge, to any person obtaining a copy
6-
* of this software and associated documentation files (the "Software"), to deal
7-
* in the Software without restriction, including without limitation the rights
8-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9-
* copies of the Software, and to permit persons to whom the Software is
10-
* furnished to do so, subject to the following conditions:
11-
*
12-
* The above copyright notice and this permission notice shall be included in all
13-
* copies or substantial portions of the Software.
14-
*
15-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
18-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
* SOFTWARE.
22-
*
23-
"""
1+
# Error
2+
# contentstack
3+
#
4+
# Created by Shailesh Mishra on 22/06/19.
5+
# Copyright © 2019 Contentstack. All rights reserved.
246

257

268
class Error:
27-
289
"""
2910
contentstack.error
3011
~~~~~~~~~~~~~~~~~~
@@ -34,7 +15,6 @@ class Error:
3415
"""
3516

3617
def __init__(self):
37-
3818
"""
3919
4020
"""
@@ -44,7 +24,6 @@ def __init__(self):
4424
self.__cause_err = str
4525

4626
def config(self, result: dict):
47-
4827
"""
4928
5029
:param result:
@@ -61,7 +40,6 @@ def config(self, result: dict):
6140

6241
@property
6342
def error_code(self):
64-
6543
"""
6644
It returns error code from the stack response
6745
:return: error_code as int
@@ -74,7 +52,6 @@ def error_code(self):
7452

7553
@property
7654
def error_message(self):
77-
7855
"""
7956
Returns error_message from the stack response
8057
:return: error_message
@@ -88,7 +65,6 @@ def error_message(self):
8865

8966
@property
9067
def error(self):
91-
9268
"""
9369
This returns error code and error_message in dict formats
9470
:return: error dict
@@ -101,7 +77,6 @@ def error(self):
10177

10278
@property
10379
def error_info(self) -> dict:
104-
10580
"""
10681
error information
10782
:return: error information
@@ -162,3 +137,13 @@ class ContentstackError(Exception):
162137

163138
class NotSupportedException(Exception):
164139
pass
140+
141+
142+
class RequestsWarning(Warning):
143+
"""Base warning for Requests."""
144+
pass
145+
146+
147+
class FileModeWarning(RequestsWarning, DeprecationWarning):
148+
"""A file was opened in text mode, but Requests determined its binary length."""
149+
pass

0 commit comments

Comments
 (0)