MCPcopy Index your code
hub / github.com/ryanmcgrath/twython / _request

Method _request

twython/api.py:139–217  ·  view source on GitHub ↗

Internal request method

(self, url, method='GET', params=None, api_call=None, json_encoded=False)

Source from the content-addressed store, hash-verified

137 return '<Twython: %s>' % (self.app_key)
138
139 def _request(self, url, method='GET', params=None, api_call=None, json_encoded=False):
140 """Internal request method"""
141 method = method.lower()
142 params = params or {}
143
144 func = getattr(self.client, method)
145 if isinstance(params, dict) and json_encoded is False:
146 params, files = _transparent_params(params)
147 else:
148 params = params
149 files = list()
150
151 requests_args = {}
152 for k, v in self.client_args.items():
153 # Maybe this should be set as a class variable and only done once?
154 if k in ('timeout', 'allow_redirects', 'stream', 'verify'):
155 requests_args[k] = v
156
157 if method == 'get' or method == 'delete':
158 requests_args['params'] = params
159 else:
160 # Check for json_encoded so we will sent params as "data" or "json"
161 if json_encoded:
162 data_key = 'json'
163 else:
164 data_key = 'data'
165 requests_args.update({
166 data_key: params,
167 'files': files,
168 })
169 try:
170 response = func(url, **requests_args)
171 except requests.RequestException as e:
172 raise TwythonError(str(e))
173
174 # create stash for last function intel
175 self._last_call = {
176 'api_call': api_call,
177 'api_error': None,
178 'cookies': response.cookies,
179 'headers': response.headers,
180 'status_code': response.status_code,
181 'url': response.url,
182 'content': response.text,
183 }
184
185 # greater than 304 (not modified) is an error
186 if response.status_code > 304:
187 error_message = self._get_error_message(response)
188 self._last_call['api_error'] = error_message
189
190 ExceptionType = TwythonError
191 if response.status_code == 429:
192 # Twitter API 1.1, always return 429 when
193 # rate limit is exceeded
194 ExceptionType = TwythonRateLimitError
195 elif response.status_code == 401 or 'Bad Authentication data' \
196 in error_message:

Callers 1

requestMethod · 0.95

Calls 4

_get_error_messageMethod · 0.95
_transparent_paramsFunction · 0.85
TwythonErrorClass · 0.85
getMethod · 0.80

Tested by

no test coverage detected