diff --git a/renga/api/client.py b/renga/api/client.py index e0ee98163c..2dc8120905 100644 --- a/renga/api/client.py +++ b/renga/api/client.py @@ -63,19 +63,24 @@ class APIClient( __attrs__ = requests.Session.__attrs__ + ['access_token', 'endpoint'] - def __init__(self, endpoint, access_token=None, **kwargs): + def __init__(self, endpoint=None, **kwargs): """Create a storage client.""" self.endpoint = endpoint + super(APIClient, self).__init__(**kwargs) - if endpoint.startswith('http:'): - os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'FIXME' - warnings.warn('Using insecure trasnport protocol, use HTTPS') + @property + def endpoint(self): + """Return endpoint value.""" + return getattr(self, '_endpoint', None) - super(APIClient, self).__init__(**kwargs) + @endpoint.setter + def endpoint(self, endpoint): + """Set and validate endpoint.""" + self._endpoint = endpoint - if access_token: - # NOTE used by storage service - self.access_token = access_token + if endpoint is not None and endpoint.startswith('http:'): + os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'FIXME' + warnings.warn('Using insecure trasnport protocol, use HTTPS') def _url(self, url, *args, **kwargs): """Format url for endpoint.""" diff --git a/renga/client.py b/renga/client.py index 142d4e3471..897c8ef41e 100644 --- a/renga/client.py +++ b/renga/client.py @@ -28,7 +28,7 @@ class RengaClient(object): Example: >>> import renga - >>> client = renga.RengaClient('http://localhost', '** TOKEN **') + >>> client = renga.RengaClient('http://localhost') """ @@ -59,7 +59,14 @@ def from_env(cls, environment=None): endpoint = environment.get('RENGA_ENDPOINT', '') access_token = environment.get('RENGA_ACCESS_TOKEN') - return cls(endpoint=endpoint, token={'access_token': access_token}) + client = cls(endpoint=endpoint, token={'access_token': access_token}) + + # FIXME temporary solution until the execution id is moved to the token + execution_id = environment.get('RENGA_VERTEX_ID') + if execution_id: + client.api.headers['Renga-Deployer-Execution'] = execution_id + + return client @property def contexts(self): diff --git a/renga/models/storage.py b/renga/models/storage.py index 961a547887..1e16366205 100644 --- a/renga/models/storage.py +++ b/renga/models/storage.py @@ -65,6 +65,11 @@ def open(self, file_name=None, mode='w'): client = self._client.__class__( self._client.api.endpoint, token={'access_token': access_token}) + if 'Renga-Deployer-Execution' in self._client.api.headers: + client.api.headers[ + 'Renga-Deployer-Execution'] = self._client.api.headers[ + 'Renga-Deployer-Execution'] + file_handle = { 'resource_id': resp['id'], 'request_type': FileHandle.REQUEST_TYPE[mode], @@ -141,6 +146,11 @@ def open(self, mode='r'): } token = self._client.api.storage_authorize(**file_handle) client = self._client.__class__(self._client.api.endpoint, token=token) + if 'Renga-Deployer-Execution' in self._client.api.headers: + client.api.headers[ + 'Renga-Deployer-Execution'] = self._client.api.headers[ + 'Renga-Deployer-Execution'] + yield FileHandle(file_handle, client=client) @@ -169,10 +179,8 @@ def __getitem__(self, resource_id): def __iter__(self): """Return all files in this bucket.""" - return ( - File(f, client=self._client, collection=self) - for f in self._client.api.get_bucket_files(self.id) - ) + return (File(f, client=self._client, collection=self) + for f in self._client.api.get_bucket_files(self.id)) def create(self, file_name=None): """Create an empty file in this bucket.""" diff --git a/renga/version.py b/renga/version.py index a078c0ab9e..9e6994ebd3 100644 --- a/renga/version.py +++ b/renga/version.py @@ -23,4 +23,4 @@ from __future__ import absolute_import, print_function -__version__ = '0.1.0.dev20170914' +__version__ = '0.1.0.dev20170915'