Skip to content

Commit 226abef

Browse files
Merge pull request #3 from contentstack/dev
Dev
2 parents 1ad040d + 500dcfd commit 226abef

File tree

5 files changed

+116
-180
lines changed

5 files changed

+116
-180
lines changed

contentstack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
from .entry import Entry
1010
from .asset import Asset
11-
from .asset_library import AssetLibrary
1211
from .config import Config
1312
from .content_type import ContentType
1413
from .errors import Error, ConfigError
1514
from .http_connection import HTTPConnection
1615

1716
# Set a default logger to prevent "No handler found" warnings
1817
import logging
18+
1919
try:
2020
from logging import NullHandler
2121
except ImportError:

contentstack/asset_library.py

Lines changed: 0 additions & 120 deletions
This file was deleted.

contentstack/config.py

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,71 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
24-
"""
24+
"""
2525
import logging
2626

2727

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)
2929
logging.getLogger("Config")
3030

3131

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+
"""
3337

3438
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
7239

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')
7360
74-
config = Config()
75-
config.host("cdn.contentstack.io")
76-
result_url = config.endpoint('entries')
77-
print(result_url)
61+
"""
7862

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

contentstack/errors.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
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
@@ -24,54 +24,91 @@
2424

2525

2626
class Error:
27+
2728
"""
2829
contentstack.error
2930
~~~~~~~~~~~~~~~~~~
3031
This module implements the Error class.
3132
API Reference: https://www.contentstack.com/docs/apis/content-delivery-api/#error
33+
3234
"""
3335

3436
def __init__(self):
37+
38+
"""
39+
40+
"""
3541
self.__error_dict = {}
3642
self.__error_code = str
3743
self.__msg = str
3844
self.__cause_err = str
3945

4046
def config(self, result: dict):
4147

48+
"""
49+
50+
:param result:
51+
:type result:
52+
:return:
53+
:rtype:
54+
"""
4255
if result is not None and len(result) > 0:
4356
self.__error_dict = result
4457
self.__error_code = self.__error_dict['error_code']
4558
self.__msg = self.__error_dict['error_message']
4659
self.__cause_err = self.__error_dict['errors']
47-
4860
return self
4961

5062
@property
5163
def error_code(self):
64+
5265
"""
66+
It returns error code from the stack response
5367
:return: error_code as int
68+
:rtype: int
69+
70+
Example. code = error.error_code
71+
5472
"""
5573
return self.__error_code
5674

5775
@property
5876
def error_message(self):
77+
5978
"""
79+
Returns error_message from the stack response
6080
:return: error_message
81+
:rtype: str
82+
83+
Example. error.error_message
84+
6185
"""
86+
6287
return self.__msg
6388

6489
@property
6590
def error(self):
91+
6692
"""
93+
This returns error code and error_message in dict formats
6794
:return: error dict
95+
:rtype: dict
96+
97+
Example. error = error.error
6898
"""
99+
69100
return self.__cause_err
70101

71102
@property
72103
def error_info(self) -> dict:
104+
73105
"""
74-
:return: dict, error information
106+
error information
107+
:return: error information
108+
:rtype: dict
109+
110+
Example. error.error_info
111+
75112
"""
76113
return self.__error_dict
77114

0 commit comments

Comments
 (0)