Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 6b97aca

Browse files
committed
Abstraction docs.
1 parent c0e4e1b commit 6b97aca

File tree

4 files changed

+33
-66
lines changed

4 files changed

+33
-66
lines changed

README.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ improved speed, lower bandwidth usage, better connection management, and more.
1313

1414
``hyper`` provides these benefits to your Python code. How? Like this::
1515

16-
from hyper import HTTP20Connection
16+
from hyper import HTTPConnection
1717

18-
conn = HTTP20Connection('http2bin.org:443')
18+
conn = HTTPConnection('http2bin.org:443')
1919
conn.request('GET', '/get')
20-
resp = conn.getresponse()
20+
resp = conn.get_response()
2121

2222
print(resp.read())
2323

docs/source/advanced.rst

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ may want to keep your connections alive only as long as you know you'll need
1313
them. In HTTP/2 this is generally not something you should do unless you're
1414
very confident you won't need the connection again anytime soon. However, if
1515
you decide you want to avoid keeping the connection open, you can use the
16-
:class:`HTTP20Connection <hyper.HTTP20Connection>` and
17-
:class:`HTTP11Connection <hyper.HTTP11Connection>` as context managers::
16+
:class:`HTTPConnection <hyper.HTTPConnection>` as a context manager::
1817

19-
with HTTP20Connection('http2bin.org') as conn:
18+
with HTTPConnection('http2bin.org') as conn:
2019
conn.request('GET', '/get')
21-
data = conn.getresponse().read()
20+
data = conn.get_response().read()
2221

2322
analyse(data)
2423

@@ -57,10 +56,9 @@ Very easy!
5756
Multithreading
5857
--------------
5958

60-
Currently, ``hyper``'s :class:`HTTP20Connection <hyper.HTTP20Connection>` and
61-
:class:`HTTP11Connection <hyper.HTTP11Connection>` classes are **not**
62-
thread-safe. Thread-safety is planned for ``hyper``'s core objects, but in this
63-
early alpha it is not a high priority.
59+
Currently, ``hyper``'s :class:`HTTPConnection <hyper.HTTPConnection>` class
60+
is **not** thread-safe. Thread-safety is planned for ``hyper``'s core objects,
61+
but in this early alpha it is not a high priority.
6462

6563
To use ``hyper`` in a multithreaded context the recommended thing to do is to
6664
place each connection in its own thread. Each thread should then have a request
@@ -155,8 +153,8 @@ headers for the original request, after, or in the middle of sending the
155153
response body.
156154

157155
In order to receive pushed resources, the
158-
:class:`HTTP20Connection <hyper.HTTP20Connection>` object must be constructed
159-
with ``enable_push=True``.
156+
:class:`HTTPConnection <hyper.HTTPConnection>` object must be constructed with
157+
``enable_push=True``.
160158

161159
You may retrieve the push promises that the server has sent *so far* by calling
162160
:meth:`get_pushes() <hyper.HTTP20Connection.get_pushes>`, which returns a

docs/source/index.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ improved speed, lower bandwidth usage, better connection management, and more.
1414

1515
``hyper`` provides these benefits to your Python code. How? Like this::
1616

17-
from hyper import HTTP20Connection
17+
from hyper import HTTPConnection
1818

19-
conn = HTTP20Connection('http2bin.org:443')
19+
conn = HTTPConnection('http2bin.org:443')
2020
conn.request('GET', '/get')
21-
resp = conn.getresponse()
21+
resp = conn.get_response()
2222

2323
print(resp.read())
2424

docs/source/quickstart.rst

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,20 @@ http2bin.org, a HTTP/1.1 and HTTP/2 testing service.
5959

6060
Begin by getting the homepage::
6161

62-
>>> from hyper import HTTP20Connection
63-
>>> c = HTTP20Connection('http2bin.org')
62+
>>> from hyper import HTTPConnection
63+
>>> c = HTTPConnection('http2bin.org')
6464
>>> c.request('GET', '/')
6565
1
6666
>>> resp = c.get_response()
6767

