Skip to content

Commit 1ad040d

Browse files
Merge pull request #2 from contentstack/dev
Dev
2 parents 5a7f3e3 + 8fc62bc commit 1ad040d

File tree

16 files changed

+462
-291
lines changed

16 files changed

+462
-291
lines changed

.gitignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ wheels/
2424
.installed.cfg
2525
*.egg
2626
MANIFEST
27-
.DS_Store
28-
.DS_Store?
27+
*.DS_Store
28+
*.DS_Store?
2929

3030
# PyInstaller
3131
# Usually these files are written by a python script from a template
@@ -104,3 +104,7 @@ venv.bak/
104104

105105
# mypy
106106
.mypy_cache/
107+
108+
109+
.idea/
110+
.DS_Store?

CHANGELOGS.md

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

2-
## v1.0.0
2+
## v 0.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
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Contentstack
3+
Copyright (c) 2012 - 2019 Contentstack. All rights reserved.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

contentstack/__init__.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
1-
# -*- coding: utf-8 -*-
1+
""" contentstack python sdk. """
22

3-
# __init__.py
4-
# Contentstack
5-
# Created by Shailesh on 22/06/19.
6-
# Copyright (c) 2012 - 2019 Contentstack. All rights reserved.
7-
8-
# [MIT License] :: Permission is hereby granted, free of charge, to any person
9-
# obtaining a copy of this software and associated documentation files (the "Software"),
10-
# to deal in the Software without restriction, including without limitation the rights
11-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
# copies of the Software, and to permit persons to whom the Software is
13-
# furnished to do so, subject to the following conditions:
14-
15-
# The above copyright notice and this permission notice shall be included in all
16-
# copies or substantial portions of the Software.
17-
18-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
# SOFTWARE.
25-
26-
"""Export module packages."""
3+
__author__ = 'Shailesh Mishra'
4+
__status__ = 'debug'
5+
__version__ = '1.0.0'
6+
__package__ = 'contentstack'
7+
__endpoint__ = 'cdn.contentstack.io'
278

289
from .entry import Entry
2910
from .asset import Asset
3011
from .asset_library import AssetLibrary
3112
from .config import Config
32-
from .sync_stack import SyncStack
33-
from .query_result import QueryResult
3413
from .content_type import ContentType
35-
from .entry_model import EntryModel
36-
from .errors import HTTPError, ConfigError
37-
from .group import Group
38-
from .http_request import HTTPRequestConnection
14+
from .errors import Error, ConfigError
15+
from .http_connection import HTTPConnection
3916

40-
__author__ = 'contentstack - (www.github.con/contentstack)'
41-
__email__ = 'shailesh.mishra@contentstack.com'
42-
__version__ = '0.0.1'
43-
__endpoint__ = 'cdn.contentstack.io'
17+
# Set a default logger to prevent "No handler found" warnings
18+
import logging
19+
try:
20+
from logging import NullHandler
21+
except ImportError:
22+
class NullHandler(logging.Handler):
23+
def emit(self, record):
24+
pass
25+
26+
logging.getLogger(__name__).addHandler(NullHandler())

contentstack/asset_library.py

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,98 @@
2323
*
2424
"""
2525

26-
import logging
2726

27+
class OrderType(object):
2828

29-
class AssetLibrary():
29+
"""
30+
OrderType is used to choose one of the ascending and descending
31+
It returns either ascending or descending
32+
"""
33+
ASC, DESC = range(0, 2)
34+
35+
pass
36+
37+
38+
class AssetLibrary:
39+
40+
"""
41+
contentstack.asset_library
42+
~~~~~~~~~~~~~~~~~~
43+
This module implements the AssetLibrary class.
44+
API Reference: https://www.contentstack.com/docs/apis/content-delivery-api/#assets
45+
46+
"""
3047

3148
def __init__(self):
32-
logging.info('Asset initialised')
49+
50+
self.__count = 0
51+
self.__stack_instance = None
52+
self.__http_request = None
53+
self.__stack_headers = {}
54+
self.__query_params = {}
55+
56+
def instance(self, stack_instance):
57+
58+
self.__stack_instance = stack_instance
59+
self.__stack_headers.update(self.__stack_instance.headers)
60+
self.__http_request = self.__stack_instance.http_request
61+
62+
def set_header(self, key: str, value):
63+
64+
if key is not None and value is not None:
65+
self.__stack_headers[key] = value
66+
return self
67+
68+
def headers(self, headers: dict):
69+
70+
if headers is not None and len(headers) > 0 and isinstance(headers, dict):
71+
self.__stack_headers = headers
72+
if 'environment' in self.__stack_headers:
73+
env_value = self.__stack_headers['environment']
74+
self.__query_params["environment"] = env_value
75+
return self
76+
77+
def remove_header(self, key):
78+
79+
if key is not None:
80+
if key in self.__stack_headers:
81+
self.__stack_headers.pop(key)
82+
return self
83+
84+
def include_count(self):
85+
self.__query_params['include_count'] = 'true'
86+
return self
87+
88+
def include_relative_url(self):
89+
self.__query_params['relative_urls'] = 'true'
90+
return self
91+
92+
def get_count(self) -> int:
93+
"""
94+
get_count returns the total size of content
95+
:return: count of content
96+
:rtype: int
97+
"""
98+
return self.__count
99+
100+
def sort(self, key: str, order_by: OrderType):
101+
102+
"""
103+
:param key: provides key on which ASC/DESC need to apply.
104+
:param order_by: object option either "asc" or "desc"
105+
:return self , instance of AssetLibrary
106+
"""
107+
if order_by == 1:
108+
self.__query_params['asc'] = key
109+
else:
110+
self.__query_params['desc'] = key
111+
112+
return self.__query_params
113+
114+
def fetch_all(self):
115+
from contentstack import Config
116+
asset_url = Config().endpoint('assets')
117+
return self.__http_request.get_result(asset_url, self.__query_params, self.__stack_headers)
118+
119+
120+

contentstack/config.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,59 @@
2020
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
23-
*
24-
"""
2523
24+
"""
2625
import logging
2726

2827

29-
class Config(object):
28+
logging.basicConfig(filename='contentstack.log', format='%(asctime)s - %(message)s', level=logging.INFO)
29+
logging.getLogger("Config")
30+
31+
32+
class Config:
33+
34+
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"]
3048

31-
def __init__(self, host_url: str = 'cdn.contentstack.io'):
32-
self.host_url = host_url
33-
self.api_version: str = 'v3'
34-
self.http_protocol: str = 'https://'
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']
3555

36-
def set_host(self, host_url=None):
37-
logging.info("set host", host_url)
38-
self.host_url = host_url
39-
return self
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()))
4065

41-
def get_host(self):
42-
logging.info('getting host url', self.host_url)
43-
return self.host_url
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
4472

45-
def get_version(self):
46-
logging.info('getting api version', self.api_version)
47-
return self.api_version
4873

49-
def get_http_protocol(self):
50-
logging.info('get http protocol', self.http_protocol)
51-
return self.http_protocol
74+
config = Config()
75+
config.host("cdn.contentstack.io")
76+
result_url = config.endpoint('entries')
77+
print(result_url)
5278

53-
def get_endpoint(self, url_path):
54-
api_version: str = self.get_version()
55-
host_url = self.get_host()
56-
http_protocol = self.get_http_protocol()
57-
config_url = "{0}{1}/{2}/{3}".format(http_protocol, host_url, api_version, url_path)
58-
return config_url

contentstack/entry_model.py

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

0 commit comments

Comments
 (0)