def test_request_preserves_accept_header(self): """ Test that the accept header is not overwritten and that text is returned """ fake_client = Client() with patch("requests.request") as request: request.return_value.text = "response text" resp_text = fake_client.request( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, ) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, json=None, params=expected_params, ) assert resp_text == "response text"
def test_request_should_return_json_when_json_accept_header_provided(self): """ Test that the requests response.json() decoding is returned when no accept header is provided """ fake_client = Client() with patch("requests.request") as request: request.return_value.json.return_value = {"key": "value"} resp = fake_client.request("GET", "http://www.google.com/", headers={"Accept": "application/json"}) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/json"}, json=None, params=expected_params, stream=False, ) assert resp == {"key": "value"}
def test_request_should_return_byte_stream_when_requested(self): """ Test that the requests response.iter_content(None, False) is returned when stream is requested """ fake_client = Client() with patch("requests.request") as request: request.return_value.iter_content.return_value = [ b"response", b" ", b"text", ] resp = fake_client.request( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, as_stream=True, ) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, json=None, params=expected_params, stream=True, ) request.return_value.iter_content.assert_called_once_with( chunk_size=None, decode_unicode=False) assert resp == [b"response", b" ", b"text"]
def test_request_should_return_bytes_when_non_json_accept_header_provided( self): """ Test that the accept header is preserved and that bytes are returned """ fake_client = Client() with patch("requests.request") as request: request.return_value.content = b"response text" resp = fake_client.request( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, ) expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Accept": "application/fake_encoding"}, json=None, params=expected_params, stream=False, ) assert resp == b"response text"
def __init__(self): """ :param access_token: """ self.client = Client() self.base_url = "https://api.buildkite.com/v2/"
def test_request_should_not_include_token(self): """ Test that the access token is not included in the call to requests if it isn't actually set. """ client = Client() with patch("requests.request") as request: request.return_value.json.return_value = {} client.request("GET", "http://www.google.com/") request.assert_called_once_with( "GET", "http://www.google.com/", headers=None, json=None, params={} )
def test_clean_query_params(self): """ Test that params with None are cleaned """ original_query_params = { "name": "FAKE-NAME", "hostname": "FAKE-HOSTNAME", "version": None, } cleaned_query_params = {"name": "FAKE-NAME", "hostname": "FAKE-HOSTNAME"} client = Client() original_query_params = client._clean_query_params(original_query_params) assert original_query_params == cleaned_query_params
class BuildKite(object): def __init__(self): """ :param access_token: """ self.client = Client() self.base_url = "https://api.buildkite.com/v2/" def set_access_token(self, access_token): """ :param access_token: :return: """ self.client.set_client_access_token(access_token) def requires_token(func): def wrapper(self, *args, **kwargs): if not self.client.is_access_token_set(): raise NoAcccessTokenException else: return func(self, *args, **kwargs) return wrapper @requires_token def organizations(self): return Organizations(self.client, self.base_url) @requires_token def pipelines(self): return Pipelines(self.client, self.base_url) @requires_token def builds(self): return Builds(self.client, self.base_url) @requires_token def jobs(self): return Jobs(self.client, self.base_url) @requires_token def agents(self): return Agents(self.client, self.base_url)
def test_request_should_include_token_when_set(self): """ Test that the access token is not included in the call to requests if it isn't actually set. """ fake_client = Client() fake_client.set_client_access_token("ABCDEF1234") with patch("requests.request") as request: request.return_value.json.return_value = {"key": "value"} ret = fake_client.request("GET", "http://www.google.com/") expected_params = b"per_page=100" request.assert_called_once_with( "GET", "http://www.google.com/", headers={"Authorization": "Bearer ABCDEF1234"}, json=None, params=expected_params, )
def test_access_token_setting(self): """ Test functionality of is_acces_token_set """ client = Client() assert not client.is_access_token_set() client.set_client_access_token("FAKE-TOKEN") assert client.is_access_token_set()
def test_request_should_include_token_when_set(self): """ Test that the access token is not included in the call to requests if it isn't actually set. """ client = Client() client.set_client_access_token("ABCDEF1234") with patch("requests.request") as request: request.return_value.json.return_value = {} client.request("GET", "http://www.google.com/") expected_params = {"access_token": "ABCDEF1234"} request.assert_called_once_with( "GET", "http://www.google.com/", headers=None, json=None, params=expected_params, )
def __init__(self): """ Create a new client """ self.client = Client() self.base_url = "https://api.buildkite.com/v2/"
class Buildkite(object): """ Public API for Buildkite """ def __init__(self): """ Create a new client """ self.client = Client() self.base_url = "https://api.buildkite.com/v2/" def set_access_token(self, access_token): """ Set the access token to be used to authenticate the requests :param access_token: The access token """ self.client.set_client_access_token(access_token) @requires_token def organizations(self): """ Get Organization operations for the Buildkite API :return: Client """ return Organizations(self.client, self.base_url) @requires_token def pipelines(self): """ Get Pipeline operations for the Buildkite API :return: Client """ return Pipelines(self.client, self.base_url) @requires_token def builds(self): """ Get Build operations for the Buildkite API :return: Client """ return Builds(self.client, self.base_url) @requires_token def jobs(self): """ Get Job operations for the Buildkite API :return: Client """ return Jobs(self.client, self.base_url) @requires_token def agents(self): """ Get Agent operations for the Buildkite API :return: Client """ return Agents(self.client, self.base_url) @requires_token def emojis(self): """ Get Emoji operations for the Buildkite API """ return Emojis(self.client, self.base_url) @requires_token def annotations(self): """ Get Annotation operations for the Buildkite API """ return Annotations(self.client, self.base_url) @requires_token def artifacts(self): """ Get Artifact operations for the Buildkite API """ return Artifacts(self.client, self.base_url) @requires_token def teams(self): """ Get Team operations for the Buildkite API """ return Teams(self.client, self.base_url) @requires_token def users(self): """ Get User operations for the Buildkite API """ return Users(self.client, self.base_url)
class BuildKite(object): """ Public API for Buildkite """ def __init__(self): """ Create a new client """ self.client = Client() self.base_url = "https://api.buildkite.com/v2/" def set_access_token(self, access_token): """ Set the access token to be used to authenticate the requests :param access_token: The access token """ self.client.set_client_access_token(access_token) def requires_token(func): """ This annotation protects API calls that require authentication. It will cause them to raise NoAcccessTokenException if the token is not set in the client. :return: Function decorated with the protection """ def wrapper(self, *args, **kwargs): """ Call func or raise NoAcccessTokenException if no access token is set :param self: :param args: Optional :param kwargs: Optional :raises NoAcccessTokenException: If no access token is set :return: """ if not self.client.is_access_token_set(): raise NoAcccessTokenException else: return func(self, *args, **kwargs) return wrapper @requires_token def organizations(self): """ Get Organisation operations for the Buildkite API :return: Client """ return Organizations(self.client, self.base_url) @requires_token def pipelines(self): """ Get Pipeline operations for the Buildkite API :return: Client """ return Pipelines(self.client, self.base_url) @requires_token def builds(self): """ Get Build operations for the Buildkite API :return: Client """ return Builds(self.client, self.base_url) @requires_token def jobs(self): """ Get Job operations for the Buildkite API :return: Client """ return Jobs(self.client, self.base_url) @requires_token def agents(self): """ Get Agent operations for the Buildkite API :return: Client """ return Agents(self.client, self.base_url) @requires_token def emojis(self): """ Get Emoji operations for the Buildkite API :return: Client """ return Emojis(self.client, self.base_url) def annotations(self): """ Get Annotation operations for the Buildkite API """ return Annotations(self.client, self.base_url)