Add top-level API (#75)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add top-level API * Add tests for top-level API
authored by Tom Christie · committed by GitHub · parent 5a63d31 · Browse files · 4 files·+311 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+311 added−1 removed
- tests:+73 added−0 removed
- other:+238 added−1 removed
Provenance
- Commit
fc627f3- Tree
ce7a934- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
5a63d31
4 files changed+311 added−1 removed
modified:httpcore/__init__.py+1 added
1+from .api import delete, get, head, options, patch, post, put, request2 from .client import AsyncClient, Client3 from .concurrency import AsyncioBackend4 from .config import PoolLimits, SSLConfig, TimeoutConfig5 from .dispatch.connection import HTTPConnection23 unmodified linesadded:httpcore/api.py+236 added
1+import typing2+3+from .client import Client4+from .config import SSLConfig, TimeoutConfig5+from .models import (6+ AuthTypes,7+ CookieTypes,8+ HeaderTypes,9+ QueryParamTypes,10+ RequestData,11+ SyncResponse,12+ URLTypes,13+)14+15+16+def request(17+ method: str,18+ url: URLTypes,19+ *,20+ data: RequestData = b"",21+ json: typing.Any = None,22+ params: QueryParamTypes = None,23+ headers: HeaderTypes = None,24+ cookies: CookieTypes = None,25+ stream: bool = False,26+ auth: AuthTypes = None,27+ allow_redirects: bool = True,28+ ssl: SSLConfig = None,29+ timeout: TimeoutConfig = None,30+) -> SyncResponse:31+ with Client() as client:32+ return client.request(33+ method=method,34+ url=url,35+ data=data,36+ json=json,37+ params=params,38+ headers=headers,39+ cookies=cookies,40+ stream=stream,41+ auth=auth,42+ allow_redirects=allow_redirects,43+ ssl=ssl,44+ timeout=timeout,45+ )46+47+48+def get(49+ url: URLTypes,50+ *,51+ params: QueryParamTypes = None,52+ headers: HeaderTypes = None,53+ cookies: CookieTypes = None,54+ stream: bool = False,55+ auth: AuthTypes = None,56+ allow_redirects: bool = True,57+ ssl: SSLConfig = None,58+ timeout: TimeoutConfig = None,59+) -> SyncResponse:60+ return request(61+ "GET",62+ url,63+ headers=headers,64+ cookies=cookies,65+ stream=stream,66+ auth=auth,67+ allow_redirects=allow_redirects,68+ ssl=ssl,69+ timeout=timeout,70+ )71+72+73+def options(74+ url: URLTypes,75+ *,76+ params: QueryParamTypes = None,77+ headers: HeaderTypes = None,78+ cookies: CookieTypes = None,79+ stream: bool = False,80+ auth: AuthTypes = None,81+ allow_redirects: bool = True,82+ ssl: SSLConfig = None,83+ timeout: TimeoutConfig = None,84+) -> SyncResponse:85+ return request(86+ "OPTIONS",87+ url,88+ headers=headers,89+ cookies=cookies,90+ stream=stream,91+ auth=auth,92+ allow_redirects=allow_redirects,93+ ssl=ssl,94+ timeout=timeout,95+ )96+97+98+def head(99+ url: URLTypes,100+ *,101+ params: QueryParamTypes = None,102+ headers: HeaderTypes = None,103+ cookies: CookieTypes = None,104+ stream: bool = False,105+ auth: AuthTypes = None,106+ allow_redirects: bool = False, # Note: Differs to usual default.107+ ssl: SSLConfig = None,108+ timeout: TimeoutConfig = None,109+) -> SyncResponse:110+ return request(111+ "HEAD",112+ url,113+ headers=headers,114+ cookies=cookies,115+ stream=stream,116+ auth=auth,117+ allow_redirects=allow_redirects,118+ ssl=ssl,119+ timeout=timeout,120+ )121+122+123+def post(124+ url: URLTypes,125+ *,126+ data: RequestData = b"",127+ json: typing.Any = None,128+ params: QueryParamTypes = None,129+ headers: HeaderTypes = None,130+ cookies: CookieTypes = None,131+ stream: bool = False,132+ auth: AuthTypes = None,133+ allow_redirects: bool = True,134+ ssl: SSLConfig = None,135+ timeout: TimeoutConfig = None,136+) -> SyncResponse:137+ return request(138+ "POST",139+ url,140+ data=data,141+ json=json,142+ headers=headers,143+ cookies=cookies,144+ stream=stream,145+ auth=auth,146+ allow_redirects=allow_redirects,147+ ssl=ssl,148+ timeout=timeout,149+ )150+151+152+def put(153+ url: URLTypes,154+ *,155+ data: RequestData = b"",156+ json: typing.Any = None,157+ params: QueryParamTypes = None,158+ headers: HeaderTypes = None,159+ cookies: CookieTypes = None,160+ stream: bool = False,161+ auth: AuthTypes = None,162+ allow_redirects: bool = True,163+ ssl: SSLConfig = None,164+ timeout: TimeoutConfig = None,165+) -> SyncResponse:166+ return request(167+ "PUT",168+ url,169+ data=data,170+ json=json,171+ headers=headers,172+ cookies=cookies,173+ stream=stream,174+ auth=auth,175+ allow_redirects=allow_redirects,176+ ssl=ssl,177+ timeout=timeout,178+ )179+180+181+def patch(182+ url: URLTypes,183+ *,184+ data: RequestData = b"",185+ json: typing.Any = None,186+ params: QueryParamTypes = None,187+ headers: HeaderTypes = None,188+ cookies: CookieTypes = None,189+ stream: bool = False,190+ auth: AuthTypes = None,191+ allow_redirects: bool = True,192+ ssl: SSLConfig = None,193+ timeout: TimeoutConfig = None,194+) -> SyncResponse:195+ return request(196+ "PATCH",197+ url,198+ data=data,199+ json=json,200+ headers=headers,201+ cookies=cookies,202+ stream=stream,203+ auth=auth,204+ allow_redirects=allow_redirects,205+ ssl=ssl,206+ timeout=timeout,207+ )208+209+210+def delete(211+ url: URLTypes,212+ *,213+ data: RequestData = b"",214+ json: typing.Any = None,215+ params: QueryParamTypes = None,216+ headers: HeaderTypes = None,217+ cookies: CookieTypes = None,218+ stream: bool = False,219+ auth: AuthTypes = None,220+ allow_redirects: bool = True,221+ ssl: SSLConfig = None,222+ timeout: TimeoutConfig = None,223+) -> SyncResponse:224+ return request(225+ "DELETE",226+ url,227+ data=data,228+ json=json,229+ headers=headers,230+ cookies=cookies,231+ stream=stream,232+ auth=auth,233+ allow_redirects=allow_redirects,234+ ssl=ssl,235+ timeout=timeout,236+ )added:tests/test_api.py+73 added
1+import asyncio2+import functools3+4+import pytest5+6+import httpcore7+8+9+def threadpool(func):10+ """11+ Our sync tests should run in seperate thread to the uvicorn server.12+ """13+14+ @functools.wraps(func)15+ async def wrapped(*args, **kwargs):16+ nonlocal func17+18+ loop = asyncio.get_event_loop()19+ if kwargs:20+ func = functools.partial(func, **kwargs)21+ await loop.run_in_executor(None, func, *args)22+23+ return pytest.mark.asyncio(wrapped)24+25+26+@threadpool27+def test_get(server):28+ response = httpcore.get("http://127.0.0.1:8000/")29+ assert response.status_code == 20030+ assert response.reason_phrase == "OK"31+ assert response.text == "Hello, world!"32+33+34+@threadpool35+def test_post(server):36+ response = httpcore.post("http://127.0.0.1:8000/", data=b"Hello, world!")37+ assert response.status_code == 20038+ assert response.reason_phrase == "OK"39+40+41+@threadpool42+def test_options(server):43+ response = httpcore.options("http://127.0.0.1:8000/")44+ assert response.status_code == 20045+ assert response.reason_phrase == "OK"46+47+48+@threadpool49+def test_head(server):50+ response = httpcore.head("http://127.0.0.1:8000/")51+ assert response.status_code == 20052+ assert response.reason_phrase == "OK"53+54+55+@threadpool56+def test_put(server):57+ response = httpcore.put("http://127.0.0.1:8000/", data=b"Hello, world!")58+ assert response.status_code == 20059+ assert response.reason_phrase == "OK"60+61+62+@threadpool63+def test_patch(server):64+ response = httpcore.patch("http://127.0.0.1:8000/", data=b"Hello, world!")65+ assert response.status_code == 20066+ assert response.reason_phrase == "OK"67+68+69+@threadpool70+def test_delete(server):71+ response = httpcore.delete("http://127.0.0.1:8000/")72+ assert response.status_code == 20073+ assert response.reason_phrase == "OK"modified:README.md+1 added-1 removed
27 unmodified lines28 * Elegant Key/Value Cookies29 * Automatic Decompression30 * Automatic Content Decoding31 * Unicode Response Bodies32-* Multipart File Uploads *TODO - Request content currently supports URL encoded data, bytes, or async byte iterators.*32+* Multipart File Uploads *TODO - Request content currently supports URL encoded data, JSON, bytes, or async byte iterators.*33 * HTTP(S) Proxy Support *TODO*34 * Connection Timeouts35 * Streaming Downloads36 * .netrc Support *TODO*214 unmodified lines