Skip to content

Commit

Permalink
Mem (2/3): migrate core to new DAL
Browse files Browse the repository at this point in the history
  • Loading branch information
cjao committed Jul 11, 2023
1 parent 475f2fb commit c7b87ed
Show file tree
Hide file tree
Showing 86 changed files with 7,990 additions and 3,662 deletions.
6 changes: 5 additions & 1 deletion covalent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
from ._dispatcher_plugins import local_redispatch as redispatch # nopycln: import
from ._dispatcher_plugins import stop_triggers # nopycln: import
from ._file_transfer import strategies as fs_strategies # nopycln: import
from ._results_manager.results_manager import cancel, get_result, sync # nopycln: import
from ._results_manager.results_manager import ( # nopycln: import
cancel,
get_result,
get_result_manager,
)
from ._shared_files.config import get_config, reload_config, set_config # nopycln: import
from ._shared_files.util_classes import RESULT_STATUS as status # nopycln: import
from ._workflow import ( # nopycln: import
Expand Down
19 changes: 19 additions & 0 deletions covalent/_api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2023 Agnostiq Inc.
#
# This file is part of Covalent.
#
# Licensed under the GNU Affero General Public License 3.0 (the "License").
# A copy of the License may be obtained with this software package or at
#
# https://www.gnu.org/licenses/agpl-3.0.en.html
#
# Use of this file is prohibited except in compliance with the License. Any
# modifications or derivative works of this file must retain this copyright
# notice, and modified files must contain a notice indicating that they have
# been altered from the originals.
#
# Covalent 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 License for more details.
#
# Relief from the License may be granted by purchasing a commercial license.
105 changes: 105 additions & 0 deletions covalent/_api/apiclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Copyright 2023 Agnostiq Inc.
#
# This file is part of Covalent.
#
# Licensed under the GNU Affero General Public License 3.0 (the "License").
# A copy of the License may be obtained with this software package or at
#
# https://www.gnu.org/licenses/agpl-3.0.en.html
#
# Use of this file is prohibited except in compliance with the License. Any
# modifications or derivative works of this file must retain this copyright
# notice, and modified files must contain a notice indicating that they have
# been altered from the originals.
#
# Covalent 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 License for more details.
#
# Relief from the License may be granted by purchasing a commercial license.


"""API client"""

import json
import os
from typing import Dict

import requests
from requests.adapters import HTTPAdapter


class CovalentAPIClient:
"""Thin wrapper around Requests to centralize error handling."""

def __init__(self, dispatcher_addr: str, adapter: HTTPAdapter = None, auto_raise: bool = True):
self.dispatcher_addr = dispatcher_addr
self.adapter = adapter
self.auto_raise = auto_raise

def get(self, endpoint: str, **kwargs):
headers = CovalentAPIClient.get_extra_headers()
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
if self.adapter:
session.mount("http://", self.adapter)

r = session.get(url, headers=headers, **kwargs)

if self.auto_raise:
r.raise_for_status()

except requests.exceptions.ConnectionError:
message = f"The Covalent server cannot be reached at {url}. Local servers can be started using `covalent start` in the terminal. If you are using a remote Covalent server, contact your systems administrator to report an outage."
print(message)
raise

return r

def put(self, endpoint: str, **kwargs):
headers = CovalentAPIClient.get_extra_headers()
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
if self.adapter:
session.mount("http://", self.adapter)

r = session.put(url, headers=headers, **kwargs)

if self.auto_raise:
r.raise_for_status()
except requests.exceptions.ConnectionError:
message = f"The Covalent server cannot be reached at {url}. Local servers can be started using `covalent start` in the terminal. If you are using a remote Covalent server, contact your systems administrator to report an outage."
print(message)
raise

return r

def post(self, endpoint: str, **kwargs):
headers = CovalentAPIClient.get_extra_headers()
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
if self.adapter:
session.mount("http://", self.adapter)

r = session.post(url, headers=headers, **kwargs)

if self.auto_raise:
r.raise_for_status()
except requests.exceptions.ConnectionError:
message = f"The Covalent server cannot be reached at {url}. Local servers can be started using `covalent start` in the terminal. If you are using a remote Covalent server, contact your systems administrator to report an outage."
print(message)
raise

return r

@classmethod
def get_extra_headers(headers: Dict) -> Dict:
# This is expected to be a JSONified dictionary
data = os.environ.get("COVALENT_EXTRA_HEADERS")
if data:
return json.loads(data)
else:
return {}
Loading

0 comments on commit c7b87ed

Please sign in to comment.