33from datetime import datetime
44from json import JSONEncoder
55from .errors import ArgumentError
6- from .errors import HttpError
6+ from .errors import HttpError # noqa
77from .lib .setter_property import SetterProperty
88
99import copy
1313import requests
1414import time
1515
16- __version__ = '2.0- alpha'
16+ __version__ = '2.0. alpha'
1717
1818
1919RELATED_DOCS_TEXT = "See https://github.com/jkeyes/python-intercom \
@@ -39,9 +39,109 @@ class _Config(object):
3939 endpoint_randomized_at = None
4040
4141
42+ class IntercomType (type ): # noqa
43+
44+ @property
45+ def app_id (self ):
46+ return self ._config .app_id
47+
48+ @app_id .setter
49+ def app_id (self , value ):
50+ self ._config .app_id = value
51+
52+ @property
53+ def app_api_key (self ):
54+ return self ._config .app_api_key
55+
56+ @app_api_key .setter
57+ def app_api_key (self , value ):
58+ self ._config .app_api_key = value
59+
60+ @property
61+ def _random_endpoint (self ):
62+ if self .endpoints :
63+ endpoints = copy .copy (self .endpoints )
64+ random .shuffle (endpoints )
65+ return endpoints [0 ]
66+
67+ @property
68+ def _alternative_random_endpoint (self ):
69+ endpoints = copy .copy (self .endpoints )
70+ if self .current_endpoint in endpoints :
71+ endpoints .remove (self .current_endpoint )
72+ random .shuffle (endpoints )
73+ if endpoints :
74+ return endpoints [0 ]
75+
76+ @property
77+ def _target_base_url (self ):
78+ if None in [self .app_id , self .app_api_key ]:
79+ raise ArgumentError ('%s %s' % (
80+ CONFIGURATION_REQUIRED_TEXT , RELATED_DOCS_TEXT ))
81+ if self ._config .target_base_url is None :
82+ basic_auth_part = '%s:%s@' % (self .app_id , self .app_api_key )
83+ if self .current_endpoint :
84+ self ._config .target_base_url = re .sub (
85+ r'(https?:\/\/)(.*)' ,
86+ '\g<1>%s\g<2>' % (basic_auth_part ),
87+ self .current_endpoint )
88+ return self ._config .target_base_url
89+
90+ @property
91+ def hostname (self ):
92+ return self ._config .hostname
93+
94+ @hostname .setter
95+ def hostname (self , value ):
96+ self ._config .hostname = value
97+ self .current_endpoint = None
98+ self .endpoints = None
99+
100+ @property
101+ def protocol (self ):
102+ return self ._config .protocol
103+
104+ @protocol .setter
105+ def protocol (self , value ):
106+ self ._config .protocol = value
107+ self .current_endpoint = None
108+ self .endpoints = None
109+
110+ @property
111+ def current_endpoint (self ):
112+ now = time .mktime (datetime .utcnow ().timetuple ())
113+ expired = self ._config .endpoint_randomized_at < (now - (60 * 5 ))
114+ if self ._config .endpoint_randomized_at is None or expired :
115+ self ._config .endpoint_randomized_at = now
116+ self .current_endpoint = self ._random_endpoint
117+ return self ._config .current_endpoint
118+
119+ @current_endpoint .setter
120+ def current_endpoint (self , value ):
121+ self ._config .current_endpoint = value
122+ self ._config .target_base_url = None
123+
124+ @property
125+ def endpoints (self ):
126+ if not self ._config .endpoints :
127+ return ['%s://%s' % (self .protocol , self .hostname )]
128+ else :
129+ return self ._config .endpoints
130+
131+ @endpoints .setter
132+ def endpoints (self , value ):
133+ self ._config .endpoints = value
134+ self .current_endpoint = self ._random_endpoint
135+
136+ @SetterProperty
137+ def endpoint (self , value ):
138+ self .endpoints = [value ]
139+
140+
42141class Intercom (object ):
43142 _config = _Config ()
44143 _class_register = {}
144+ __metaclass__ = IntercomType
45145
46146 @classmethod
47147 def send_request_to_path (cls , method , path , params = None ):
@@ -68,6 +168,7 @@ def send_request_to_path(cls, method, path, params=None):
68168 method , url , timeout = cls ._config .timeout ,
69169 auth = (Intercom .app_id , Intercom .app_api_key ), ** req_params )
70170
171+ print resp .status_code , resp .content
71172 if resp .content :
72173 return json .loads (resp .content )
73174
@@ -87,104 +188,6 @@ def put(cls, path, **params):
87188 def delete (cls , path , ** params ):
88189 return cls .send_request_to_path ('DELETE' , path , params )
89190
90- class __metaclass__ (type ):
91-
92- @property
93- def app_id (cls ):
94- return cls ._config .app_id
95-
96- @app_id .setter
97- def app_id (cls , value ):
98- cls ._config .app_id = value
99-
100- @property
101- def app_api_key (cls ):
102- return cls ._config .app_api_key
103-
104- @app_api_key .setter
105- def app_api_key (cls , value ):
106- cls ._config .app_api_key = value
107-
108- @property
109- def _random_endpoint (cls ):
110- if cls .endpoints :
111- endpoints = copy .copy (cls .endpoints )
112- random .shuffle (endpoints )
113- return endpoints [0 ]
114-
115- @property
116- def _alternative_random_endpoint (cls ):
117- endpoints = copy .copy (cls .endpoints )
118- if cls .current_endpoint in endpoints :
119- endpoints .remove (cls .current_endpoint )
120- random .shuffle (endpoints )
121- if endpoints :
122- return endpoints [0 ]
123-
124- @property
125- def _target_base_url (cls ):
126- if None in [cls .app_id , cls .app_api_key ]:
127- raise ArgumentError ('%s %s' % (
128- CONFIGURATION_REQUIRED_TEXT , RELATED_DOCS_TEXT ))
129- if cls ._config .target_base_url is None :
130- basic_auth_part = '%s:%s@' % (cls .app_id , cls .app_api_key )
131- if cls .current_endpoint :
132- cls ._config .target_base_url = re .sub (
133- r'(https?:\/\/)(.*)' ,
134- '\g<1>%s\g<2>' % (basic_auth_part ),
135- cls .current_endpoint )
136- return cls ._config .target_base_url
137-
138- @property
139- def hostname (cls ):
140- return cls ._config .hostname
141-
142- @hostname .setter
143- def hostname (cls , value ):
144- cls ._config .hostname = value
145- cls .current_endpoint = None
146- cls .endpoints = None
147-
148- @property
149- def protocol (cls ):
150- return cls ._config .protocol
151-
152- @protocol .setter
153- def protocol (cls , value ):
154- cls ._config .protocol = value
155- cls .current_endpoint = None
156- cls .endpoints = None
157-
158- @property
159- def current_endpoint (cls ):
160- now = time .mktime (datetime .utcnow ().timetuple ())
161- expired = cls ._config .endpoint_randomized_at < (now - (60 * 5 ))
162- if cls ._config .endpoint_randomized_at is None or expired :
163- cls ._config .endpoint_randomized_at = now
164- cls .current_endpoint = cls ._random_endpoint
165- return cls ._config .current_endpoint
166-
167- @current_endpoint .setter
168- def current_endpoint (cls , value ):
169- cls ._config .current_endpoint = value
170- cls ._config .target_base_url = None
171-
172- @property
173- def endpoints (cls ):
174- if not cls ._config .endpoints :
175- return ['%s://%s' % (cls .protocol , cls .hostname )]
176- else :
177- return cls ._config .endpoints
178-
179- @endpoints .setter
180- def endpoints (cls , value ):
181- cls ._config .endpoints = value
182- cls .current_endpoint = cls ._random_endpoint
183-
184- @SetterProperty
185- def endpoint (cls , value ):
186- cls .endpoints = [value ]
187-
188191
189192class ResourceEncoder (JSONEncoder ):
190193 def default (self , o ):
0 commit comments