Skip to content

Commit

Permalink
Merge pull request #52 from smartystreets/spencer/add-x-forward
Browse files Browse the repository at this point in the history
Spencer/add x forward
  • Loading branch information
RyanLCox1 authored May 31, 2024
2 parents 5703c64 + 4006a6b commit 4ac794e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
4 changes: 2 additions & 2 deletions examples/us_street_single_address_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def run():
# The appropriate license values to be used for your subscriptions
# can be found on the Subscriptions page of the account dashboard.
# https://www.smartystreets.com/docs/cloud/licensing
client = ClientBuilder(credentials).with_licenses(["us-core-cloud"]).build_us_street_api_client()
# client = ClientBuilder(credentials).with_licenses(["us-core-cloud"]).build_us_street_api_client()
# client = ClientBuilder(credentials).with_custom_header({'User-Agent': 'smartystreets (python@0.0.0)', 'Content-Type': 'application/json'}).build_us_street_api_client()
# client = ClientBuilder(credentials).with_http_proxy('localhost:8080', 'user', 'password').build_us_street_api_client()
#client = ClientBuilder(credentials).with_http_proxy('localhost').build_us_street_api_client()
# Uncomment the line above to try it with a proxy instead

# Documentation for input fields can be found at:
Expand Down
10 changes: 10 additions & 0 deletions smartystreets_python_sdk/client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, signer):
self.debug = None
self.header = None
self.licenses = []
self.ip = None
self.INTERNATIONAL_STREET_API_URL = "https://international-street.api.smarty.com/verify"
self.INTERNATIONAL_AUTOCOMPLETE_API_URL = "https://international-autocomplete.api.smarty.com/v2/lookup"
self.US_AUTOCOMPLETE_PRO_API_URL = "https://us-autocomplete-pro.api.smarty.com/lookup"
Expand Down Expand Up @@ -132,6 +133,15 @@ def with_licenses(self, licenses):
"""
self.licenses = licenses
return self

def withXForwardedFor(self,ip):
"""
Allows the caller to include an X-Forwarded-For header in their request, passing open the end user's ip address
param: string ip, The IP of the end user
return: returns self to accomodate method chaining
"""
self.ip = ip
return self

def build_international_street_api_client(self):
self.ensure_url_prefix_not_null(self.INTERNATIONAL_STREET_API_URL)
Expand Down
40 changes: 22 additions & 18 deletions smartystreets_python_sdk/requests_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@


class RequestsSender:
def __init__(self, max_timeout=None, proxy=None):
def __init__(self, max_timeout=None, proxy=None, ip = None):
self.session = Session()
self.max_timeout = max_timeout or 10
self.proxy = proxy
self.debug = None

self.ip = ip

def send(self, smarty_request):
request = build_request(smarty_request)
request = RequestsSender.build_request(self,smarty_request)
prepped_request = self.session.prepare_request(request)

prepped_proxies = self.build_proxies()
if self.debug:
print_request_data(prepped_request)
Expand Down Expand Up @@ -43,21 +45,23 @@ def build_proxies(self):
return {'http': proxy_string, 'https': proxy_string}


def build_request(smarty_request):
try:
request = Request(url=smarty_request.url_prefix, params=smarty_request.parameters)
request.headers['User-Agent'] = "smartystreets (sdk:python@{})".format(version.__version__)
request.headers['Content-Type'] = smarty_request.content_type
if smarty_request.referer:
request.headers['Referer'] = smarty_request.referer
if smarty_request.payload:
request.data = smarty_request.payload
request.method = 'POST'
else:
request.method = 'GET'
return request
except AttributeError:
return smarty_request
def build_request(self,smarty_request):
try:
request = Request(url=smarty_request.url_prefix, params=smarty_request.parameters)
request.headers['User-Agent'] = "smartystreets (sdk:python@{})".format(version.__version__)
request.headers['Content-Type'] = smarty_request.content_type
if smarty_request.referer:
request.headers['Referer'] = smarty_request.referer
if self.ip:
request.headers['X-Forwarded-For'] = self.ip
if smarty_request.payload:
request.data = smarty_request.payload
request.method = 'POST'
else:
request.method = 'GET'
return request
except AttributeError:
return smarty_request


def build_smarty_response(inner_response, error=None):
Expand Down
27 changes: 27 additions & 0 deletions test/XForwardedForTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
import smartystreets_python_sdk as smarty
from test.mocks import *


class XForwardedForTest (unittest.TestCase):
def testNativeSetOnQuery(self):
smartyrequest = smarty.Request()
smartyrequest.url_prefix = "http://localhost"
smartyrequest.payload = "Test Payload"
sender = smarty.RequestsSender(10000,None,"0.0.0.0")
request = smarty.RequestsSender.build_request(sender,smartyrequest)

self.assertEqual("0.0.0.0",request.headers['X-Forwarded-For'])



def testNativeNotSet(self):
smartyrequest = smarty.Request()
smartyrequest.url_prefix = "http://localhost"
smartyrequest.payload = "Test Payload"
sender = smarty.RequestsSender()
request = smarty.RequestsSender.build_request(sender,smartyrequest)

self.assertFalse('X-Forwarded-For' in request.headers)


2 changes: 1 addition & 1 deletion test/custom_header_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_custom_headers_used(self):
smartyrequest.payload = "This is the test content."

request = sender.build_request(smartyrequest)

request = smarty.requests_sender.build_request(request)

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

0 comments on commit 4ac794e

Please sign in to comment.