Skip to content

Commit 0e2735e

Browse files
author
Clark Perkins
committed
Updated how the request method gets called
1 parent 7c83a17 commit 0e2735e

File tree

1 file changed

+83
-71
lines changed

1 file changed

+83
-71
lines changed

stackdio/client/http.py

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class Request(object):
7373
def __init__(self, dfunc=None, rfunc=None, quiet=False):
7474
super(Request, self).__init__()
7575

76+
self.obj = None
77+
7678
self.data_func = dfunc
7779
self.response_func = rfunc
7880

@@ -91,78 +93,88 @@ def data(self, dfunc):
9193
def response(self, rfunc):
9294
return type(self)(self.data_func, rfunc, self.quiet)
9395

94-
# Here's how the request actually happens
96+
def __repr__(self):
97+
if self.obj:
98+
return '<bound method HTTP {0} request for \'/api/{1}\' on {2}>'.format(method, path, repr(self.obj))
99+
else:
100+
return super(Request, self).__repr__()
101+
102+
# We need this so we can save the client object as an attribute, and then it can be used
103+
# in __call__
95104
def __get__(self, obj, objtype=None):
96-
def do_request(*args, **kwargs):
97-
none_on_404 = kwargs.pop('none_on_404', False)
98-
raise_for_status = kwargs.pop('raise_for_status', True)
99-
100-
# Get what locals() would return directly after calling
101-
# 'func' with the given args and kwargs
102-
future_locals = getcallargs(self.data_func, *((obj,) + args), **kwargs)
103-
104-
# Build the variable we'll inject
105-
url = '{url}{path}'.format(
106-
url=obj.url,
107-
path=path.format(**future_locals)
108-
)
109-
110-
if not self.quiet:
111-
self._http_log.info("{0}: {1}".format(method, url))
112-
113-
data = None
114-
if self.data_func:
115-
data = json.dumps(self.data_func(obj, *args, **kwargs))
116-
117-
result = requests.request(method,
118-
url,
119-
data=data,
120-
auth=obj._http_options['auth'],
121-
headers=self.headers,
122-
params=kwargs,
123-
verify=obj._http_options['verify'])
124-
125-
# Handle special conditions
126-
if none_on_404 and result.status_code == 404:
127-
return None
128-
129-
elif result.status_code == 204:
130-
return None
131-
132-
elif raise_for_status:
133-
try:
134-
result.raise_for_status()
135-
except Exception:
136-
logger.error(result.text)
137-
raise
138-
139-
if jsonify:
140-
response = result.json()
141-
else:
142-
response = result.text
143-
144-
if method == 'GET' and paginate and jsonify:
145-
res = response['results']
146-
147-
next_url = response['next']
148-
149-
while next_url:
150-
next_page = requests.request(method,
151-
next_url,
152-
data=data,
153-
auth=obj._http_options['auth'],
154-
headers=self.headers,
155-
params=kwargs,
156-
verify=obj._http_options['verify']).json()
157-
res.extend(next_page['results'])
158-
next_url = next_page['next']
159-
160-
response = res
161-
162-
# now process the result
163-
return self.response_func(obj, response)
164-
165-
return do_request
105+
self.obj = obj
106+
assert issubclass(objtype, HttpMixin)
107+
return self
108+
109+
# Here's how the request actually happens
110+
def __call__(self, *args, **kwargs):
111+
none_on_404 = kwargs.pop('none_on_404', False)
112+
raise_for_status = kwargs.pop('raise_for_status', True)
113+
114+
# Get what locals() would return directly after calling
115+
# 'func' with the given args and kwargs
116+
future_locals = getcallargs(self.data_func, *((self.obj,) + args), **kwargs)
117+
118+
# Build the variable we'll inject
119+
url = '{url}{path}'.format(
120+
url=self.obj.url,
121+
path=path.format(**future_locals)
122+
)
123+
124+
if not self.quiet:
125+
self._http_log.info("{0}: {1}".format(method, url))
126+
127+
data = None
128+
if self.data_func:
129+
data = json.dumps(self.data_func(self.obj, *args, **kwargs))
130+
131+
result = requests.request(method,
132+
url,
133+
data=data,
134+
auth=self.obj._http_options['auth'],
135+
headers=self.headers,
136+
params=kwargs,
137+
verify=self.obj._http_options['verify'])
138+
139+
# Handle special conditions
140+
if none_on_404 and result.status_code == 404:
141+
return None
142+
143+
elif result.status_code == 204:
144+
return None
145+
146+
elif raise_for_status:
147+
try:
148+
result.raise_for_status()
149+
except Exception:
150+
logger.error(result.text)
151+
raise
152+
153+
if jsonify:
154+
response = result.json()
155+
else:
156+
response = result.text
157+
158+
if method == 'GET' and paginate and jsonify:
159+
res = response['results']
160+
161+
next_url = response['next']
162+
163+
while next_url:
164+
next_page = requests.request(method,
165+
next_url,
166+
data=data,
167+
auth=self.obj._http_options['auth'],
168+
headers=self.headers,
169+
params=kwargs,
170+
verify=self.obj._http_options['verify']).json()
171+
res.extend(next_page['results'])
172+
next_url = next_page['next']
173+
174+
response = res
175+
176+
# now process the result
177+
return self.response_func(self.obj, response)
166178

167179
return Request
168180

0 commit comments

Comments
 (0)