Skip to content

Commit

Permalink
api: move the creation of open api clients to reana-commons
Browse files Browse the repository at this point in the history
  • Loading branch information
okraskaj committed Dec 6, 2018
1 parent 20c287b commit 44cadba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
36 changes: 20 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,33 @@
HTTPNotFound)

from reana_commons.config import OPENAPI_SPECS
from reana_commons.errors import MissingAPIClientConfiguration


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 MissingAPIClientConfiguration(
'Configuration to connect to {} is missing.'.format(service)
)
self._client.swagger_spec.api_url = server_url
self.server_url = server_url

def _load_config_from_env(self):
"""Override configuration from environment."""
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 +129,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 MissingAPIClientConfiguration(Exception):
"""REANA Server URL is not set."""

0 comments on commit 44cadba

Please sign in to comment.