client.py
This file
Path
httpcore/client.py
246 lines · 6.6 KiB · Python
Last changed by
Truckin' onTom Christiebefore adoption6b60d95
History of this fileTake it
Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 1–17why, one step back1import typing2from types import TracebackType34from .adapters.authentication import AuthenticationAdapter5from .adapters.cookies import CookieAdapter6from .adapters.environment import EnvironmentAdapter7from .adapters.redirects import RedirectAdapter8from .config import (9 DEFAULT_MAX_REDIRECTS,10 DEFAULT_POOL_LIMITS,11 DEFAULT_SSL_CONFIG,12 DEFAULT_TIMEOUT_CONFIG,13 PoolLimits,14 SSLConfig,15 TimeoutConfig,16)17from .dispatch.connection_pool import ConnectionPoolPrefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 18why, one step back18from .models import URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypesMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 19–25why, one step back192021class Client:22 def __init__(23 self,24 ssl: SSLConfig = DEFAULT_SSL_CONFIG,25 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,Truckin' onTom Christiebefore adoption6b60d95
line 26why, one step back26 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 27–28why, one step back27 max_redirects: int = DEFAULT_MAX_REDIRECTS,28 ):Truckin' onTom Christiebefore adoption6b60d95
lines 29–31why, one step back29 connection_pool = ConnectionPool(30 ssl=ssl, timeout=timeout, pool_limits=pool_limits31 )Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 32–41why, one step back32 cookie_adapter = CookieAdapter(dispatch=connection_pool)33 auth_adapter = AuthenticationAdapter(dispatch=cookie_adapter)34 redirect_adapter = RedirectAdapter(35 dispatch=auth_adapter, max_redirects=max_redirects36 )37 self.adapter = EnvironmentAdapter(dispatch=redirect_adapter)3839 async def request(40 self,41 method: str,Finessing interfaceTom Christiebefore adoptiond7619a9
line 42why, one step back42 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 43why, one step back43 *,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 44why, one step back44 content: ByteOrByteStream = b"",Finessing interfaceTom Christiebefore adoptiond7619a9
line 45why, one step back45 headers: HeaderTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 46–47why, one step back46 stream: bool = False,47 allow_redirects: bool = True,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 48–49why, one step back48 ssl: SSLConfig = None,49 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 50why, one step back50 ) -> Response:Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 51why, one step back51 request = Request(method, url, headers=headers, content=content)Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 52–63why, one step back52 self.prepare_request(request)53 response = await self.send(54 request,55 stream=stream,56 allow_redirects=allow_redirects,57 ssl=ssl,58 timeout=timeout,59 )60 return response6162 async def get(63 self,Finessing interfaceTom Christiebefore adoptiond7619a9
line 64why, one step back64 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 65why, one step back65 *,Finessing interfaceTom Christiebefore adoptiond7619a9
line 66why, one step back66 headers: HeaderTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 67why, one step back67 stream: bool = False,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 68–70why, one step back68 allow_redirects: bool = True,69 ssl: SSLConfig = None,70 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 71–72why, one step back71 ) -> Response:72 return await self.request(Finessing interfaceTom Christiebefore adoptiond7619a9
lines 73–79why, one step back73 "GET",74 url,75 headers=headers,76 stream=stream,77 allow_redirects=allow_redirects,78 ssl=ssl,79 timeout=timeout,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 80–81why, one step back80 )81Finessing interfaceTom Christiebefore adoptiond7619a9
lines 82–121why, one step back82 async def options(83 self,84 url: URLTypes,85 *,86 headers: HeaderTypes = None,87 stream: bool = False,88 allow_redirects: bool = True,89 ssl: SSLConfig = None,90 timeout: TimeoutConfig = None,91 ) -> Response:92 return await self.request(93 "OPTIONS",94 url,95 headers=headers,96 stream=stream,97 allow_redirects=allow_redirects,98 ssl=ssl,99 timeout=timeout,100 )101102 async def head(103 self,104 url: URLTypes,105 *,106 headers: HeaderTypes = None,107 stream: bool = False,108 allow_redirects: bool = False, # Note: Differs to usual default.109 ssl: SSLConfig = None,110 timeout: TimeoutConfig = None,111 ) -> Response:112 return await self.request(113 "HEAD",114 url,115 headers=headers,116 stream=stream,117 allow_redirects=allow_redirects,118 ssl=ssl,119 timeout=timeout,120 )121Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 122–123why, one step back122 async def post(123 self,Finessing interfaceTom Christiebefore adoptiond7619a9
line 124why, one step back124 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 125why, one step back125 *,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 126why, one step back126 content: ByteOrByteStream = b"",Finessing interfaceTom Christiebefore adoptiond7619a9
line 127why, one step back127 headers: HeaderTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 128why, one step back128 stream: bool = False,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 129–131why, one step back129 allow_redirects: bool = True,130 ssl: SSLConfig = None,131 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 132–135why, one step back132 ) -> Response:133 return await self.request(134 "POST",135 url,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 136why, one step back136 content=content,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 137–138why, one step back137 headers=headers,138 stream=stream,Finessing interfaceTom Christiebefore adoptiond7619a9
line 139why, one step back139 allow_redirects=allow_redirects,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 140–143why, one step back140 ssl=ssl,141 timeout=timeout,142 )143Finessing interfaceTom Christiebefore adoptiond7619a9
lines 144–147why, one step back144 async def put(145 self,146 url: URLTypes,147 *,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 148why, one step back148 content: ByteOrByteStream = b"",Finessing interfaceTom Christiebefore adoptiond7619a9
lines 149–157why, one step back149 headers: HeaderTypes = None,150 stream: bool = False,151 allow_redirects: bool = True,152 ssl: SSLConfig = None,153 timeout: TimeoutConfig = None,154 ) -> Response:155 return await self.request(156 "PUT",157 url,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 158why, one step back158 content=content,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 159–169why, one step back159 headers=headers,160 stream=stream,161 allow_redirects=allow_redirects,162 ssl=ssl,163 timeout=timeout,164 )165166 async def patch(167 self,168 url: URLTypes,169 *,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 170why, one step back170 content: ByteOrByteStream = b"",Finessing interfaceTom Christiebefore adoptiond7619a9
lines 171–179why, one step back171 headers: HeaderTypes = None,172 stream: bool = False,173 allow_redirects: bool = True,174 ssl: SSLConfig = None,175 timeout: TimeoutConfig = None,176 ) -> Response:177 return await self.request(178 "PATCH",179 url,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 180why, one step back180 content=content,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 181–191why, one step back181 headers=headers,182 stream=stream,183 allow_redirects=allow_redirects,184 ssl=ssl,185 timeout=timeout,186 )187188 async def delete(189 self,190 url: URLTypes,191 *,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 192why, one step back192 content: ByteOrByteStream = b"",Finessing interfaceTom Christiebefore adoptiond7619a9
lines 193–201why, one step back193 headers: HeaderTypes = None,194 stream: bool = False,195 allow_redirects: bool = True,196 ssl: SSLConfig = None,197 timeout: TimeoutConfig = None,198 ) -> Response:199 return await self.request(200 "DELETE",201 url,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 202why, one step back202 content=content,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 203–209why, one step back203 headers=headers,204 stream=stream,205 allow_redirects=allow_redirects,206 ssl=ssl,207 timeout=timeout,208 )209Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 210–218why, one step back210 def prepare_request(self, request: Request) -> None:211 self.adapter.prepare_request(request)212213 async def send(214 self,215 request: Request,216 *,217 stream: bool = False,218 allow_redirects: bool = True,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 219–220why, one step back219 ssl: SSLConfig = None,220 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 221why, one step back221 ) -> Response:Finessing interfaceTom Christiebefore adoptiond7619a9
lines 222–226why, one step back222 options = {223 "stream": stream,224 "allow_redirects": allow_redirects,225 } # type: typing.Dict[str, typing.Any]226Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 227–230why, one step back227 if ssl is not None:228 options["ssl"] = ssl229 if timeout is not None:230 options["timeout"] = timeoutFinessing interfaceTom Christiebefore adoptiond7619a9
line 231why, one step back231Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 232–246why, one step back232 return await self.adapter.send(request, **options)233234 async def close(self) -> None:235 await self.adapter.close()236237 async def __aenter__(self) -> "Client":238 return self239240 async def __aexit__(241 self,242 exc_type: typing.Type[BaseException] = None,243 exc_value: BaseException = None,244 traceback: TracebackType = None,245 ) -> None:246 await self.close()