Skip to content

Commit 161a47d

Browse files
code refactored
1 parent 068c76a commit 161a47d

File tree

7 files changed

+82
-97
lines changed

7 files changed

+82
-97
lines changed

contentstack/HTTPConnection.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
11
import logging
2-
#import requests
3-
#from requests.auth import HTTPDigestAuth
2+
import requests
3+
from requests.auth import HTTPDigestAuth
44
import json
5-
import config
65
import urllib.parse
6+
from contentstack import config
77

88

99
class HTTPConnection:
1010

1111
def __init__(self, local_headers, query):
12-
self.__local_headers = {}
13-
self.__query = {}
12+
self.__local_headers = local_headers
1413
self.__local_headers = local_headers
1514
self.__query = query
1615

17-
1816
def get_query_request(self):
1917
return self.__http_request()
2018

21-
2219
def get_entry_request(self):
2320
return self.__http_request()
2421

25-
2622
def get_stack_request(self):
2723
return self.__http_request()
2824

29-
3025
def get_content_type_req(self):
3126
return self.__http_request()
3227

33-
3428
def get_group_request(self):
3529
return self.__http_request()
3630

37-
3831
def __http_request(self):
39-
40-
__config = config.Config()
32+
4133
encoded_query = urllib.parse.urlencode(self.__query)
42-
self.__local_headers.update({'Content-Type': 'application/json',
43-
'X-User-Agent': __config.SDK_VERSION })
44-
#response = requests.post(__config.get_host(), encoded_query, self.__local_headers)
45-
46-
#print(response.json)
34+
self.__local_headers['Content-Type'] ='application/json'
35+
self.__local_headers['X-User-Agent'] = config.Config.SDK_VERSION
36+
response = requests.post(config.Config.get_host(), encoded_query, self.__local_headers)
37+
38+
print(response.json)
4739

4840

41+
connection: HTTPConnection = HTTPConnection(local_headers={'dwd','wdq'},query={'dwwd':'dwdwq'})
42+
connection.get_entry_request()

contentstack/asser_model.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@
2626
import logging
2727

2828

29-
class AssetModel():
30-
logging.info("Asset Model")
29+
class AssetModel:
3130

3231
def __init__(self):
33-
logging.info('AssetModel initialised')
32+
print(AssetModel, 'is initialised')
33+
34+
35+
36+
37+
38+
asser_model = AssetModel()

contentstack/asset_library.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828

2929
class AssetLibrary():
30-
logging.info("Asset library")
3130

3231
def __init__(self):
33-
logging.info('Asset initialised')
32+
logging.info('Asset initialised')

contentstack/config.py

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,38 @@
2525

2626
import logging
2727

28-
class Config(object):
29-
30-
def __init__(self):
31-
self.host_url = 'cdn.contentstack.io'
32-
self.api_version = 'v3'
33-
self.environment = None
34-
self.SDK_VERSION = "0.0.1"
35-
self.SDK_NAME = 'contentstack-python'
36-
37-
38-
def set_host(self, host_url= 'cdn.contentstack.io'):
39-
logging.info("set host")
40-
if host_url != None:
41-
self.url = host_url
42-
return self
43-
44-
45-
def set_environment(self, environment):
46-
logging.info("set environment")
47-
if environment != None:
48-
self.environment = environment
49-
return self
50-
51-
def get_host(self):
52-
logging.info('getting host url')
53-
return self.host_url
54-
55-
56-
def get_version(self):
57-
logging.info('getting api version')
58-
return self.api_version
5928

29+
class Config(object):
6030

61-
def get_environment(self):
62-
logging.info('get invironment')
63-
return self.environment
31+
SDK_VERSION = 'contentstack-python[0.0.1]'
32+
33+
def __init__(self):
34+
self.host_url = 'cdn.contentstack.io'
35+
self.api_version = 'v3'
36+
self.environment = None
37+
self.SDK_VERSION = "0.0.1"
38+
self.SDK_NAME = 'contentstack-python'
39+
40+
def set_host(self, host_url='cdn.contentstack.io'):
41+
logging.info("set host")
42+
if host_url != None:
43+
self.url = host_url
44+
return self
45+
46+
def set_environment(self, environment):
47+
logging.info("set environment")
48+
if environment != None:
49+
self.environment = environment
50+
return self
51+
52+
def get_host(self):
53+
logging.info('getting host url')
54+
return self.host_url
55+
56+
def get_version(self):
57+
logging.info('getting api version')
58+
return self.api_version
59+
60+
def get_environment(self):
61+
logging.info('get invironment')
62+
return self.environment

