Skip to content

Commit

Permalink
Merge pull request #48 from smartystreets/eric/proxy-http-fix
Browse files Browse the repository at this point in the history
Fix Http Proxy Issue
  • Loading branch information
RyanLCox1 authored May 21, 2024
2 parents aa67ca5 + 97ed7eb commit ab9de38
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/us_enrichment_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run():
# https://www.smartystreets.com/docs/cloud/licensing
client = ClientBuilder(credentials).with_licenses(["us-property-data-principal-cloud"]).build_us_enrichment_api_client()
# client = ClientBuilder(credentials).with_custom_header({'User-Agent': 'smartystreets (python@0.0.0)', 'Content-Type': 'application/json'}).build_us_enrichment_api_client()
# client = ClientBuilder(credentials).with_proxy('localhost:8080', 'user', 'password').build_us_street_api_client()
# client = ClientBuilder(credentials).with_http_proxy('localhost:8080', 'user', 'password').build_us_street_api_client()
# Uncomment the line above to try it with a proxy instead

smarty_key = "1682393594"
Expand Down
2 changes: 1 addition & 1 deletion examples/us_street_single_address_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run():
# https://www.smartystreets.com/docs/cloud/licensing
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_proxy('localhost:8080', 'user', 'password').build_us_street_api_client()
# client = ClientBuilder(credentials).with_http_proxy('localhost:8080', 'user', 'password').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
19 changes: 16 additions & 3 deletions smartystreets_python_sdk/client_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,28 @@ def with_base_url(self, base_url):
self.url_prefix = base_url
return self

def with_proxy(self, host, username=None, password=None):
def with_http_proxy(self, host, username=None, password=None):
"""
Assigns a proxy through which to send all Lookups.
Assigns a http proxy through which to send all Lookups.
:param host: The proxy host including port, but not scheme. (example: localhost:8080)
:param username: Username to authenticate with the proxy server
:param password: Password to authenticate with the proxy server
:return: Returns self to accommodate method chaining.
"""
self.proxy = smarty.Proxy(host, username, password)
full_host = 'http://' + host
self.proxy = smarty.Proxy(full_host, username, password)
return self

def with_https_proxy(self, host, username=None, password=None):
"""
Assigns a https proxy through which to send all Lookups.
:param host: The proxy host including port, but not scheme. (example: localhost:8080)
:param username: Username to authenticate with the proxy server
:param password: Password to authenticate with the proxy server
:return: Returns self to accommodate method chaining.
"""
full_host = 'https://' + host
self.proxy = smarty.Proxy(full_host, username, password)
return self

def with_custom_header(self, custom_header):
Expand Down
11 changes: 6 additions & 5 deletions smartystreets_python_sdk/requests_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ def send(self, smarty_request):
def build_proxies(self):
if not self.proxy:
return {}
if not self.proxy.host:
if (self.proxy.host == 'http://' or self.proxy.host =='https://'):
raise smarty.exceptions.SmartyException('Proxy must have a valid host (including port)')

proxy_string = 'https://'
proxy_string = self.proxy.host

if self.proxy.username:
proxy_string += '{}:{}@'.format(self.proxy.username, self.proxy.password)

proxy_string += self.proxy.host

return {'https': proxy_string}
if ('https://' in self.proxy.host):
return {'https': proxy_string}
else:
return {'http': proxy_string, 'https': proxy_string}


def build_request(smarty_request):
Expand Down

0 comments on commit ab9de38

Please sign in to comment.