Skip to content

Commit

Permalink
Improve APIClient and handling of sublattice dispatches
Browse files Browse the repository at this point in the history
  • Loading branch information
cjao committed Jan 4, 2024
1 parent c4b1319 commit a86d9d6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
10 changes: 5 additions & 5 deletions covalent/_api/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, dispatcher_addr: str, adapter: HTTPAdapter = None, auto_raise
self.adapter = adapter
self.auto_raise = auto_raise

def prepare_headers(self, **kwargs):
def prepare_headers(self, kwargs):
extra_headers = CovalentAPIClient.get_extra_headers()
headers = kwargs.get("headers", {})
if headers:
Expand All @@ -42,7 +42,7 @@ def prepare_headers(self, **kwargs):
return headers

def get(self, endpoint: str, **kwargs):
headers = self.prepare_headers(**kwargs)
headers = self.prepare_headers(kwargs)
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
Expand All @@ -62,7 +62,7 @@ def get(self, endpoint: str, **kwargs):
return r

def put(self, endpoint: str, **kwargs):
headers = self.prepare_headers()
headers = self.prepare_headers(kwargs)
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
Expand All @@ -81,7 +81,7 @@ def put(self, endpoint: str, **kwargs):
return r

def post(self, endpoint: str, **kwargs):
headers = self.prepare_headers()
headers = self.prepare_headers(kwargs)
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
Expand All @@ -100,7 +100,7 @@ def post(self, endpoint: str, **kwargs):
return r

def delete(self, endpoint: str, **kwargs):
headers = self.prepare_headers()
headers = self.prepare_headers(kwargs)

Check warning on line 103 in covalent/_api/apiclient.py

View check run for this annotation

Codecov / codecov/patch

covalent/_api/apiclient.py#L103

Added line #L103 was not covered by tests
url = self.dispatcher_addr + endpoint
try:
with requests.Session() as session:
Expand Down
11 changes: 7 additions & 4 deletions covalent/_dispatcher_plugins/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import tempfile
from copy import deepcopy
from functools import wraps
Expand Down Expand Up @@ -616,14 +617,16 @@ def _upload_asset(local_uri, remote_uri):
local_path = local_uri

with open(local_path, "rb") as reader:
app_log.debug(f"uploading to {remote_uri}")
content_length = os.path.getsize(local_path)
f = furl(remote_uri)
scheme = f.scheme
host = f.host
port = f.port
dispatcher_addr = f"{scheme}://{host}:{port}"
endpoint = str(f.path)
endpoint = f"{str(f.path)}?{str(f.query)}"
api_client = APIClient(dispatcher_addr)

r = api_client.put(endpoint, data=reader)
if content_length == 0:
r = api_client.put(endpoint, headers={"Content-Length": "0"}, data=reader.read())
else:
r = api_client.put(endpoint, data=reader)
r.raise_for_status()
3 changes: 2 additions & 1 deletion covalent/_workflow/electron.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@ def _build_sublattice_graph(sub: Lattice, json_parent_metadata: str, *args, **kw
return recv_manifest.model_dump_json()

except Exception as ex:
if os.environ.get("COVALENT_DISABLE_LEGACY_SUBLATTICES") == "1":
raise

Check warning on line 919 in covalent/_workflow/electron.py

View check run for this annotation

Codecov / codecov/patch

covalent/_workflow/electron.py#L919

Added line #L919 was not covered by tests
# Fall back to legacy sublattice handling
print("Falling back to legacy sublattice handling")
return sub.serialize_to_json()

0 comments on commit a86d9d6

Please sign in to comment.