def get_headers(): """ Using session credentials/config, get headers for a V2 API request. Users may have their own proxy layer and so we free up the `authorization` header for this purpose (instead adding the user authorization in a new `plotly-authorization` header). See pull #239. :returns: (dict) Headers to add to a requests.request call. """ from plotly import version creds = config.get_credentials() headers = { "plotly-client-platform": "python {}".format(version.stable_semver()), "content-type": "application/json", } plotly_auth = basic_auth(creds["username"], creds["api_key"]) proxy_auth = basic_auth(creds["proxy_username"], creds["proxy_password"]) if config.get_config()["plotly_proxy_authorization"]: headers["authorization"] = proxy_auth if creds["username"] and creds["api_key"]: headers["plotly-authorization"] = plotly_auth else: if creds["username"] and creds["api_key"]: headers["authorization"] = plotly_auth return headers
def get_headers(): """ Using session credentials/config, get headers for a V2 API request. Users may have their own proxy layer and so we free up the `authorization` header for this purpose (instead adding the user authorization in a new `plotly-authorization` header). See pull #239. :returns: (dict) Headers to add to a requests.request call. """ creds = config.get_credentials() headers = { 'plotly-client-platform': 'python {}'.format(version.stable_semver()), 'content-type': 'application/json' } plotly_auth = basic_auth(creds['username'], creds['api_key']) proxy_auth = basic_auth(creds['proxy_username'], creds['proxy_password']) if config.get_config()['plotly_proxy_authorization']: headers['authorization'] = proxy_auth if creds['username'] and creds['api_key']: headers['plotly-authorization'] = plotly_auth else: if creds['username'] and creds['api_key']: headers['authorization'] = plotly_auth return headers
def clientresp(data, **kwargs): """ Deprecated endpoint, still used because it can parse data out of a plot. When we get around to forcing users to create grids and then create plots, we can finally get rid of this. :param (list) data: The data array from a figure. """ creds = config.get_credentials() cfg = config.get_config() dumps_kwargs = {'sort_keys': True, 'cls': utils.PlotlyJSONEncoder} payload = { 'platform': 'python', 'version': version.stable_semver(), 'args': _json.dumps(data, **dumps_kwargs), 'un': creds['username'], 'key': creds['api_key'], 'origin': 'plot', 'kwargs': _json.dumps(kwargs, **dumps_kwargs) } url = '{plotly_domain}/clientresp'.format(**cfg) response = request('post', url, data=payload) # Old functionality, just keeping it around. parsed_content = response.json() if parsed_content.get('warning'): warnings.warn(parsed_content['warning']) if parsed_content.get('message'): print(parsed_content['message']) return response
def test_normal_auth(self): headers = utils.get_headers() expected_headers = { 'plotly-client-platform': 'python {}'.format(version.stable_semver()), 'authorization': 'Basic Zm9vOmJhcg==', 'content-type': 'application/json' } self.assertEqual(headers, expected_headers)
def test_normal_auth(self): headers = utils.get_headers() expected_headers = { "plotly-client-platform": "python {}".format(version.stable_semver()), "authorization": "Basic Zm9vOmJhcg==", "content-type": "application/json", } self.assertEqual(headers, expected_headers)
def test_proxy_auth(self): sign_in(self.username, self.api_key, plotly_proxy_authorization=True) headers = utils.get_headers() expected_headers = { 'plotly-client-platform': 'python {}'.format(version.stable_semver()), 'authorization': 'Basic Y25ldDpob29wbGE=', 'plotly-authorization': 'Basic Zm9vOmJhcg==', 'content-type': 'application/json' } self.assertEqual(headers, expected_headers)
def test_proxy_auth(self): sign_in(self.username, self.api_key, plotly_proxy_authorization=True) headers = utils.get_headers() expected_headers = { "plotly-client-platform": "python {}".format(version.stable_semver()), "authorization": "Basic Y25ldDpob29wbGE=", "plotly-authorization": "Basic Zm9vOmJhcg==", "content-type": "application/json", } self.assertEqual(headers, expected_headers)
def test_data_only(self): data = [{'y': [3, 5], 'name': Duck()}] clientresp(data) assert self.request_mock.call_count == 1 args, kwargs = self.request_mock.call_args method, url = args self.assertEqual(method, 'post') self.assertEqual(url, '{}/clientresp'.format(self.plotly_domain)) expected_data = ({ 'origin': 'plot', 'args': '[{"name": "what else floats?", "y": [3, 5]}]', 'platform': 'python', 'version': version.stable_semver(), 'key': 'bar', 'kwargs': '{}', 'un': 'foo' }) self.assertEqual(kwargs['data'], expected_data) self.assertTrue(kwargs['verify']) self.assertEqual(kwargs['headers'], {})
def test_data_and_kwargs(self): data = [{'y': [3, 5], 'name': Duck()}] clientresp_kwargs = {'layout': {'title': 'mah plot'}, 'filename': 'ok'} clientresp(data, **clientresp_kwargs) assert self.request_mock.call_count == 1 args, kwargs = self.request_mock.call_args method, url = args self.assertEqual(method, 'post') self.assertEqual(url, '{}/clientresp'.format(self.plotly_domain)) expected_data = ({ 'origin': 'plot', 'args': '[{"name": "what else floats?", "y": [3, 5]}]', 'platform': 'python', 'version': version.stable_semver(), 'key': 'bar', 'kwargs': '{"filename": "ok", "layout": {"title": "mah plot"}}', 'un': 'foo' }) self.assertEqual(kwargs['data'], expected_data) self.assertTrue(kwargs['verify']) self.assertEqual(kwargs['headers'], {})