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

Commit 10df54a

Browse files
committed
Document the API better.
1 parent 532867f commit 10df54a

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

hyper/http20/connection.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ def __init__(self, host, port=None, **kwargs):
3232
3333
Most of the standard library's arguments to the constructor are
3434
irrelevant for HTTP/2.0 or not supported by hyper.
35+
36+
:param host: The host to connect to. This may be an IP address or a
37+
hostname, and optionally may include a port: for example,
38+
``'twitter.com'``, ``'twitter.com:443'`` or ``'127.0.0.1'``.
39+
:param port: (optional) The port to connect to. If not provided and one
40+
also isn't provided in the ``host`` parameter, defaults to 443.
3541
"""
3642
if port is None:
3743
try:
@@ -78,7 +84,12 @@ def request(self, method, url, body=None, headers={}):
7884
encodings, pass a bytes object. The Content-Length header is set to the
7985
length of the body field.
8086
81-
Returns a stream ID for the request.
87+
:returns: A stream ID for the request.
88+
:param method: The request method, e.g. ``'GET'``.
89+
:param url: The URL to contact, e.g. ``'/path/segment'``.
90+
:param body: (optional) The request body to send. Must be a bytestring
91+
or a file-like object.
92+
:param headers: (optional) The headers to send on the request.
8293
"""
8394
stream_id = self.putrequest(method, url)
8495

@@ -100,6 +111,10 @@ def getresponse(self, stream_id=None):
100111
the request whose response you want. Returns a HTTPResponse instance.
101112
If you pass no stream_id, you will receive the oldest HTTPResponse
102113
still outstanding.
114+
115+
:param stream_id: (optional) The stream ID of the request for which to
116+
get a response.
117+
:returns: A HTTP response object.
103118
"""
104119
stream = (self.streams[stream_id] if stream_id is not None
105120
else self.recent_stream)
@@ -109,6 +124,8 @@ def connect(self):
109124
"""
110125
Connect to the server specified when the object was created. This is a
111126
no-op if we're already connected.
127+
128+
:returns: Nothing.
112129
"""
113130
if self._sock is None:
114131
sock = socket.create_connection((self.host, self.port), 5)
@@ -131,6 +148,8 @@ def connect(self):
131148
def close(self):
132149
"""
133150
Close the connection to the server.
151+
152+
:returns: Nothing.
134153
"""
135154
# Todo: we should actually clean ourselves up if possible by sending
136155
# GoAway frames and closing all outstanding streams. For now this will
@@ -144,6 +163,10 @@ def putrequest(self, method, selector, **kwargs):
144163
This should be the first call for sending a given HTTP request to a
145164
server. It returns a stream ID for the given connection that should be
146165
passed to all subsequent request building calls.
166+
167+
:param method: The request method, e.g. ``'GET'``.
168+
:param selector: The path selector.
169+
:returns: A stream ID for the request.
147170
"""
148171
# Create a new stream.
149172
s = self._new_stream()
@@ -170,6 +193,12 @@ def putheader(self, header, argument, stream_id=None):
170193
Unlike the httplib version of this function, this version does not
171194
actually send anything when called. Instead, it queues the headers up
172195
to be sent when you call ``endheaders``.
196+
197+
:param header: The name of the header.
198+
:param argument: The value of the header.
199+
:param stream_id: (optional) The stream ID of the request to add the
200+
header to.
201+
:returns: Nothing.
173202
"""
174203
stream = (self.streams[stream_id] if stream_id is not None
175204
else self.recent_stream)
@@ -186,6 +215,15 @@ def endheaders(self, message_body=None, final=False, stream_id=None):
186215
``final`` argument is set to True, the stream will also immediately
187216
be closed: otherwise, the stream will be left open and subsequent calls
188217
to ``send()`` will be required.
218+
219+
:param message_body: (optional) The body to send. May not be provided
220+
assuming that ``send()`` will be called.
221+
:param final: (optional) If the ``message_body`` parameter is provided,
222+
should be set to ``True`` if no further data will be provided via
223+
calls to ``send()``.
224+
:param stream_id: (optional) The stream ID of the request to finish
225+
sending the headers on.
226+
:returns: Nothing.
189227
"""
190228
self.connect()
191229

@@ -208,6 +246,13 @@ def send(self, data, final=False, stream_id=None):
208246
(excluding the normal HTTP/2.0 flow control rules). If this is the last
209247
data that will be sent as part of this request, the ``final`` argument
210248
should be set to ``True``. This will cause the stream to be closed.
249+
250+
:param data: The data to send.
251+
:param final: (optional) Whether this is the last bit of data to be
252+
sent on this request.
253+
:param stream_id: (optional) The stream ID of the request to send the
254+
data on.
255+
:returns: Nothing.
211256
"""
212257
stream = (self.streams[stream_id] if stream_id is not None
213258
else self.recent_stream)

hyper/http20/response.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def __init__(self, headers, stream):
9494
def read(self, amt=None, decode_content=True):
9595
"""
9696
Reads the response body, or up to the next ``amt`` bytes.
97+
98+
:param amt: (optional) The amount of data to read. If not provided, all
99+
the data will be read from the response.
100+
:param decode_content: (optional) If ``True``, will transparently
101+
decode the response data.
102+
:returns: The read data. Note that if ``decode_content`` is set to
103+
``True``, the actual amount of data returned may be different to
104+
the amount requested.
97105
"""
98106
if amt is not None and amt <= len(self._data_buffer):
99107
data = self._data_buffer[:amt]
@@ -126,17 +134,24 @@ def getheader(self, name, default=None):
126134
value ``name``, return all of the values joined by ', '. If ``default``
127135
is any iterable other than a single string, its elements are similarly
128136
returned joined by commas.
137+
138+
:param name: The name of the header to get the value of.
139+
:param default: (optional) The return value if the header wasn't sent.
140+
:returns: The value of the header.
129141
"""
130142
return self._headers.get(name, default)
131143

132144
def getheaders(self):
133145
"""
134-
Return a list of (header, value) tuples.
146+
Get all the headers sent on the response.
147+
148+
:returns: A list of (header, value) tuples.
135149
"""
136150
return list(self._headers.items())
137151

138152
def fileno(self):
139153
"""
140-
Return the ``fileno`` of the underlying socket.
154+
Return the ``fileno`` of the underlying socket. This function is
155+
currently not implemented.
141156
"""
142157
pass

0 commit comments

Comments
 (0)