Skip to content

Commit ef45482

Browse files
committed
Removing config class.
1 parent 7ffbc34 commit ef45482

File tree

2 files changed

+47
-45
lines changed

2 files changed

+47
-45
lines changed

intercom/__init__.py

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,32 @@
3838
Intercom.app_api_key to use this client."
3939

4040

41-
class _Config(object):
42-
app_id = None
43-
app_api_key = None
44-
hostname = "api.intercom.io"
45-
protocol = "https"
46-
endpoints = None
47-
current_endpoint = None
48-
target_base_url = None
49-
endpoint_randomized_at = None
50-
51-
5241
class IntercomType(type): # noqa
5342

43+
_app_id = None
44+
_app_api_key = None
45+
_hostname = "api.intercom.io"
46+
_protocol = "https"
47+
_endpoints = None
48+
_current_endpoint = None
49+
_target_base_url = None
50+
_endpoint_randomized_at = None
51+
5452
@property
5553
def app_id(self):
56-
return self._config.app_id
54+
return self._app_id
5755

5856
@app_id.setter
5957
def app_id(self, value):
60-
self._config.app_id = value
58+
self._app_id = value
6159

6260
@property
6361
def app_api_key(self):
64-
return self._config.app_api_key
62+
return self._app_api_key
6563

6664
@app_api_key.setter
6765
def app_api_key(self, value):
68-
self._config.app_api_key = value
66+
self._app_api_key = value
6967

7068
@property
7169
def _auth(self):
@@ -88,63 +86,63 @@ def _alternative_random_endpoint(self):
8886
return endpoints[0]
8987

