From ee50dfaaa3f02ebeb2aa3caf13b8a11b2e1b2676 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Mon, 4 May 2020 22:24:48 -0700 Subject: [PATCH 1/8] Allow custom session in Requestor --- sigopt/interface.py | 3 ++- sigopt/requestor.py | 5 ++++- test/test_interface.py | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/sigopt/interface.py b/sigopt/interface.py index 23fc5bee..33dd9edb 100644 --- a/sigopt/interface.py +++ b/sigopt/interface.py @@ -274,7 +274,7 @@ class Connection(object): Client-facing interface for creating Connections. Shouldn't be changed without a major version change. """ - def __init__(self, client_token=None, user_agent=None): + def __init__(self, client_token=None, user_agent=None, session=None): client_token = client_token or os.environ.get('SIGOPT_API_TOKEN') api_url = os.environ.get('SIGOPT_API_URL') or DEFAULT_API_URL if not client_token: @@ -289,6 +289,7 @@ def __init__(self, client_token=None, user_agent=None): client_token, '', default_headers, + session=session, ) self.impl = ConnectionImpl(requestor, api_url=api_url) diff --git a/sigopt/requestor.py b/sigopt/requestor.py index 94fc0c68..984b1b4b 100644 --- a/sigopt/requestor.py +++ b/sigopt/requestor.py @@ -16,6 +16,7 @@ def __init__( proxies=None, timeout=DEFAULT_HTTP_TIMEOUT, client_ssl_certs=None, + session=None, ): self._set_auth(user, password) self.default_headers = headers or {} @@ -23,6 +24,7 @@ def __init__( self.proxies = proxies self.timeout = timeout self.client_ssl_certs = client_ssl_certs + self.session = session def _set_auth(self, username, password): if username is not None: @@ -48,7 +50,8 @@ def delete(self, url, params=None, json=None, headers=None): def request(self, method, url, params=None, json=None, headers=None): headers = self._with_default_headers(headers) try: - response = requests.request( + caller = (self.session or requests) + response = caller.request( method=method, url=url, params=params, diff --git a/test/test_interface.py b/test/test_interface.py index 3775b513..92bfe6f9 100644 --- a/test/test_interface.py +++ b/test/test_interface.py @@ -1,6 +1,7 @@ import os import pytest import mock +import requests from sigopt.interface import Connection from sigopt.requestor import DEFAULT_HTTP_TIMEOUT @@ -11,11 +12,16 @@ def test_create(self): conn = Connection(client_token='client_token') assert conn.impl.api_url == 'https://api.sigopt.com' assert conn.impl.requestor.verify_ssl_certs is True + assert conn.impl.requestor.session is None assert conn.impl.requestor.proxies is None assert conn.impl.requestor.timeout == DEFAULT_HTTP_TIMEOUT assert isinstance(conn.clients, ApiResource) assert isinstance(conn.experiments, ApiResource) + def test_create_uses_session_if_provided(self): + conn = Connection(client_token='client_token', session=requests.Session()) + assert conn.impl.requestor.session is not None + def test_environment_variable(self): with mock.patch.dict(os.environ, {'SIGOPT_API_TOKEN': 'client_token'}): Connection() From 7888866b016b5876b36dcde45df58cb9094411ce Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Mon, 4 May 2020 22:33:38 -0700 Subject: [PATCH 2/8] Use `None` default for verify --- sigopt/requestor.py | 2 +- test/test_interface.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sigopt/requestor.py b/sigopt/requestor.py index 984b1b4b..70c24f22 100644 --- a/sigopt/requestor.py +++ b/sigopt/requestor.py @@ -12,7 +12,7 @@ def __init__( user, password, headers, - verify_ssl_certs=True, + verify_ssl_certs=None, proxies=None, timeout=DEFAULT_HTTP_TIMEOUT, client_ssl_certs=None, diff --git a/test/test_interface.py b/test/test_interface.py index 92bfe6f9..47a584f6 100644 --- a/test/test_interface.py +++ b/test/test_interface.py @@ -11,7 +11,7 @@ class TestInterface(object): def test_create(self): conn = Connection(client_token='client_token') assert conn.impl.api_url == 'https://api.sigopt.com' - assert conn.impl.requestor.verify_ssl_certs is True + assert conn.impl.requestor.verify_ssl_certs is None assert conn.impl.requestor.session is None assert conn.impl.requestor.proxies is None assert conn.impl.requestor.timeout == DEFAULT_HTTP_TIMEOUT From 3ddbec094ebf084b7ed4bd158b04737e678500c1 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Tue, 5 May 2020 15:39:15 -0700 Subject: [PATCH 3/8] Better test --- test/test_interface.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/test_interface.py b/test/test_interface.py index 47a584f6..aa894d09 100644 --- a/test/test_interface.py +++ b/test/test_interface.py @@ -19,8 +19,9 @@ def test_create(self): assert isinstance(conn.experiments, ApiResource) def test_create_uses_session_if_provided(self): - conn = Connection(client_token='client_token', session=requests.Session()) - assert conn.impl.requestor.session is not None + session = requests.Session() + conn = Connection(client_token='client_token', session=session) + assert conn.impl.requestor.session is session def test_environment_variable(self): with mock.patch.dict(os.environ, {'SIGOPT_API_TOKEN': 'client_token'}): From 496e3acb6dfa804b7fe3e7766fc5e1468ff425dc Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Tue, 5 May 2020 15:46:52 -0700 Subject: [PATCH 4/8] Update test --- requirements-dev.txt | 2 +- test/test_interface.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 14c6464e..6a91ce0c 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,4 @@ # For continuous integration and development -mock==1.0.1 +mock==4.0.2 pytest==2.8.7 twine==1.9.1 diff --git a/test/test_interface.py b/test/test_interface.py index aa894d09..9ee82d20 100644 --- a/test/test_interface.py +++ b/test/test_interface.py @@ -1,7 +1,6 @@ import os import pytest import mock -import requests from sigopt.interface import Connection from sigopt.requestor import DEFAULT_HTTP_TIMEOUT @@ -19,10 +18,18 @@ def test_create(self): assert isinstance(conn.experiments, ApiResource) def test_create_uses_session_if_provided(self): - session = requests.Session() + session = mock.Mock() conn = Connection(client_token='client_token', session=session) assert conn.impl.requestor.session is session + response = mock.Mock() + session.request.return_value = response + response.status_code = 200 + response.text = '{}' + session.request.assert_not_called() + conn.experiments().fetch() + session.request.assert_called_once() + def test_environment_variable(self): with mock.patch.dict(os.environ, {'SIGOPT_API_TOKEN': 'client_token'}): Connection() From 4251c0077b1ee07cb83431c2f34ea9bd9044452c Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Tue, 5 May 2020 16:21:52 -0700 Subject: [PATCH 5/8] Support old pythons --- requirements-dev.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-dev.txt b/requirements-dev.txt index 6a91ce0c..f37a8f83 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,4 +1,4 @@ # For continuous integration and development -mock==4.0.2 +mock>=3.0.5 pytest==2.8.7 twine==1.9.1 From b89ee4a2a01b0ac5ddde3dc4106bf1b756b5ab79 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Tue, 5 May 2020 16:24:45 -0700 Subject: [PATCH 6/8] acknowledgments --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index ea855e28..8424b864 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,9 @@ To lint, install requirements (included in the previous step) and run ```bash make lint ``` + +## Acknowledgmnts + +We would like to thank the following people for their contributions: + +- @aadamson for their contributions in supporting custom `requests.Session` objects (#170) From a5a31f43c5b713cd7ce3f5407c345c42dfe89c5a Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Tue, 5 May 2020 16:28:05 -0700 Subject: [PATCH 7/8] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8424b864..b9da231f 100644 --- a/README.md +++ b/README.md @@ -104,4 +104,4 @@ make lint We would like to thank the following people for their contributions: -- @aadamson for their contributions in supporting custom `requests.Session` objects (#170) +- [@aadamson](github.com/aadamson) for their contributions in supporting custom `requests.Session` objects [#170](https://github.com/sigopt/sigopt-python/pull/170) From f8b99230bef272fcaab912a6cceab1cb27fa9110 Mon Sep 17 00:00:00 2001 From: Patrick Hayes Date: Tue, 5 May 2020 16:28:49 -0700 Subject: [PATCH 8/8] fix link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b9da231f..cacf77d5 100644 --- a/README.md +++ b/README.md @@ -104,4 +104,4 @@ make lint We would like to thank the following people for their contributions: -- [@aadamson](github.com/aadamson) for their contributions in supporting custom `requests.Session` objects [#170](https://github.com/sigopt/sigopt-python/pull/170) +- [@aadamson](https://github.com/aadamson) for their contributions in supporting custom `requests.Session` objects [#170](https://github.com/sigopt/sigopt-python/pull/170)