Skip to content

Commit 4ac794e

Browse files
authored
Merge pull request #52 from smartystreets/spencer/add-x-forward
Spencer/add x forward
2 parents 5703c64 + 4006a6b commit 4ac794e

File tree

5 files changed

+62
-21
lines changed

5 files changed

+62
-21
lines changed

examples/us_street_single_address_example.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ def run():
2626
# The appropriate license values to be used for your subscriptions
2727
# can be found on the Subscriptions page of the account dashboard.
2828
# https://www.smartystreets.com/docs/cloud/licensing
29-
client = ClientBuilder(credentials).with_licenses(["us-core-cloud"]).build_us_street_api_client()
29+
# client = ClientBuilder(credentials).with_licenses(["us-core-cloud"]).build_us_street_api_client()
3030
# client = ClientBuilder(credentials).with_custom_header({'User-Agent': 'smartystreets ([email protected])', 'Content-Type': 'application/json'}).build_us_street_api_client()
31-
# client = ClientBuilder(credentials).with_http_proxy('localhost:8080', 'user', 'password').build_us_street_api_client()
31+
#client = ClientBuilder(credentials).with_http_proxy('localhost').build_us_street_api_client()
3232
# Uncomment the line above to try it with a proxy instead
3333

3434
# Documentation for input fields can be found at:

smartystreets_python_sdk/client_builder.py

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(self, signer):
2626
self.debug = None
2727
self.header = None
2828
self.licenses = []
29+
self.ip = None
2930
self.INTERNATIONAL_STREET_API_URL = "https://international-street.api.smarty.com/verify"
3031
self.INTERNATIONAL_AUTOCOMPLETE_API_URL = "https://international-autocomplete.api.smarty.com/v2/lookup"
3132
self.US_AUTOCOMPLETE_PRO_API_URL = "https://us-autocomplete-pro.api.smarty.com/lookup"
@@ -132,6 +133,15 @@ def with_licenses(self, licenses):
132133
"""
133134
self.licenses = licenses
134135
return self
136+
137+
def withXForwardedFor(self,ip):
138+
"""
139+
Allows the caller to include an X-Forwarded-For header in their request, passing open the end user's ip address
140+
param: string ip, The IP of the end user
141+
return: returns self to accomodate method chaining
142+
"""
143+
self.ip = ip
144+
return self
135145

136146
def build_international_street_api_client(self):
137147
self.ensure_url_prefix_not_null(self.INTERNATIONAL_STREET_API_URL)

smartystreets_python_sdk/requests_sender.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66

77
class RequestsSender:
8-
def __init__(self, max_timeout=None, proxy=None):
8+
def __init__(self, max_timeout=None, proxy=None, ip = None):
99
self.session = Session()
1010
self.max_timeout = max_timeout or 10
1111
self.proxy = proxy
1212
self.debug = None
13-
13+
self.ip = ip
14+
1415
def send(self, smarty_request):
15-
request = build_request(smarty_request)
16+
request = RequestsSender.build_request(self,smarty_request)
1617
prepped_request = self.session.prepare_request(request)
18+
1719
prepped_proxies = self.build_proxies()
1820
if self.debug:
1921
print_request_data(prepped_request)
@@ -43,21 +45,23 @@ def build_proxies(self):
4345
return {'http': proxy_string, 'https': proxy_string}
4446

4547

46-
def build_request(smarty_request):
47-
try:
48-
request = Request(url=smarty_request.url_prefix, params=smarty_request.parameters)
49-
request.headers['User-Agent'] = "smartystreets (sdk:python@{})".format(version.__version__)
50-
request.headers['Content-Type'] = smarty_request.content_type
51-
if smarty_request.referer:
52-
request.headers['Referer'] = smarty_request.referer
53-
if smarty_request.payload:
54-
request.data = smarty_request.payload
55-
request.method = 'POST'
56-
else:
57-
request.method = 'GET'
58-
return request
59-
except AttributeError:
60-
return smarty_request
48+
def build_request(self,smarty_request):
49+
try:
50+
request = Request(url=smarty_request.url_prefix, params=smarty_request.parameters)
51+
request.headers['User-Agent'] = "smartystreets (sdk:python@{})".format(version.__version__)
52+
request.headers['Content-Type'] = smarty_request.content_type
53+
if smarty_request.referer:
54+
request.headers['Referer'] = smarty_request.referer
55+
if self.ip:
56+
request.headers['X-Forwarded-For'] = self.ip
57+
if smarty_request.payload:
58+
request.data = smarty_request.payload
59+
request.method = 'POST'
60+
else:
61+
request.method = 'GET'
62+
return request
63+
except AttributeError:
64+
return smarty_request
6165

6266

6367
def build_smarty_response(inner_response, error=None):

test/XForwardedForTest.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
import smartystreets_python_sdk as smarty
3+
from test.mocks import *
4+
5+
6+
class XForwardedForTest (unittest.TestCase):
7+
def testNativeSetOnQuery(self):
8+
smartyrequest = smarty.Request()
9+
smartyrequest.url_prefix = "http://localhost"
10+
smartyrequest.payload = "Test Payload"
11+
sender = smarty.RequestsSender(10000,None,"0.0.0.0")
12+
request = smarty.RequestsSender.build_request(sender,smartyrequest)
13+
14+
self.assertEqual("0.0.0.0",request.headers['X-Forwarded-For'])
15+
16+
17+
18+
def testNativeNotSet(self):
19+
smartyrequest = smarty.Request()
20+
smartyrequest.url_prefix = "http://localhost"
21+
smartyrequest.payload = "Test Payload"
22+
sender = smarty.RequestsSender()
23+
request = smarty.RequestsSender.build_request(sender,smartyrequest)
24+
25+
self.assertFalse('X-Forwarded-For' in request.headers)
26+
27+

test/custom_header_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_custom_headers_used(self):
3434
smartyrequest.payload = "This is the test content."
3535

3636
request = sender.build_request(smartyrequest)
37-
37+
3838
request = smarty.requests_sender.build_request(request)
3939

4040
self.assertEqual('Test-Agent', request.headers['User-Agent'])

0 commit comments

Comments
 (0)