]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Still truckin' on
authorTom Christie <tom@tomchristie.com>
Wed, 1 May 2019 14:17:58 +0000 (15:17 +0100)
committerTom Christie <tom@tomchristie.com>
Wed, 1 May 2019 14:17:58 +0000 (15:17 +0100)
README.md
httpcore/models.py
httpcore/sync.py

index 571f2467b9a93784d8b809b450bff5eac42d9f91..0c8817a7762e959b7d843265e7151b71f5e87e5c 100644 (file)
--- a/README.md
+++ b/README.md
@@ -36,9 +36,11 @@ Making a request:
 ```python
 >>> import httpcore
 >>> client = httpcore.Client()
->>> response = await client.get('http://example.com')
+>>> response = await client.get('https://example.com')
 >>> response.status_code
 <StatusCode.ok: 200>
+>>> response.protocol
+'HTTP/2'
 >>> response.text
 '<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>\n...'
 ```
@@ -48,9 +50,11 @@ Alternatively, thread-synchronous requests:
 ```python
 >>> import httpcore
 >>> client = httpcore.SyncClient()
->>> response = client.get('http://example.com')
+>>> response = client.get('https://example.com')
 >>> response.status_code
 <StatusCode.ok: 200>
+>>> response.protocol
+'HTTP/2'
 >>> response.text
 '<!doctype html>\n<html>\n<head>\n<title>Example Domain</title>\n...'
 ```
@@ -61,22 +65,24 @@ Alternatively, thread-synchronous requests:
 
 #### `Client([ssl], [timeout], [pool_limits], [max_redirects])`
 
-* `.request(method, url, ...)`
-* `.get(url, ...)`
-* `.options(url, ...)`
-* `.head(url, ...)`
-* `.post(url, ...)`
-* `.put(url, ...)`
-* `.patch(url, ...)`
-* `.delete(url, ...)`
-* `.prepare_request(request)`
-* `.send(request, ...)`
-* `.close()`
+* `async def .request(method, url, ...)`
+* `async def .get(url, ...)`
+* `async def .options(url, ...)`
+* `async def .head(url, ...)`
+* `async def .post(url, ...)`
+* `async def .put(url, ...)`
+* `async def .patch(url, ...)`
+* `async def .delete(url, ...)`
+* `def .prepare_request(request)`
+* `async def .send(request, ...)`
+* `async def .close()`
 
 ### Models
 
 #### `Response(...)`
 
+*An HTTP response.**
+
 * `.status_code` - **int**
 * `.reason_phrase` - **str**
 * `.protocol` - `"HTTP/2"` or `"HTTP/1.1"`
@@ -89,9 +95,6 @@ Alternatively, thread-synchronous requests:
 * `.request` - **Request**
 * `.cookies` - **Cookies** *TODO*
 * `.history` - **List[Response]**
-
-Methods:
-
 * `def .raise_for_status()` - **Response** *TODO*
 * `def .json()` - **Any** *TODO*
 * `async def .read()` - **bytes**
@@ -102,6 +105,14 @@ Methods:
 
 #### `Request(method, url, content, headers)`
 
+*An HTTP request. Can be constructed explicitly for more control over exactly
+what gets sent over the wire.*
+
+```python
+>>> request = Request("GET", "http://example.org", headers={'host': 'example.org'})
+>>> response = await client.send(request)
+```
+
 * `.method` - **str** (Uppercased)
 * `.url` - **URL**
 * `.content` - **byte** or **byte async iterator**
@@ -123,9 +134,6 @@ Methods:
 * `.origin` - **Origin**
 * `.is_absolute_url` - **bool**
 * `.is_relative_url` - **bool**
-
-Methods:
-
 * `def .copy_with([scheme], [authority], [path], [query], [fragment])` - **URL**
 * `def .resolve_with(url)` - **URL**
 
index 72a52e27ba2f15961fbf2a74a4b9e03168714b1f..267c1ce39e048a93dec519d171eeecbd3affa8f5 100644 (file)
@@ -421,6 +421,11 @@ class Request:
         for item in reversed(auto_headers):
             self.headers.raw.insert(0, item)
 
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        url = str(self.url)
+        return f"<{class_name}({self.method}, {url})>"
+
 
 class Response:
     def __init__(
@@ -437,7 +442,7 @@ class Response:
     ):
         try:
             # Use a StatusCode IntEnum if possible, for a nicer representation.
-            self.status_code = codes(status_code)
+            self.status_code = codes(status_code)  # type: int
         except ValueError:
             self.status_code = status_code
         self.reason_phrase = reason_phrase or get_reason_phrase(status_code)
@@ -619,3 +624,7 @@ class Response:
             )
             and "location" in self.headers
         )
+
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        return f"<{class_name}(status_code={self.status_code})>"
index 63fdf9ff48aad865f7ec98b763c8d69914c6981e..300c276bd0936751904353e06f605949f96f7866 100644 (file)
@@ -36,6 +36,10 @@ class SyncResponse:
     def reason_phrase(self) -> str:
         return self._response.reason_phrase
 
+    @property
+    def protocol(self) -> typing.Optional[str]:
+        return self._response.protocol
+
     @property
     def headers(self) -> Headers:
         return self._response.headers
@@ -70,6 +74,10 @@ class SyncResponse:
     def close(self) -> None:
         return self._loop.run_until_complete(self._response.close())
 
+    def __repr__(self) -> str:
+        class_name = self.__class__.__name__
+        return f"<{class_name}(status_code={self.status_code})>"
+
 
 class SyncClient:
     def __init__(
@@ -99,19 +107,16 @@ class SyncClient:
         ssl: SSLConfig = None,
         timeout: TimeoutConfig = None,
     ) -> SyncResponse:
-        response = self._loop.run_until_complete(
-            self._client.request(
-                method,
-                url,
-                content=content,
-                headers=headers,
-                stream=stream,
-                allow_redirects=allow_redirects,
-                ssl=ssl,
-                timeout=timeout,
-            )
+        request = Request(method, url, headers=headers, content=content)
+        self.prepare_request(request)
+        response = self.send(
+            request,
+            stream=stream,
+            allow_redirects=allow_redirects,
+            ssl=ssl,
+            timeout=timeout,
         )
-        return SyncResponse(response, self._loop)
+        return response
 
     def get(
         self,
@@ -261,6 +266,29 @@ class SyncClient:
             timeout=timeout,
         )
 
+    def prepare_request(self, request: Request) -> None:
+        self._client.prepare_request(request)
+
+    def send(
+        self,
+        request: Request,
+        *,
+        stream: bool = False,
+        allow_redirects: bool = True,
+        ssl: SSLConfig = None,
+        timeout: TimeoutConfig = None,
+    ) -> SyncResponse:
+        response = self._loop.run_until_complete(
+            self._client.send(
+                request,
+                stream=stream,
+                allow_redirects=allow_redirects,
+                ssl=ssl,
+                timeout=timeout,
+            )
+        )
+        return SyncResponse(response, self._loop)
+
     def close(self) -> None:
         self._loop.run_until_complete(self._client.close())