PATCH support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parent bdf9f90 · Browse files · 3 files·+81 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+81 added−28 removed
- other:+81 added−28 removed
Provenance
- Commit
00f066a- Tree
8fc0790- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent
bdf9f90
3 files changed+81 added−28 removed
modified:requests/api.py+61 added-22 removed
9 unmodified lines10 :license: ISC, see LICENSE for more details.11 12 """13 14-import requests14 import config15 from .models import Request, Response, AuthManager, AuthObject, auth_manager16 17 19-__all__ = ('request', 'get', 'head', 'post', 'put', 'delete')18+__all__ = ('request', 'get', 'head', 'post', 'patch', 'put', 'delete')19 20+def request(method, url,21-def request(method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None,22- timeout=None, allow_redirects=False, proxies=None):21+ params=None, data=None, headers=None, cookies=None, files=None, auth=None,22+ timeout=None, allow_redirects=False, proxies=None):23+24 """Constructs and sends a :class:`Request <models.Request>`. Returns :class:`Response <models.Response>` object.25 26 :param method: method for the new :class:`Request` object.27 :param url: URL for the new :class:`Request` object.25 unmodified lines53 r.send()54 55 return r.response56 56-def get(url, params=None, headers=None, cookies=None, auth=None, timeout=None, proxies=None):57+58+def get(url,59+ params=None, headers=None, cookies=None, auth=None, timeout=None,60+ proxies=None):61+62 """Sends a GET request. Returns :class:`Response` object.63 64 :param url: URL for the new :class:`Request` object.65 :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.3 unmodified lines69 :param timeout: (optional) Float describing the timeout of the request.70 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.71 """72 68- return request('GET', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout,69- proxies=proxies)73+ return request('GET', url,74+ params=params, headers=headers, cookies=cookies, auth=auth,75+ timeout=timeout, proxies=proxies)76 77 72-def head(url, params=None, headers=None, cookies=None, auth=None, timeout=None, proxies=None):78+def head(url,79+ params=None, headers=None, cookies=None, auth=None, timeout=None,80+ proxies=None):81+82 """Sends a HEAD request. Returns :class:`Response` object.83 84 :param url: URL for the new :class:`Request` object.85 :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.3 unmodified lines89 :param timeout: (optional) Float describing the timeout of the request.90 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.91 """92 84- return request('HEAD', url, params=params, headers=headers, cookies=cookies, auth=auth, timeout=timeout,85- proxies=proxies)93+ return request('HEAD', url,94+ params=params, headers=headers, cookies=cookies, auth=auth,95+ timeout=timeout, proxies=proxies)96 97 88-def post(url, data='', headers=None, files=None, cookies=None, auth=None,89- timeout=None, allow_redirects=False, params=None, proxies=None):98+def post(url,99+ data='', headers=None, files=None, cookies=None, auth=None, timeout=None,100+ allow_redirects=False, params=None, proxies=None):101+102 """Sends a POST request. Returns :class:`Response` object.103 104 :param url: URL for the new :class:`Request` object.105 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.6 unmodified lines112 :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.113 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.114 """115 104- return request('POST', url, params=params, data=data, headers=headers,105- files=files, cookies=cookies, auth=auth, timeout=timeout,106- allow_redirects=allow_redirects, proxies=proxies)116+ return request('POST', url,117+ params=params, data=data, headers=headers, files=files,118+ cookies=cookies, auth=auth, timeout=timeout,119+ allow_redirects=allow_redirects, proxies=proxies)120 121 122 def put(url, data='', headers=None, files=None, cookies=None, auth=None,123 timeout=None, allow_redirects=False, params=None, proxies=None):10 unmodified lines134 :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.135 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.136 """137 125- return request('PUT', url, params=params, data=data, headers=headers,126- files=files, cookies=cookies, auth=auth, timeout=timeout,127- allow_redirects=allow_redirects, proxies=proxies)138+ return request('PUT', url,139+ params=params, data=data, headers=headers, files=files,140+ cookies=cookies, auth=auth, timeout=timeout,141+ allow_redirects=allow_redirects, proxies=proxies)142 143 130-def delete(url, params=None, headers=None, cookies=None, auth=None, timeout=None, allow_redirects=False,131- proxies=None):144+def patch(url, data='', headers=None, files=None, cookies=None, auth=None,145+ timeout=None, allow_redirects=False, params=None, proxies=None):146+ """Sends a PATCH request. Returns :class:`Response` object.147+148+ :param url: URL for the new :class:`Request` object.149+ :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.150+ :param headers: (optional) Dictionary of HTTP Headers to sent with the :class:`Request`.151+ :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.152+ :param cookies: (optional) CookieJar object to send with the :class:`Request`.153+ :param auth: (optional) AuthObject to enable Basic HTTP Auth.154+ :param timeout: (optional) Float describing the timeout of the request.155+ :param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed.156+ :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.157+ :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.158+ """159+160+ return request('PATCH', url,161+ params=params, data=data, headers=headers, files=files,162+ cookies=cookies, auth=auth, timeout=timeout,163+ allow_redirects=allow_redirects, proxies=proxies)164+165+166+def delete(url,167+ params=None, headers=None, cookies=None, auth=None, timeout=None,168+ allow_redirects=False, proxies=None):169+170 """Sends a DELETE request. Returns :class:`Response` object.171 172 :param url: URL for the new :class:`Request` object.173 :param params: (optional) Dictionary of parameters, or bytes, to be sent in the query string for the :class:`Request`.4 unmodified lines178 :param allow_redirects: (optional) Boolean. Set to True if redirect following is allowed.179 :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.180 """181 182+ return request('DELETE', url,144- return request('DELETE', url, params=params, headers=headers, cookies=cookies, auth=auth,145- timeout=timeout, allow_redirects=allow_redirects, proxies=proxies)183+ params=params, headers=headers, cookies=cookies, auth=auth,184+ timeout=timeout, allow_redirects=allow_redirects, proxies=proxies)modified:requests/models.py+5 added-5 removed
26 unmodified lines27 """The :class:`Request <models.Request>` object. It carries out all functionality of28 Requests. Recommended interface is with the Requests functions.29 """30 31- _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE')31+ _METHODS = ('GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH')32 33- def __init__(self, url=None, headers=dict(), files=None, method=None,34- data=dict(), params=dict(), auth=None, cookiejar=None,35- timeout=None, redirect=False, allow_redirects=False,36- proxies=None):33+ def __init__(self,34+ url=None, headers=dict(), files=None, method=None, data=dict(),35+ params=dict(), auth=None, cookiejar=None, timeout=None, redirect=False,36+ allow_redirects=False, proxies=None):37 38 socket.setdefaulttimeout(timeout)39 40 #: Request URL.494 unmodified linesmodified:test_requests.py+15 added-1 removed
126 unmodified lines127 post2 = requests.post(url, files={'some': open('test_requests.py')})128 self.assertEqual(post2.status_code, 200)129 130 post3 = requests.post(url, data='[{"some": "json"}]')131- self.assertEqual(post.status_code, 200)131+ self.assertEqual(post3.status_code, 200)132 133 134+ def test_POSTBIN_GET_PATCH_FILES(self):135+ url = httpbin('patch')136+ patch = requests.patch(url).raise_for_status()137+138+ patch = requests.post(url, data={'some': 'data'})139+ self.assertEqual(patch.status_code, 200)140+141+ patch2 = requests.post(url, files={'some': open('test_requests.py')})142+ self.assertEqual(patch2.status_code, 200)143+144+ patch3 = requests.post(url, data='[{"some": "json"}]')145+ self.assertEqual(patch3.status_code, 200)146+147+148 def test_POSTBIN_GET_POST_FILES_WITH_PARAMS(self):149 150 url = httpbin('post')151 post = requests.post(url, files={'some': open('test_requests.py')}, data={'some': 'data'})163 unmodified lines