Skip to content

Commit 8fc62bc

Browse files
asset oder - asc/desc added
1 parent 438046d commit 8fc62bc

File tree

4 files changed

+48
-75
lines changed

4 files changed

+48
-75
lines changed

contentstack/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
__package__ = 'contentstack'
77
__endpoint__ = 'cdn.contentstack.io'
88

9-
# from .stack import Stack
10-
# from .stack import Stack
119
from .entry import Entry
1210
from .asset import Asset
1311
from .asset_library import AssetLibrary
@@ -16,7 +14,6 @@
1614
from .errors import Error, ConfigError
1715
from .http_connection import HTTPConnection
1816

19-
2017
# Set a default logger to prevent "No handler found" warnings
2118
import logging
2219
try:

contentstack/asset_library.py

Lines changed: 36 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
"""
2525

2626

27+
class OrderType(object):
28+
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+
2738
class AssetLibrary:
2839

2940
"""
@@ -35,23 +46,27 @@ class AssetLibrary:
3546
"""
3647

3748
def __init__(self):
38-
self.count = 0
49+
50+
self.__count = 0
3951
self.__stack_instance = None
4052
self.__http_request = None
4153
self.__stack_headers = {}
4254
self.__query_params = {}
4355

4456
def instance(self, stack_instance):
57+
4558
self.__stack_instance = stack_instance
4659
self.__stack_headers.update(self.__stack_instance.headers)
4760
self.__http_request = self.__stack_instance.http_request
4861

4962
def set_header(self, key: str, value):
63+
5064
if key is not None and value is not None:
5165
self.__stack_headers[key] = value
5266
return self
5367

5468
def headers(self, headers: dict):
69+
5570
if headers is not None and len(headers) > 0 and isinstance(headers, dict):
5671
self.__stack_headers = headers
5772
if 'environment' in self.__stack_headers:
@@ -60,6 +75,7 @@ def headers(self, headers: dict):
6075
return self
6176

6277
def remove_header(self, key):
78+
6379
if key is not None:
6480
if key in self.__stack_headers:
6581
self.__stack_headers.pop(key)
@@ -74,79 +90,31 @@ def include_relative_url(self):
7490
return self
7591

7692
def get_count(self) -> int:
77-
return self.count
78-
79-
# Color = enumerate(RED="ASCENDING", GREEN='DESCENDING')
80-
# [PENDING], Need to add
81-
# order_by = Enum('ORDER_BY', 'ASCENDING DESCENDING')
82-
# def sort(self, key: str, order_by):
83-
# if ORDER_.ASCENDING:
84-
# self.__post_params['asc'] = key
85-
# if ORDER_BY.DESCENDING:
86-
# self.__post_params['desc'] = key
87-
# return self.__post_params
88-
89-
def fetch_all(self):
90-
91-
import requests
92-
from urllib import parse
93-
from requests import Response
94-
from contentstack import Config
95-
from contentstack import Error
96-
from contentstack import Asset
97-
98-
error = None
99-
asset_url = Config().endpoint('assets')
100-
self.__stack_headers.update(self.header_agents())
101-
payload = parse.urlencode(query=self.__query_params, encoding='UTF-8')
102-
103-
try:
104-
response: Response = requests.get(asset_url, params=payload, headers=self.__stack_headers)
105-
list_asset: list[Asset] = []
106-
107-
if response.ok:
108-
109-
response: dict = response.json()['assets']
110-
111-
for asset in response:
112-
asset_instance = Asset()
113-
asset_resp: Asset = asset_instance.configure(response=asset)
114-
list_asset.append(asset_resp)
115-
116-
return list_asset
117-
else:
118-
error_dict = response.json()
119-
return Error().error(error_dict)
120-
121-
except requests.exceptions.RequestException as e:
122-
raise ConnectionError(e.response)
123-
124-
125-
@classmethod
126-
def header_agents(cls) -> dict:
93+
"""
94+
get_count returns the total size of content
95+
:return: count of content
96+
:rtype: int
97+
"""
98+
return self.__count
12799

128-
import contentstack
129-
import platform
100+
def sort(self, key: str, order_by: OrderType):
130101

131102
"""
132-
Contentstack-User-Agent header.
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
133106
"""
134-
header = {'sdk': dict(name=contentstack.__package__, version=contentstack.__version__)}
135-
os_name = platform.system()
107+
if order_by == 1:
108+
self.__query_params['asc'] = key
109+
else:
110+
self.__query_params['desc'] = key
136111

137-
if os_name == 'Darwin':
138-
os_name = 'macOS'
112+
return self.__query_params
139113

140-
elif not os_name or os_name == 'Java':
141-
os_name = None
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)
142118

143-
elif os_name and os_name not in ['macOS', 'Windows']:
144-
os_name = 'Linux'
145119

146-
header['os'] = {
147-
'name': os_name,
148-
'version': platform.release()
149-
}
150120

151-
local_headers = {'X-User-Agent': str(header), "Content-Type": 'application/json'}
152-
return local_headers

contentstack/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ def header_agents() -> dict:
7575

7676
local_headers = {'X-User-Agent': header, "Content-Type": 'application/json'}
7777
return local_headers
78+
79+
80+
81+

setup.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# !/usr/bin/env python
22
# distutils/setuptools install script.
33

4-
from setuptools import setup, find_packages
4+
import setuptools
55
import os
66
import re
77

88
ROOT = os.path.dirname(__file__)
99
VERSION_RE = re.compile(r'''__version__ = ['"]([0-9.]+)['"]''')
1010

11+
with open("README.md", "r") as fh:
12+
long_description = fh.read()
13+
1114

1215
def get_version():
1316
init = open(os.path.join(ROOT, 'contentstack', '__init__.py')).read()
@@ -26,12 +29,13 @@ def read(fname):
2629
'python-dateutil'
2730
]
2831

29-
setup(
32+
setuptools.setup(
3033

3134
name='contentstack',
3235
version=get_version(),
3336
py_modules=['contentstack'],
34-
packages=find_packages(exclude=['tests']),
37+
scripts=['dokr'],
38+
# packages=find_packages(exclude=['tests']),
3539
url='https://github.com/contentstack/contentstack-python.git',
3640
license='MIT License',
3741
author='Shailesh Mishra',
@@ -40,7 +44,7 @@ def read(fname):
4044
'Contentstack. The SDK uses Content Delivery APIs. ',
4145
install_requires=['requests', 'asset'],
4246
tests_require=['pytest'],
43-
long_description=read('README.rst'),
47+
long_description=read('README.md'),
4448
include_package_data=True,
4549

4650
classifiers=[

0 commit comments

Comments
 (0)