MCPcopy Create free account
hub / github.com/ccxt/ccxt / fetch

Method fetch

python/ccxt/async_support/base/exchange.py:170–302  ·  view source on GitHub ↗

Perform a HTTP request and return decoded JSON data

(self, url, method='GET', headers=None, body=None)

Source from the content-addressed store, hash-verified

168 self.socks_proxy_sessions = None
169
170 async def fetch(self, url, method='GET', headers=None, body=None):
171 """Perform a HTTP request and return decoded JSON data"""
172
173 # ##### PROXY & HEADERS #####
174 request_headers = self.prepare_request_headers(headers)
175 self.last_request_headers = request_headers
176 # proxy-url
177 proxyUrl = self.check_proxy_url_settings(url, method, headers, body)
178 if proxyUrl is not None:
179 request_headers.update({'Origin': self.origin})
180 url = proxyUrl + self.url_encoder_for_proxy_url(url)
181 # proxy agents
182 final_proxy = None # set default
183 proxy_session = None
184 httpProxy, httpsProxy, socksProxy = self.check_proxy_settings(url, method, headers, body)
185 if httpProxy:
186 final_proxy = httpProxy
187 elif httpsProxy:
188 final_proxy = httpsProxy
189 elif socksProxy:
190 if SocksProxyConnector is None:
191 raise NotSupported(self.id + ' - to use SOCKS proxy with ccxt, you need "aiohttp_socks" module that can be installed by "pip install aiohttp_socks"')
192 # override session
193 if (self.socks_proxy_sessions is None):
194 self.socks_proxy_sessions = {}
195 if (socksProxy not in self.socks_proxy_sessions):
196 # Create our SSL context object with our CA cert file
197 self.open() # ensure `asyncio_loop` is set
198 proxy_session = self.get_socks_proxy_session(socksProxy)
199 # add aiohttp_proxy for python as exclusion
200 elif self.aiohttp_proxy:
201 final_proxy = self.aiohttp_proxy
202
203 proxyAgentSet = final_proxy is not None or socksProxy is not None
204 self.check_conflicting_proxies(proxyAgentSet, proxyUrl)
205
206 # avoid old proxies mixing
207 if (self.aiohttp_proxy is not None) and (proxyUrl is not None or httpProxy is not None or httpsProxy is not None or socksProxy is not None):
208 raise NotSupported(self.id + ' you have set multiple proxies, please use one or another')
209
210 # log
211 if self.verbose:
212 self.log("\nfetch Request:", self.id, method, url, "RequestHeaders:", request_headers, "RequestBody:", body)
213 self.logger.debug("%s %s, Request: %s %s", method, url, headers, body)
214 # end of proxies & headers
215
216 request_body = body
217 # check content-type is multipart/form-data for lighter
218 has_multipart = False
219 content_type_key = None
220 for k, v in request_headers.items():
221 lk = k.lower()
222 if lk == 'content-type':
223 if v == 'multipart/form-data':
224 content_type_key = k
225 has_multipart = True
226 data = aiohttp.FormData()
227 # TODO: attach file?

Callers 1

fetch2Method · 0.95

Calls 15

check_proxy_settingsMethod · 0.95
openMethod · 0.95
logMethod · 0.95
on_rest_responseMethod · 0.95
parse_jsonMethod · 0.95
handle_errorsMethod · 0.95

Tested by

no test coverage detected