From e3dbdbecfaae525a8863bbbd30be12504e4696bf Mon Sep 17 00:00:00 2001 From: quantumjot Date: Wed, 10 Mar 2021 12:40:24 +0000 Subject: [PATCH] Added API --- cellx/api/__init__.py | 0 cellx/api/v1/__init__.py | 0 cellx/api/v1/submit.py | 63 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 cellx/api/__init__.py create mode 100644 cellx/api/v1/__init__.py create mode 100644 cellx/api/v1/submit.py diff --git a/cellx/api/__init__.py b/cellx/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cellx/api/v1/__init__.py b/cellx/api/v1/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cellx/api/v1/submit.py b/cellx/api/v1/submit.py new file mode 100644 index 0000000..b6713a2 --- /dev/null +++ b/cellx/api/v1/submit.py @@ -0,0 +1,63 @@ +import base64 +import io +import os + +import imageio +import numpy as np +import requests + +# store the URL and token in environmental variables +# DO NOT HARDCODE THEM HERE +API_URL = os.environ["CELLX-API-URL"] +API_TOKEN = os.environ["CELLX-API-TOKEN"] + + +def _base64_png_to_img(x: str) -> np.ndarray: + """Convert a base64 encoded PNG image to a numpy array. + + Parameters + ---------- + x : str + The base64 encoded string + + Returns + ------- + img : np.ndarray + The image as a numpy array + + """ + decode = base64.decodebytes(x.encode("utf-8")) + stream = io.BytesIO(decode) + return imageio.imread(stream, format="png") + + +def _img_to_base64_png(x: np.ndarray) -> str: + """Convert a numpy array to a base64 encoded PNG image. + + Parameters + ---------- + x : str + The base64 encoded string + + Returns + ------- + img : np.ndarray + The image as a numpy array + + """ + stream = io.BytesIO() + imageio.imsave(stream, x, format="png") + stream_data = stream.getvalue() + return base64.b64encode(stream_data).decode("utf-8") + + +def _parse_data(data: dict) -> dict: + """Parse the data before submitting to server.""" + return data + + +def submit_to_server(data: dict): + """Submit the data to the server.""" + parsed = _parse_data(data) + response = requests.post(API_URL, data=parsed) + return response