contentstack/contentstack.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,33 @@
2626

2727
import config
2828
import logging
29-
import stack
30-
from contentstack import stack
3129

3230

3331
class Contentstack(object):
3432

3533
def stack(self, api_key, access_token, environment, config):
3634
logging.info("Initicalised with configs")
37-
35+
3836
if api_key is None:
3937
logging.error("API Key can not be empty")
4038
raise AssertionError("API Key can not be empty")
41-
39+
4240
if access_token is None:
4341
logging.error("Access Token can not be empty")
4442
raise AssertionError("Access Token can not be empty")
45-
43+
4644
if environment is None:
4745
logging.error("Environment can not be empty")
4846
raise AssertionError("Environment can not be empty")
49-
47+
5048
if config is None:
5149
logging.error("congig can not be empty")
5250
raise AssertionError("Environment can not be empty")
53-
51+
5452
else:
5553
logging.info("stack initialisation attempted with config")
56-
return_stack = stack(api_key, access_token, environment)
57-
return return_stack
58-
59-
54+
# return_stack = stack(api_key, access_token, environment)
55+
#return return_stack
6056

6157

6258

contentstack/entry_model.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
21
class EntryModel:
3-
2+
43
def __init__(self, result):
54
self.result = result
65
self.entry_uid = None
@@ -13,9 +12,7 @@ def __init__(self, result):
1312
self._metadata = dict
1413
self.tags = dict
1514

16-
1715
def get_uid(self):
18-
if self.result != None :
19-
if 'uid' in self.result :
16+
if self.result != None:
17+
if 'uid' in self.result:
2018
self.result['uid'] = 'uid'
21-

contentstack/errors.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,29 @@
2323
*
2424
"""
2525

26-
2726
import logging
2827

28+
2929
class HTTPError(Exception):
30-
3130
errors_str = {
32-
33-
'error_invalid_json' : "Please provide valid JSON.",
34-
'error_message_stack_api_key_is_null' : "Stack api key can not be null.",
35-
'error_form_name' : "Please set contentType name.",
36-
'error_stack_access_token_is_null' : "Access token can not be null.",
37-
'error_stack_environment_is_null' : "Environment can not be null.",
38-
'Error_Connection_Error' : "Connection error",
39-
'Error_Auth_Failure_Error' : "Authentication Not present.",
40-
'Error_Parse_Error' : "Parsing Error.",
41-
'Error_Server_Error' : "Server interaction went wrong, Please try again.",
42-
'Error_Default' : "Oops! Something went wrong. Please try again.",
43-
'Error_No_Network' : "Network not available.",
44-
'Error_Called_Default_Method' : "You must called Contentstack.stack() first",
45-
'Error_Query_Filter_Exception' : "Please provide valid params."
31+
32+
'error_invalid_json': "Please provide valid JSON.",
33+
'error_message_stack_api_key_is_null': "Stack api key can not be null.",
34+
'error_form_name': "Please set contentType name.",
35+
'error_stack_access_token_is_null': "Access token can not be null.",
36+
'error_stack_environment_is_null': "Environment can not be null.",
37+
'Error_Connection_Error': "Connection error",
38+
'Error_Auth_Failure_Error': "Authentication Not present.",
39+
'Error_Parse_Error': "Parsing Error.",
40+
'Error_Server_Error': "Server interaction went wrong, Please try again.",
41+
'Error_Default': "Oops! Something went wrong. Please try again.",
42+
'Error_No_Network': "Network not available.",
43+
'Error_Called_Default_Method': "You must called Contentstack.stack() first",
44+
'Error_Query_Filter_Exception': "Please provide valid params."
4645
}
4746

48-
def __init__(self):
4947

48+
def __init__(self):
5049
errors = {
5150

5251
400: "The request was incorrect or corrupted.",
@@ -61,17 +60,13 @@ def __init__(self):
6160
504: "A server did not receive a timely response from another server that it was accessing while attempting to load the web page or fill another request by the browser."
6261
}
6362

64-
6563
def get_error(self, response):
6664
print('Error')
6765

68-
69-
def set_logging_config(self,level):
70-
print('level '+level)
66+
def set_logging_config(self, level):
67+
print('level ' + level)
7168

7269

7370
class ConfigError(Exception):
7471
"""Configuration Error Class"""
7572
pass
76-
77-

0 commit comments

Comments
 (0)