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

Method _request

twython/streaming/api.py:97–163  ·  view source on GitHub ↗

Internal stream request handling

(self, url, method='GET', params=None)

Source from the content-addressed store, hash-verified

95 self.chunk_size = chunk_size
96
97 def _request(self, url, method='GET', params=None):
98 """Internal stream request handling"""
99 self.connected = True
100 retry_counter = 0
101
102 method = method.lower()
103 func = getattr(self.client, method)
104 params, _ = _transparent_params(params)
105
106 def _send(retry_counter):
107 requests_args = {}
108 for k, v in self.client_args.items():
109 # Maybe this should be set as a class
110 # variable and only done once?
111 if k in ('timeout', 'allow_redirects', 'verify'):
112 requests_args[k] = v
113
114 while self.connected:
115 try:
116 if method == 'get':
117 requests_args['params'] = params
118 else:
119 requests_args['data'] = params
120
121 response = func(url, **requests_args)
122 except requests.exceptions.Timeout:
123 self.on_timeout()
124 else:
125 if response.status_code != 200:
126 self.on_error(response.status_code, response.content, response.headers)
127
128 if self.retry_count and \
129 (self.retry_count - retry_counter) > 0:
130 time.sleep(self.retry_in)
131 retry_counter += 1
132 _send(retry_counter)
133
134 return response
135
136 while self.connected:
137 response = _send(retry_counter)
138
139 for line in response.iter_lines(self.chunk_size):
140 if not self.connected:
141 break
142 if line:
143 try:
144 if is_py3:
145 line = line.decode('utf-8')
146 data = json.loads(line)
147 except ValueError: # pragma: no cover
148 self.on_error(response.status_code,
149 'Unable to decode response, \
150 not valid JSON.')
151 else:
152 if self.on_success(data): # pragma: no cover
153 for message_type in self.handlers:
154 if message_type in data:

Callers 4

filterMethod · 0.45
sampleMethod · 0.45
firehoseMethod · 0.45
dynamic_filterMethod · 0.45

Calls 4

on_errorMethod · 0.95
on_successMethod · 0.95
_transparent_paramsFunction · 0.85
getMethod · 0.80

Tested by

no test coverage detected