9088
@property
91-
def _target_base_url(self):
89+
def target_base_url(self):
9290
if None in [self.app_id, self.app_api_key]:
9391
raise ArgumentError('%s %s' % (
9492
CONFIGURATION_REQUIRED_TEXT, RELATED_DOCS_TEXT))
95-
if self._config.target_base_url is None:
93+
if self._target_base_url is None:
9694
basic_auth_part = '%s:%s@' % (self.app_id, self.app_api_key)
9795
if self.current_endpoint:
98-
self._config.target_base_url = re.sub(
96+
self._target_base_url = re.sub(
9997
r'(https?:\/\/)(.*)',
10098
'\g<1>%s\g<2>' % (basic_auth_part),
10199
self.current_endpoint)
102-
return self._config.target_base_url
100+
return self._target_base_url
103101

104102
@property
105103
def hostname(self):
106-
return self._config.hostname
104+
return self._hostname
107105

108106
@hostname.setter
109107
def hostname(self, value):
110-
self._config.hostname = value
108+
self._hostname = value
111109
self.current_endpoint = None
112110
self.endpoints = None
113111

114112
@property
115113
def protocol(self):
116-
return self._config.protocol
114+
return self._protocol
117115

118116
@protocol.setter
119117
def protocol(self, value):
120-
self._config.protocol = value
118+
self._protocol = value
121119
self.current_endpoint = None
122120
self.endpoints = None
123121

124122
@property
125123
def current_endpoint(self):
126124
now = time.mktime(datetime.utcnow().timetuple())
127-
expired = self._config.endpoint_randomized_at < (now - (60 * 5))
128-
if self._config.endpoint_randomized_at is None or expired:
129-
self._config.endpoint_randomized_at = now
130-
self.current_endpoint = self._random_endpoint
131-
return self._config.current_endpoint
125+
expired = self._endpoint_randomized_at < (now - (60 * 5))
126+
if self._endpoint_randomized_at is None or expired:
127+
self._endpoint_randomized_at = now
128+
self._current_endpoint = self._random_endpoint
129+
return self._current_endpoint
132130

133131
@current_endpoint.setter
134132
def current_endpoint(self, value):
135-
self._config.current_endpoint = value
136-
self._config.target_base_url = None
133+
self._current_endpoint = value
134+
self._target_base_url = None
137135

138136
@property
139137
def endpoints(self):
140-
if not self._config.endpoints:
138+
if not self._endpoints:
141139
return ['%s://%s' % (self.protocol, self.hostname)]
142140
else:
143-
return self._config.endpoints
141+
return self._endpoints
144142

145143
@endpoints.setter
146144
def endpoints(self, value):
147-
self._config.endpoints = value
145+
self._endpoints = value
148146
self.current_endpoint = self._random_endpoint
149147

150148
@SetterProperty
@@ -153,7 +151,6 @@ def endpoint(self, value):
153151

154152

155153
class Intercom(object):
156-
_config = _Config()
157154
_class_register = {}
158155
__metaclass__ = IntercomType
159156

@@ -165,18 +162,23 @@ def get_url(cls, path):
165162
url = cls.current_endpoint + path
166163
return url
167164

165+
@classmethod
166+
def request(cls, method, path, params):
167+
return Request.send_request_to_path(
168+
method, cls.get_url(path), cls._auth, params)
169+
168170
@classmethod
169171
def get(cls, path, **params):
170-
return Request.send_request_to_path('GET', cls.get_url(path), cls._auth, params)
172+
return cls.request('GET', path, params)
171173

172174
@classmethod
173175
def post(cls, path, **params):
174-
return Request.send_request_to_path('POST', cls.get_url(path), cls._auth, params)
176+
return cls.request('POST', path, params)
175177

176178
@classmethod
177179
def put(cls, path, **params):
178-
return Request.send_request_to_path('PUT', cls.get_url(path), cls._auth, params)
180+
return cls.request('PUT', path, params)
179181

180182
@classmethod
181183
def delete(cls, path, **params):
182-
return Request.send_request_to_path('DELETE', cls.get_url(path), cls._auth, params)
184+
return cls.request('DELETE', path, params)

tests/unit/intercom_spec.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ def it_raises_argumenterror_if_no_app_id_or_app_api_key_specified(self):
2525
self.intercom.app_id = None
2626
self.intercom.app_api_key = None
2727
with expect.to_raise_error(intercom.ArgumentError):
28-
self.intercom._target_base_url
28+
self.intercom.target_base_url
2929

3030
def it_returns_the_app_id_and_app_api_key_previously_set(self):
3131
expect(self.intercom.app_id) == 'abc123'
3232
expect(self.intercom.app_api_key) == 'super-secret-key'
3333

3434
def it_defaults_to_https_to_api_intercom_io(self):
35-
expect(self.intercom._target_base_url) == \
35+
expect(self.intercom.target_base_url) == \
3636
'https://abc123:super-secret-key@api.intercom.io'
3737

3838
class DescribeOverridingProtocolHostname:
@@ -56,13 +56,13 @@ def it_allows_overriding_of_the_endpoint_and_protocol(self):
5656

5757
def it_prefers_endpoints(self):
5858
self.intercom.endpoint = "https://localhost:7654"
59-
expect(self.intercom._target_base_url) == "https://abc123:super-secret-key@localhost:7654"
59+
expect(self.intercom.target_base_url) == "https://abc123:super-secret-key@localhost:7654"
6060

6161
# turn off the shuffle
6262
with mock.patch("random.shuffle") as mock_shuffle:
6363
mock_shuffle.return_value = ["http://example.com", "https://localhost:7654"]
6464
self.intercom.endpoints = ["http://example.com", "https://localhost:7654"]
65-
expect(self.intercom._target_base_url) == \
65+
expect(self.intercom.target_base_url) == \
6666
'http://abc123:super-secret-key@example.com'
6767

6868
def it_has_endpoints(self):
@@ -72,12 +72,12 @@ def it_has_endpoints(self):
7272

7373
def it_should_randomize_endpoints_if_last_checked_endpoint_is_gt_5_minutes_ago(self):
7474
now = time.mktime(datetime.utcnow().timetuple())
75-
self.intercom._config._endpoint_randomized_at = now
75+
self.intercom._endpoint_randomized_at = now
7676
self.intercom.endpoints = ["http://alternative"]
7777
self.intercom.current_endpoint = "http://start"
7878

79-
self.intercom._config.endpoint_randomized_at = now - 120
79+
self.intercom._endpoint_randomized_at = now - 120
8080
expect(self.intercom.current_endpoint) == "http://start"
81-
self.intercom._config.endpoint_randomized_at = now - 360
81+
self.intercom._endpoint_randomized_at = now - 360
8282
expect(self.intercom.current_endpoint) == "http://alternative"
8383

0 commit comments

Comments
 (0)