@@ -59,20 +59,20 @@ http2bin.org, a HTTP/1.1 and HTTP/2 testing service.
59
59
60
60
Begin by getting the homepage::
61
61
62
- >>> from hyper import HTTP20Connection
63
- >>> c = HTTP20Connection ('http2bin.org')
62
+ >>> from hyper import HTTPConnection
63
+ >>> c = HTTPConnection ('http2bin.org')
64
64
>>> c.request('GET', '/')
65
65
1
66
66
>>> resp = c.get_response()
67
67
68
68
Used in this way, ``hyper `` behaves exactly like ``http.client ``. You can make
69
69
sequential requests using the exact same API you're accustomed to. The only
70
70
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.
76
76
77
77
Once you've got the data, things diverge a little bit::
78
78
@@ -101,8 +101,8 @@ the response from any of them, and switch between them using their stream IDs.
101
101
102
102
For example::
103
103
104
- >>> from hyper import HTTP20Connection
105
- >>> c = HTTP20Connection ('http2bin.org')
104
+ >>> from hyper import HTTPConnection
105
+ >>> c = HTTPConnection ('http2bin.org')
106
106
>>> first = c.request('GET', '/get')
107
107
>>> second = c.request('POST', '/post', data='key=value')
108
108
>>> third = c.request('GET', '/ip')
@@ -112,40 +112,18 @@ For example::
112
112
113
113
``hyper `` will ensure that each response is matched to the correct request.
114
114
115
- Making Your First HTTP/1.1 Request
116
- -----------------------------------
115
+ Abstraction
116
+ -----------
117
117
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.
135
121
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.
138
125
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.
149
127
150
128
Requests Integration
151
129
--------------------
@@ -155,8 +133,7 @@ requests doesn't support HTTP/2 though. To rectify that oversight, ``hyper``
155
133
provides a transport adapter that can be plugged directly into Requests, giving
156
134
it instant HTTP/2 support.
157
135
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::
160
137
161
138
>>> import requests
162
139
>>> from hyper.contrib import HTTP20Adapter
@@ -169,14 +146,6 @@ HTTP/2. Once you've worked that out, you can get started straight away::
169
146
This transport adapter is subject to all of the limitations that apply to
170
147
``hyper ``, and provides all of the goodness of requests.
171
148
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
-
180
149
.. _requests : http://python-requests.org/
181
150
182
151
HTTPie Integration
0 commit comments