|
| 1 | +# |
| 2 | +# Copyright (c) 2017-2018 LabKey Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | +""" |
| 17 | +############################################################################ |
| 18 | +NAME: |
| 19 | +LabKey Storage API |
| 20 | +
|
| 21 | +SUMMARY: |
| 22 | +This module provides functions for interacting with storage items on a LabKey Server. |
| 23 | +
|
| 24 | +DESCRIPTION: |
| 25 | +Create, update, or delete a LabKey Freezer Manager storage item. Storage items can be used in the creation of a |
| 26 | +freezer hierarchy. Freezer hierarchies consist of a top level Freezer, which can have any combination of child |
| 27 | +non-terminal storage locations (i.e. those that do not directly contain samples but can contain other units) and |
| 28 | +terminal storage locations (i.e. units in the freezer that directly contain samples and cannot contain other units). |
| 29 | +
|
| 30 | +Storage items can be of the following types: Physical Location, Freezer, Shelf, Rack, Canister, Storage Unit Type, or Terminal Storage Location. |
| 31 | +The specific set of props will differ for each storage item type: |
| 32 | + - Physical Location: name, description, locationId (rowId of the parent Physical Location) |
| 33 | + - Freezer: name, description, locationId (rowId of the parent Physical Location), manufacturer, freezerModel, temperature, temperatureUnits, serialNumber, sensorName, lossRate, status |
| 34 | + - Shelf/Rack/Canister: name, description, locationId (rowId of the parent freezer or Shelf/Rack/Canister) |
| 35 | + - Storage Unit Type: name, description, unitType (one of the following: "Box", "Plate", "Bag", "Cane", "Tube Rack"), rows, cols (required if positionFormat is not "Num"), positionFormat (one of the following: "Num", "AlphaNum", "AlphaAlpha", "NumAlpha", "NumNum"), positionOrder (one of the following: "RowColumn", "ColumnRow") |
| 36 | + - Terminal Storage Location: name, description, typeId (rowId of the Storage Unit Type), locationId (rowId of the parent freezer or Shelf/Rack/Canister) |
| 37 | +
|
| 38 | +Installation and Setup for the LabKey Python API: |
| 39 | +https://github.com/LabKey/labkey-api-python/blob/master/README.md |
| 40 | +
|
| 41 | +Additional details from Labkey Documentation: |
| 42 | +https://www.labkey.org/SampleManagerHelp/wiki-page.view?name=createFreezer |
| 43 | +https://www.labkey.org/SampleManagerHelp/wiki-page.view?name=freezerLocation |
| 44 | +
|
| 45 | +############################################################################ |
| 46 | +""" |
| 47 | +import functools |
| 48 | +from dataclasses import dataclass |
| 49 | + |
| 50 | +from typing import Union, List |
| 51 | + |
| 52 | +from labkey.server_context import ServerContext |
| 53 | + |
| 54 | +STORAGE_CONTROLLER = "storage" |
| 55 | + |
| 56 | + |
| 57 | +def create_storage_item( |
| 58 | + server_context: ServerContext, type: str, props: dict, container_path: str = None |
| 59 | +): |
| 60 | + """ |
| 61 | + Create a new LabKey Freezer Manager storage item that can be used in the creation of a freezer hierarchy. |
| 62 | + :param server_context: A LabKey server context. See utils.create_server_context. |
| 63 | + :param type: |
| 64 | + :param props: |
| 65 | + :param container_path: |
| 66 | + :return: |
| 67 | + """ |
| 68 | + url = server_context.build_url(STORAGE_CONTROLLER, "create.api", container_path) |
| 69 | + payload = {"type": type, "props": props} |
| 70 | + |
| 71 | + return server_context.make_request(url, json=payload) |
| 72 | + |
| 73 | + |
| 74 | +def update_storage_item( |
| 75 | + server_context: ServerContext, type: str, props: dict, container_path: str = None |
| 76 | +): |
| 77 | + """ |
| 78 | + Update an existing LabKey Freezer Manager storage item to change its properties or location within the freezer hierarchy. |
| 79 | + For update_storage_item, the "rowId" primary key value is required to be set within the props. |
| 80 | + :param server_context: A LabKey server context. See utils.create_server_context. |
| 81 | + :param type: |
| 82 | + :param props: |
| 83 | + :param container_path: |
| 84 | + :return: |
| 85 | + """ |
| 86 | + url = server_context.build_url(STORAGE_CONTROLLER, "update.api", container_path) |
| 87 | + payload = {"type": type, "props": props} |
| 88 | + |
| 89 | + return server_context.make_request(url, json=payload) |
| 90 | + |
| 91 | + |
| 92 | +def delete_storage_item( |
| 93 | + server_context: ServerContext, type: str, row_id: int, container_path: str = None |
| 94 | +): |
| 95 | + """ |
| 96 | + Delete an existing LabKey Freezer Manager storage item. Note that deletion of freezers or locations within the |
| 97 | + freezer hierarchy will cascade the delete down the hierarchy to remove child locations and terminal storage locations. |
| 98 | + Samples in the deleted freezer location(s) will not be deleted but will be removed from storage. |
| 99 | + :param server_context: A LabKey server context. See utils.create_server_context. |
| 100 | + :param type: |
| 101 | + :param row_id: |
| 102 | + :param container_path: |
| 103 | + :return: |
| 104 | + """ |
| 105 | + url = server_context.build_url(STORAGE_CONTROLLER, "delete.api", container_path) |
| 106 | + payload = {"type": type, "props": {"rowId": row_id}} |
| 107 | + |
| 108 | + return server_context.make_request(url, json=payload) |
| 109 | + |
| 110 | + |
| 111 | +class StorageWrapper: |
| 112 | + """ |
| 113 | + Wrapper for all of the API methods exposed in the storage module. Used by the APIWrapper class. |
| 114 | + """ |
| 115 | + |
| 116 | + def __init__(self, server_context: ServerContext): |
| 117 | + self.server_context = server_context |
| 118 | + |
| 119 | + @functools.wraps(create_storage_item) |
| 120 | + def create_storage_item(self, type: str, props: dict, container_path: str = None): |
| 121 | + return create_storage_item(self.server_context, type, props, container_path) |
| 122 | + |
| 123 | + @functools.wraps(update_storage_item) |
| 124 | + def update_storage_item(self, type: str, props: dict, container_path: str = None): |
| 125 | + return update_storage_item(self.server_context, type, props, container_path) |
| 126 | + |
| 127 | + @functools.wraps(delete_storage_item) |
| 128 | + def delete_storage_item(self, type: str, row_id: int, container_path: str = None): |
| 129 | + return delete_storage_item(self.server_context, type, row_id, container_path) |
0 commit comments