Skip to content

Commit

Permalink
Merge de92ff5 into 20c287b
Browse files Browse the repository at this point in the history
  • Loading branch information
okraskaj committed Dec 5, 2018
2 parents 20c287b + de92ff5 commit c2bca10
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
37 changes: 21 additions & 16 deletions reana_commons/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,8 @@
# This file is part of REANA.
# Copyright (C) 2018 CERN.
#
# REANA is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# REANA is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# REANA; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
# Suite 330, Boston, MA 02111-1307, USA.
#
# In applying this license, CERN does not waive the privileges and immunities
# granted to it by virtue of its status as an Intergovernmental Organization or
# submit itself to any jurisdiction.
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""REANA REST API base client."""

import json
Expand All @@ -30,22 +16,34 @@
HTTPNotFound)

from reana_commons.config import OPENAPI_SPECS
from reana_commons.errors import REANAServerNotSet


class BaseAPIClient(object):
"""REANA API client code."""

def __init__(self, service, http_client=None):
"""Create an OpenAPI client."""
self._load_config_from_env()
server_url, spec_file = OPENAPI_SPECS[service]
json_spec = self._get_spec(spec_file)
self._client = SwaggerClient.from_spec(
json_spec,
http_client=http_client,
config={'also_return_response': True})
if server_url is None:
raise REANAServerNotSet(
'REANA_SERVER_URL variable is not set'
'REANA client is not connected to any REANA cluster.'
)
self._client.swagger_spec.api_url = server_url
self.server_url = server_url

def _load_config_from_env(self):
"""."""
OPENAPI_SPECS['reana-server'] = (os.getenv('REANA_SERVER_URL'),
'reana_server.json')

def _get_spec(self, spec_file):
"""Get json specification from package data."""
spec_file_path = os.path.join(
Expand Down Expand Up @@ -132,3 +130,10 @@ def check_if_cached(self, job_spec, step, workflow_workspace):
raise HTTPInternalServerError('Internal Server Error. Error: {}'.
format(http_response.data))
return http_response


def get_current_api_client(component):
"""Return current state of the search extension."""
rwc_api_client = BaseAPIClient(component)

return rwc_api_client._client
2 changes: 1 addition & 1 deletion reana_commons/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
port=os.getenv('WORKFLOW_CONTROLLER_SERVICE_PORT_HTTP', '5000')),
'reana_workflow_controller.json'),
'reana-server': (
os.getenv('REANA_SERVER_URL', '0.0.0.0'),
os.getenv('REANA_SERVER_URL'),
'reana_server.json'),
'reana-job-controller': (
'http://{address}:{port}'.format(
Expand Down
12 changes: 12 additions & 0 deletions reana_commons/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
#
# This file is part of REANA.
# Copyright (C) 2018 CERN.
#
# REANA is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.
"""REANA Commons errors."""


class REANAServerNotSet(Exception):
"""REANA Server URL is not set."""

0 comments on commit c2bca10

Please sign in to comment.