6868
Used in this way, ``hyper`` behaves exactly like ``http.client``. You can make
6969
sequential requests using the exact same API you're accustomed to. The only
7070
difference is that
71-
:meth:`HTTP20Connection.request() <hyper.HTTP20Connection.request>` returns a
72-
value, unlike the equivalent ``http.client`` function. The return value is the
73-
HTTP/2 *stream identifier*. If you're planning to use ``hyper`` in this very
74-
simple way, you can choose to ignore it, but it's potentially useful. We'll
75-
come back to it.
71+
:meth:`HTTPConnection.request() <hyper.HTTPConnection.request>` may return a
72+
value, unlike the equivalent ``http.client`` function. If present, the return
73+
value is the HTTP/2 *stream identifier*. If you're planning to use ``hyper``
74+
in this very simple way, you can choose to ignore it, but it's potentially
75+
useful. We'll come back to it.
7676

7777
Once you've got the data, things diverge a little bit::
7878

@@ -101,8 +101,8 @@ the response from any of them, and switch between them using their stream IDs.
101101

102102
For example::
103103

104-
>>> from hyper import HTTP20Connection
105-
>>> c = HTTP20Connection('http2bin.org')
104+
>>> from hyper import HTTPConnection
105+
>>> c = HTTPConnection('http2bin.org')
106106
>>> first = c.request('GET', '/get')
107107
>>> second = c.request('POST', '/post', data='key=value')
108108
>>> third = c.request('GET', '/ip')
@@ -112,40 +112,18 @@ For example::
112112

113113
``hyper`` will ensure that each response is matched to the correct request.
114114

115-
Making Your First HTTP/1.1 Request
116-
-----------------------------------
115+
Abstraction
116+
-----------
117117

118-
With ``hyper`` installed, you can start making HTTP/2 requests. At this
119-
stage, ``hyper`` can only be used with services that *definitely* support
120-
HTTP/2. Before you begin, ensure that whichever service you're contacting
121-
definitely supports HTTP/2. For the rest of these examples, we'll use
122-
Twitter.
123-
124-
You can also use ``hyper`` to make HTTP/1.1 requests. The code is very similar.
125-
For example, to get the Twitter homepage::
126-
127-
>>> from hyper import HTTP11Connection
128-
>>> c = HTTP11Connection('twitter.com:443')
129-
>>> c.request('GET', '/')
130-
>>> resp = c.get_response()
131-
132-
The key difference between HTTP/1.1 and HTTP/2 is that when you make HTTP/1.1
133-
requests you do not get a stream ID. This is, of course, because HTTP/1.1 does
134-
not have streams.
118+
When you use the :class:`HTTPConnection <hyper.HTTPConnection>` object, you
119+
don't have to know in advance whether your service supports HTTP/2 or not. If
120+
it doesn't, ``hyper`` will transparently fall back to HTTP/1.1.
135121

136-
Things behave exactly like they do in the HTTP/2 case, right down to the data
137-
reading::
122+
You can tell the difference: if :meth:`request <hyper.HTTPConnection.request>`
123+
returns a stream ID, then the connection is using HTTP/2: if it returns
124+
``None``, then HTTP/1.1 is being used.
138125

139-
>>> resp.headers['content-encoding']
140-
[b'deflate']
141-
>>> resp.headers
142-
HTTPHeaderMap([(b'x-xss-protection', b'1; mode=block')...
143-
>>> resp.status
144-
200
145-
>>> body = resp.read()
146-
b'<!DOCTYPE html>\n<!--[if IE 8]><html clas ....
147-
148-
That's all it takes.
126+
Generally, though, you don't need to care.
149127

150128
Requests Integration
151129
--------------------
@@ -155,8 +133,7 @@ requests doesn't support HTTP/2 though. To rectify that oversight, ``hyper``
155133
provides a transport adapter that can be plugged directly into Requests, giving
156134
it instant HTTP/2 support.
157135

158-
All you have to do is identify a host that you'd like to communicate with over
159-
HTTP/2. Once you've worked that out, you can get started straight away::
136+
Using ``hyper`` with requests is super simple::
160137

161138
>>> import requests
162139
>>> from hyper.contrib import HTTP20Adapter
@@ -169,14 +146,6 @@ HTTP/2. Once you've worked that out, you can get started straight away::
169146
This transport adapter is subject to all of the limitations that apply to
170147
``hyper``, and provides all of the goodness of requests.
171148

172-
A quick warning: some hosts will redirect to new hostnames, which may redirect
173-
you away from HTTP/2. Make sure you install the adapter for all the hostnames
174-
you're interested in::
175-
176-
>>> a = HTTP20Adapter()
177-
>>> s.mount('https://http2bin.org', a)
178-
>>> s.mount('https://www.http2bin.org', a)
179-
180149
.. _requests: http://python-requests.org/
181150

182151
HTTPie Integration

0 commit comments

Comments
 (0)