forked from burnash/gspread
/
httpsession.py
87 lines (62 loc) · 2.32 KB
/
httpsession.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
"""
gspread.httpsession
~~~~~~~~~~~~~~~~~~~
This module contains a class for working with http sessions.
"""
import requests
try:
import httplib as client
from urlparse import urlparse
from urllib import urlencode
except ImportError:
from http import client
from urllib.parse import urlparse
from urllib.parse import urlencode
try:
unicode
except NameError:
basestring = unicode = str
from .exceptions import HTTPError
class HTTPSession(object):
"""Handles HTTP activity while keeping headers persisting across requests.
:param headers: A dict with initial headers.
"""
def __init__(self, headers=None):
self.headers = headers or {}
def request(self, method, url, data=None, headers=None):
if data and isinstance(data, bytes):
data = data.decode()
if data and not isinstance(data, basestring):
data = urlencode(data)
if data is not None:
data = data.encode('utf8')
# If we have data and Content-Type is not set, set it...
if data and not headers.get('Content-Type', None):
headers['Content-Type'] = 'application/x-www-form-urlencoded'
request_headers = self.headers.copy()
if headers:
for k, v in headers.items():
if v is None:
del request_headers[k]
else:
request_headers[k] = v
try:
func = getattr(requests, method.lower())
except AttributeError:
raise Exception("HTTP method '{}' is not supported".format(method))
response = func(url, data=data, headers=request_headers)
if response.status_code > 399:
raise HTTPError(response.status_code, "{}: {}".format(
response.status_code, response.content))
return response
def get(self, url, **kwargs):
return self.request('GET', url, **kwargs)
def delete(self, url, **kwargs):
return self.request('DELETE', url, **kwargs)
def post(self, url, data=None, headers={}):
return self.request('POST', url, data=data, headers=headers)
def put(self, url, data=None, **kwargs):
return self.request('PUT', url, data=data, **kwargs)
def add_header(self, name, value):
self.headers[name] = value