Cleanup Advanced doc and remove `.:' from docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Cory Benfield · committed by Cory Benfield · parent c323047 · Browse files · 1 file·+42 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+42 added−22 removed
- other:+42 added−22 removed
Provenance
- Commit
b5ce502- Tree
3f61ab0- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent
c323047
1 file changed+42 added−22 removed
modified:docs/user/advanced.rst+42 added-22 removed
91 unmodified lines92 <Response [200]>93 94 You can also pass ``verify`` the path to a CA_BUNDLE file for private certs. You can also set the ``REQUESTS_CA_BUNDLE`` environment variable.95 96-Requests can also ignore verifying the SSL certficate if you set ``verify`` to False. ::96+Requests can also ignore verifying the SSL certficate if you set ``verify`` to False.97 98+::99+100 >>> requests.get('https://kennethreitz.com', verify=False)101 <Response [200]>102 103 By default, ``verify`` is set to True. Option ``verify`` only applies to host certs.18 unmodified lines122 123 tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'124 r = requests.get(tarball_url)125 124-The request has been made, but the connection is still open. The response body has not been downloaded yet. ::126+The request has been made, but the connection is still open. The response body has not been downloaded yet.127 128+::129+130 r.content131 132 The content has been downloaded and cached.133 148 unmodified lines282 283 With ``requests.Response.iter_lines()`` you can easily iterate over streaming284 APIs such as the `Twitter Streaming API <https://dev.twitter.com/docs/streaming-api>`_.285 282-To use the Twitter Streaming API to track the keyword "requests":286+To use the Twitter Streaming API to track the keyword "requests"::287 284-::285-288 import requests289 import json290 291 r = requests.post('https://stream.twitter.com/1/statuses/filter.json',21 unmodified lines313 Proxies314 -------315 316 If you need to use a proxy, you can configure individual requests with the315-``proxies`` argument to any request method:317+``proxies`` argument to any request method::318 317-::318-319 import requests320 321 proxies = {322 "http": "10.10.1.10:3128",11 unmodified lines334 $ python335 >>> import requests336 >>> requests.get("http://example.org")337 338-To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax:338+To use HTTP Basic Auth with your proxy, use the `http://user:password@host/` syntax::339 340-::341-340 proxies = {341 "http": "http://user:pass@10.10.1.10:3128/",342 }343 52 unmodified lines396 into Python objects. Because GitHub returned UTF-8, we should use the397 ``r.text`` method, not the ``r.content`` method. ``r.content`` returns a398 bytestring, while ``r.text`` returns a Unicode-encoded string. I have no plans399 to perform byte-manipulation on this response, so I want any Unicode code402-points encoded.::400+points encoded.401 402+::403+404 >>> import json405 >>> commit_data = json.loads(r.text)406 >>> print commit_data.keys()407 [u'committer', u'author', u'url', u'tree', u'sha', u'parents', u'message']4 unmodified lines412 413 So far, so simple. Well, let's investigate the GitHub API a little bit. Now,414 we could look at the documentation, but we might have a little more fun if we415 use Requests instead. We can take advantage of the Requests OPTIONS verb to416-see what kinds of HTTP methods are supported on the url we just used.::416+see what kinds of HTTP methods are supported on the url we just used.417 418+::419+420 >>> verbs = requests.options(r.url)421 >>> verbs.status_code422 500423 424 Uh, what? That's unhelpful! Turns out GitHub, like many API providers, don't425 actually implement the OPTIONS method. This is an annoying oversight, but it's426 OK, we can just use the boring documentation. If GitHub had correctly427 implemented OPTIONS, however, they should return the allowed methods in the426-headers, e.g.::428+headers, e.g.429 430+::431+432 >>> verbs = requests.options('http://a-good-website.com/api/cats')433 >>> print verbs.headers['allow']434 GET,HEAD,POST,OPTIONS435 2 unmodified lines438 we should probably avoid making ham-handed POSTS to it. Instead, let's play439 with the Issues feature of GitHub.440 441 This documentation was added in response to Issue #482. Given that this issue438-already exists, we will use it as an example. Let's start by getting it.::442+already exists, we will use it as an example. Let's start by getting it.443 444+::445+446 >>> r = requests.get('https://api.github.com/repos/kennethreitz/requests/issues/482')447 >>> r.status_code448 200449 >>> issue = json.loads(r.text)1 unmodified line451 Feature any http verb in docs452 >>> print issue[u'comments']453 3454 449-Cool, we have three comments. Let's take a look at the last of them.::455+Cool, we have three comments. Let's take a look at the last of them.456 457+::458+459 >>> r = requests.get(r.url + u'/comments')460 >>> r.status_code461 200462 >>> comments = json.loads(r.text)2 unmodified lines465 >>> print comments[2][u'body']466 Probably in the "advanced" section467 468 Well, that seems like a silly place. Let's post a comment telling the poster461-that he's silly. Who is the poster, anyway?::469+that he's silly. Who is the poster, anyway?470 471+::472+473 >>> print comments[2][u'user'][u'login']474 kennethreitz475 476 OK, so let's tell this Kenneth guy that we think this example should go in the477 quickstart guide instead. According to the GitHub API doc, the way to do this468-is to POST to the thread. Let's do it.::478+is to POST to the thread. Let's do it.479 480+::481+482 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it!"})483 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/482/comments"484 >>> r = requests.post(url=url, data=body)485 >>> r.status_code486 404487 488 Huh, that's weird. We probably need to authenticate. That'll be a pain, right?489 Wrong. Requests makes it easy to use many forms of authentication, including478-the very common Basic Auth.::490+the very common Basic Auth.491 492+::493+494 >>> from requests.auth import HTTPBasicAuth495 >>> auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password')496 >>> r = requests.post(url=url, data=body, auth=auth)497 >>> r.status_code4 unmodified lines502 503 Brilliant. Oh, wait, no! I meant to add that it would take me a while, because504 I had to go feed my cat. If only I could edit this comment! Happily, GitHub505 allows us to use another HTTP verb, PATCH, to edit this comment. Let's do492-that.::506+that.507 508+::509+510 >>> print content[u"id"]511 5804413512 >>> body = json.dumps({u"body": u"Sounds great! I'll get right on it once I feed my cat."})513 >>> url = u"https://api.github.com/repos/kennethreitz/requests/issues/comments/5804413"3 unmodified lines517 518 Excellent. Now, just to torture this Kenneth guy, I've decided to let him519 sweat and not tell him that I'm working on this. That means I want to delete520 this comment. GitHub lets us delete comments using the incredibly aptly named505-DELETE method. Let's get rid of it.::521+DELETE method. Let's get rid of it.522 523+::524+525 >>> r = requests.delete(url=url, auth=auth)526 >>> r.status_code527 204528 >>> r.headers['status']1 unmodified line530 531 Excellent. All gone. The last thing I want to know is how much of my ratelimit532 I've used. Let's find out. GitHub sends that information in the headers, so533 rather than download the whole page I'll send a HEAD request to get the516-headers.::534+headers.535 536+::537+538 >>> r = requests.head(url=url, auth=auth)539 >>> print r.headers540 ...541 'x-ratelimit-remaining': '4995'26 unmodified lines