Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.

Commit 6726d60

Browse files
committed
Add http_provider_with_proxy
1 parent b806b39 commit 6726d60

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
'''
2+
------------------------------------------------------------------------------
3+
Copyright (c) 2015 Microsoft Corporation
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+
------------------------------------------------------------------------------
23+
'''
24+
from __future__ import unicode_literals, with_statement
25+
import requests
26+
from onedrivesdk import http_provider_base, http_response
27+
28+
29+
class HttpProviderWithProxy(http_provider_base.HttpProviderBase):
30+
"""Use this HttpProvider when you want to proxy your requests.
31+
For example, if you have an HTTP request capture suite, you
32+
can use this provider to proxy the requests through that
33+
capture suite.
34+
"""
35+
36+
DEFAULT_PROXIES = {
37+
'http': 'http://127.0.0.1:8888',
38+
'https': 'https://127.0.0.1:8888'
39+
}
40+
41+
def __init__(self, proxies=None, verify_ssl=True):
42+
"""Initializes the provider. Proxy and SSL settings are stored
43+
in the object and applied to every request.
44+
45+
Args:
46+
proxies (dict of str:str):
47+
Mapping of protocols to proxy URLs. See `requests`
48+
documentation:
49+
http://docs.python-requests.org/en/latest/api/#requests.request
50+
If None, HttpProviderWithProxy.DEFAULT_PROXIES is used.
51+
verify_ssl (bool):
52+
Whether SSL certs should be verified during
53+
request proxy.
54+
"""
55+
self.proxies = proxies if proxies is not None \
56+
else HttpProviderWithProxy.DEFAULT_PROXIES
57+
self.verify_ssl = verify_ssl
58+
59+
def send(self, method, headers, url, data=None, content=None, path=None):
60+
"""Send the built request using all the specified
61+
parameters.
62+
63+
Args:
64+
method (str): The HTTP method to use (ex. GET)
65+
headers (dict of (str, str)): A dictionary of name-value
66+
pairs for headers in the request
67+
url (str): The URL for the request to be sent to
68+
data (str): Defaults to None, data to include in the body
69+
of the request which is not in JSON format
70+
content (dict): Defaults to None, a dictionary to include
71+
in JSON format in the body of the request
72+
path (str): Defaults to None, the path to the local file
73+
to send in the body of the request
74+
75+
Returns:
76+
:class:`HttpResponse<onedrivesdk.http_response.HttpResponse>`:
77+
The response to the request
78+
"""
79+
session = requests.Session()
80+
81+
if path:
82+
with open(path, mode='rb') as f:
83+
request = requests.Request(method,
84+
url,
85+
headers=headers,
86+
data=f)
87+
prepped = request.prepare()
88+
response = session.send(prepped,
89+
verify=self.verify_ssl,
90+
proxies=self.proxies)
91+
else:
92+
request = requests.Request(method,
93+
url,
94+
headers=headers,
95+
data=data,
96+
json=content)
97+
prepped = request.prepare()
98+
response = session.send(prepped,
99+
verify=self.verify_ssl,
100+
proxies=self.proxies)
101+
102+
custom_response = http_response.HttpResponse(response.status_code, response.headers, response.text)
103+
return custom_response
104+
105+
def download(self, headers, url, path):
106+
"""Downloads a file to the stated path
107+
108+
Args:
109+
headers (dict of (str, str)): A dictionary of name-value
110+
pairs to be used as headers in the request
111+
url (str): The URL from which to download the file
112+
path (str): The local path to save the downloaded file
113+
114+
Returns:
115+
:class:`HttpResponse<onedrivesdk.http_response.HttpResponse>`:
116+
The response to the request
117+
"""
118+
response = requests.get(
119+
url,
120+
stream=True,
121+
headers=headers,
122+
verify=self.verify_ssl,
123+
proxies=self.proxies)
124+
125+
if response.status_code == 200:
126+
with open(path, 'wb') as f:
127+
for chunk in response.iter_content(chunk_size=1024):
128+
if chunk:
129+
f.write(chunk)
130+
f.flush()
131+
custom_response = http_response.HttpResponse(response.status_code, response.headers, None)
132+
else:
133+
custom_response = http_response.HttpResponse(response.status_code, response.headers, response.text)
134+
135+
return custom_response

0 commit comments

Comments
 (0)