diff options
-rw-r--r-- | wqflask/wqflask/requests.py | 16 | ||||
-rw-r--r-- | wqflask/wqflask/views.py | 52 |
2 files changed, 50 insertions, 18 deletions
diff --git a/wqflask/wqflask/requests.py b/wqflask/wqflask/requests.py new file mode 100644 index 00000000..43c8001f --- /dev/null +++ b/wqflask/wqflask/requests.py @@ -0,0 +1,16 @@ +"""requests but with monads""" +import requests +from pymonad.either import Left, Right, Either + +def __wrap_response__(resp) -> Either: + if resp.status_code == 200: + return Right(resp) + return Left(resp) + +def get(url, params=None, **kwargs) -> Either: + """Wrap requests get method with Either monad""" + return __wrap_response__(requests.get(url, params=params, **kwargs)) + +def post(url, data=None, json=None, **kwargs) -> Either: + """Wrap requests post method with Either monad""" + return __wrap_response__(requests.post(url, data=data, json=json, **kwargs)) diff --git a/wqflask/wqflask/views.py b/wqflask/wqflask/views.py index 30d0f872..4e14c980 100644 --- a/wqflask/wqflask/views.py +++ b/wqflask/wqflask/views.py @@ -108,6 +108,8 @@ import jobs.jobs as jobs from wqflask.oauth2.session import session_info from wqflask.oauth2.checks import user_logged_in +from wqflask import requests as monad_requests + Redis = get_redis_conn() @@ -1179,31 +1181,45 @@ def edit_case_attributes(inbredset_id: int) -> Response: edit_case_attributes_page = redirect(url_for( "edit_case_attributes", inbredset_id=inbredset_id)) - return client.post( - f"case-attribute/{inbredset_id}/edit", + token = session_info()["user"]["token"].either( + lambda err: err, lambda tok: tok["access_token"]) + return monad_requests.post( + urljoin( + current_app.config["GN_SERVER_URL"], + f"/api/case-attribute/{inbredset_id}/edit"), json={ "edit-data": reduce(__process_data__, form.items(), {}) - }).either( + }, + headers={ + "Authorization": f"Bearer {token}"}).either( with_flash_error(edit_case_attributes_page), with_flash_success(edit_case_attributes_page)) def __fetch_strains__(inbredset_group): - return client.get(f"case-attribute/{inbredset_id}/strains").then( - lambda strains: {**inbredset_group, "strains": strains}) + return monad_requests.get(urljoin( + current_app.config["GN_SERVER_URL"], + f"/api/case-attribute/{inbredset_id}/strains")).then( + lambda resp: {**inbredset_group, "strains": resp.json()}) def __fetch_names__(strains): - return client.get(f"case-attribute/{inbredset_id}/names").then( - lambda canames: {**strains, "case_attribute_names": canames}) + return monad_requests.get(urljoin( + current_app.config["GN_SERVER_URL"], + f"/api/case-attribute/{inbredset_id}/names")).then( + lambda resp: {**strains, "case_attribute_names": resp.json()}) def __fetch_values__(canames): - return client.get(f"case-attribute/{inbredset_id}/values").then( - lambda cavalues: {**canames, "case_attribute_values": { - value["StrainName"]: value for value in cavalues}}) - - return client.get(f"case-attribute/{inbredset_id}").then( - lambda iset_group: {"inbredset_group": iset_group}).then( - __fetch_strains__).then(__fetch_names__).then( - __fetch_values__).either( - lambda err: err, ## TODO: Handle error better - lambda values: render_template( - "edit_case_attributes.html", inbredset_id=inbredset_id, **values)) + return monad_requests.get(urljoin( + current_app.config["GN_SERVER_URL"], + f"/api/case-attribute/{inbredset_id}/values")).then( + lambda resp: {**canames, "case_attribute_values": { + value["StrainName"]: value for value in resp.json()}}) + + return monad_requests.get(urljoin( + current_app.config["GN_SERVER_URL"], + f"/api/case-attribute/{inbredset_id}")).then( + lambda resp: {"inbredset_group": resp.json()}).then( + __fetch_strains__).then(__fetch_names__).then( + __fetch_values__).either( + lambda err: err, ## TODO: Handle error better + lambda values: render_template( + "edit_case_attributes.html", inbredset_id=inbredset_id, **values)) |