about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
authorFrederick Muriuki Muriithi2025-06-05 15:05:38 -0500
committerFrederick Muriuki Muriithi2025-06-05 15:09:17 -0500
commitf030152daee508647097b2e541be5bbc38f27330 (patch)
treeeacb0ce6116177a6c5fde23b466b2f60afef524e /scripts
parentb6c27b907ddb3a1aa7d97632fcc9aa36901df459 (diff)
downloadgn-uploader-f030152daee508647097b2e541be5bbc38f27330.tar.gz
`update_auth()` scaffolding
Add untested scaffolding for how the `update_auth()` function will
work. Include notes to help with figuring the feature out.
Diffstat (limited to 'scripts')
-rw-r--r--scripts/load_phenotypes_to_db.py76
1 files changed, 74 insertions, 2 deletions
diff --git a/scripts/load_phenotypes_to_db.py b/scripts/load_phenotypes_to_db.py
index d5d2d16..18f9792 100644
--- a/scripts/load_phenotypes_to_db.py
+++ b/scripts/load_phenotypes_to_db.py
@@ -3,14 +3,16 @@ import uuid
 import json
 import logging
 import argparse
+import datetime
 from pathlib import Path
 from zipfile import ZipFile
 from typing import Any, Union
+from urllib.parse import urljoin
 from functools import reduce, partial
 
 from MySQLdb.cursors import Cursor, DictCursor
 
-from gn_libs import jobs, mysqldb, sqlite3
+from gn_libs import jobs, mysqldb, sqlite3, monadic_requests as mrequests
 
 from r_qtl import r_qtl2 as rqtl2
 from uploader.species.models import species_by_id
@@ -227,7 +229,77 @@ def cross_reference_phenotypes_publications_and_data(
 
 def update_auth(authserver, token, species, population, dataset, xrefdata):
     """Grant the user access to their data."""
-    raise NotImplemented("Please implement this!")
+    # TODO Call into the auth server to:
+    #      1. Link the phenotypes with a user group
+    #         - fetch group: http://localhost:8081/auth/user/group
+    #         - link data to group: http://localhost:8081/auth/data/link/phenotype
+    #         - *might need code update in gn-auth: remove restriction, perhaps*
+    #      2. Create resource (perhaps?)
+    #         - Get resource categories: http://localhost:8081/auth/resource/categories
+    #         - Create a new resource: http://localhost:80host:8081/auth/resource/create
+    #           - single resource for all phenotypes
+    #           - resource name from user, species, population, dataset, datetime?
+    #         - User will have "ownership" of resource by default
+    #      3. Link data to the resource: http://localhost:8081/auth/resource/data/link
+    #         - Update code to allow linking multiple items in a single request
+    _tries = 0 # TODO use this to limit how many tries before quiting and bailing
+    _delay = 1
+    headers = {
+        "Authorization": f"Bearer {token}",
+        "Content-Type": "application/json"
+    }
+    def authserveruri(endpoint):
+        return urljoin(authserver, endpoint)
+
+    def __fetch_user_details__():
+        return mrequests.get(
+            authserveruri("/auth/user/"),
+            headers=headers
+        )
+
+    def __link_data__(user, group):
+        return mrequests.post(
+            authserveruri("/auth/data/link/phenotype"),
+            headers=headers,
+            data={
+                "group_id": user["group"]["group_id"],
+                "traits": []# TODO: process these here
+            }).then(lambda ld_results: (user, ld_results))
+
+    def __fetch_phenotype_category_details__(user, linkeddata):
+        return mrequests.get(
+            authserveruri("/auth/resource/categories"),
+            headers=headers
+        ).then(
+            lambda categories: (
+                user,
+                linkeddata,
+                next(category for category in categories
+                     if category["resource_category_key"] == "phenotype"))
+        )
+
+    def __create_resource__(user, linkeddata, category):
+        now = datetime.datetime.now().isoformat()
+        return mrequests.post(
+            authserveruri("/auth/resource/create"),
+            headers=headers,
+            data={
+                "resource_category": category["resource_category_id"],
+                "resource_name": (f"{user['email']}—{dataset['name']}—{now}—"
+                                  f"{len(xrefdata)} phenotypes"),
+                "public": "off"
+            }).then(lambda cr_results: (user, linkeddata, resource))
+
+    def __attach_data_to_resource__(user, linkeddata, resource):
+        return mrequests.post(
+            authserveruri("/auth/resource/data/link"),
+            headers=headers,
+            data={
+                "dataset_type": "phenotype",
+                "resource_id": resource["resource_id"],
+                "data_link_ids": [item["data_link_id"] for item in linkeddata]
+            }).then(lambda attc: (user, linkeddata, resource, attc))
+    raise NotImplementedError("Please implement this!")
 
 
 def load_data(conn: mysqldb.Connection, job: dict) -> int: