Skip to content

Commit 266cfac

Browse files
committed
Make server_context the first argument
1 parent 10b6fb1 commit 266cfac

File tree

5 files changed

+54
-45
lines changed

5 files changed

+54
-45
lines changed

labkey/experiment.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# print("Load an Assay batch from the server")
3333
# assay_id = # provide one from your server
3434
# batch_id = # provide one from your server
35-
# run_group = load_batch(assay_id, batch_id, server_context)
35+
# run_group = load_batch(server_context, assay_id, batch_id)
3636
#
3737
# if run_group is not None:
3838
# print("Batch Id: " + str(run_group.id))
@@ -44,23 +44,24 @@
4444
# run_group.properties[batch_property_name] = batch_property_value
4545
#
4646
# print("Save the batch")
47-
# save_batch(assay_id, run_group, server_context)
47+
# save_batch(server_context, assay_id, run_group)
4848

4949
# --------
5050
# /EXAMPLE
5151

5252
# TODO Incorporate logging
5353

54-
def load_batch(assay_id, batch_id, server_context):
54+
def load_batch(server_context, assay_id, batch_id):
5555
"""
5656
Loads a batch from the server.
57+
:param server_context: A LabKey server context. See utils.create_server_context.
5758
:param assay_id: The protocol id of the assay from which to load a batch.
5859
:param batch_id:
59-
:param server_context: A LabKey server context. See utils.create_server_context.
6060
:return:
6161
"""
62-
load_batch_url = build_url('assay', 'getAssayBatch.api', server_context)
62+
load_batch_url = build_url(server_context, 'assay', 'getAssayBatch.api')
6363
session = server_context['session']
64+
loaded_batch = None
6465

6566
payload = {
6667
'assayId': assay_id,
@@ -76,39 +77,38 @@ def load_batch(assay_id, batch_id, server_context):
7677
response = session.post(load_batch_url, data=json.dumps(payload), headers=headers)
7778
json_body = handle_response(response)
7879
if json_body is not None:
79-
return Batch.from_data(json_body['batch'])
80+
loaded_batch = Batch.from_data(json_body['batch'])
8081
except SSLError as e:
8182
raise Exception("Failed to match server SSL configuration. Failed to load batch.")
8283

83-
return None
84+
return loaded_batch
8485

8586

86-
def save_batch(assay_id, batch, server_context):
87+
def save_batch(server_context, assay_id, batch):
8788
"""
8889
Saves a modified batch.
90+
:param server_context: A LabKey server context. See utils.create_server_context.
8991
:param assay_id: The assay protocol id.
9092
:param batch: The Batch to save.
91-
:param server_context: A LabKey server context. See utils.create_server_context.
9293
:return:
9394
"""
94-
result = save_batches(assay_id,[batch],server_context)
95+
result = save_batches(server_context, assay_id, [batch])
9596

9697
if result is not None:
9798
return result[0]
98-
else:
99-
return None
99+
return None
100100

101101

102-
def save_batches(assay_id, batches, server_context):
102+
def save_batches(server_context, assay_id, batches):
103103
"""
104104
Saves a modified batches.
105-
:param assay_id: The assay protocol id.
106-
:param batch: The Batch(es) to save.
107105
:param server_context: A LabKey server context. See utils.create_server_context.
106+
:param assay_id: The assay protocol id.
107+
:param batches: The Batch(es) to save.
108108
:return:
109109
"""
110110

111-
save_batch_url = build_url('assay', 'saveAssayBatch.api', server_context)
111+
save_batch_url = build_url(server_context, 'assay', 'saveAssayBatch.api')
112112
session = server_context['session']
113113

114114
json_batches = []
@@ -145,7 +145,7 @@ def save_batches(assay_id, batches, server_context):
145145

146146
class ExpObject(object):
147147
def __init__(self, **kwargs):
148-
self.lsid = kwargs.pop('lsid', None)
148+
self.lsid = kwargs.pop('lsid', None) # Life Science identifier
149149
self.name = kwargs.pop('name', None)
150150
self.id = kwargs.pop('id', 0)
151151
self.row_id = self.id

labkey/query.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
}
5656

5757

58-
def delete_rows(schema_name, query_name, rows, server_context, container_path=None):
59-
url = build_url('query', 'deleteRows.api', server_context, container_path=container_path)
58+
def delete_rows(server_context, schema_name, query_name, rows, container_path=None):
59+
url = build_url(server_context, 'query', 'deleteRows.api', container_path=container_path)
6060

6161
payload = {
6262
'schemaName': schema_name,
@@ -68,9 +68,9 @@ def delete_rows(schema_name, query_name, rows, server_context, container_path=No
6868
return delete_rows_response
6969

7070

71-
def execute_sql(schema_name, sql, server_context, container_path=None,
71+
def execute_sql(server_context, schema_name, sql, container_path=None,
7272
max_rows=None, sort=None, offset=None, container_filter=None):
73-
url = build_url('query', 'executeSql.api', server_context, container_path=container_path)
73+
url = build_url(server_context, 'query', 'executeSql.api', container_path=container_path)
7474

7575
payload = {
7676
'schemaName': schema_name,
@@ -93,8 +93,8 @@ def execute_sql(schema_name, sql, server_context, container_path=None,
9393
return execute_sql_response
9494

9595

96-
def insert_rows(schema_name, query_name, rows, server_context, container_path=None):
97-
url = build_url('query', 'insertRows.api', server_context, container_path=container_path)
96+
def insert_rows(server_context, schema_name, query_name, rows, container_path=None):
97+
url = build_url(server_context, 'query', 'insertRows.api', container_path=container_path)
9898

9999
payload = {
100100
'schemaName': schema_name,
@@ -107,11 +107,11 @@ def insert_rows(schema_name, query_name, rows, server_context, container_path=No
107107

108108

109109
# TODO: Support all the properties
110-
def select_rows(schema_name, query_name, server_context, view_name=None,
110+
def select_rows(server_context, schema_name, query_name, view_name=None,
111111
filter_array=None, container_path=None, columns=None, max_rows=None, sort=None,
112112
offset=None, container_filter=None):
113113
# TODO: Support data_region_name
114-
url = build_url('query', 'getQuery.api', server_context, container_path=container_path)
114+
url = build_url(server_context, 'query', 'getQuery.api', container_path=container_path)
115115

116116
payload = {
117117
'schemaName': schema_name,
@@ -146,8 +146,8 @@ def select_rows(schema_name, query_name, server_context, view_name=None,
146146
return select_rows_response
147147

148148

149-
def update_rows(schema_name, query_name, rows, server_context, container_path=None):
150-
url = build_url('query', 'updateRows.api', server_context, container_path=container_path)
149+
def update_rows(server_context, schema_name, query_name, rows, container_path=None):
150+
url = build_url(server_context, 'query', 'updateRows.api', container_path=container_path)
151151

152152
payload = {
153153
'schemaName': schema_name,

labkey/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ def init_poolmanager(self, connections, maxsize, block=False):
3333

3434

3535
def create_server_context(domain, container_path, context_path=None, use_ssl=True):
36-
# TODO: Document
36+
"""
37+
Create a LabKey server context. This context is used to encapsulate properties
38+
about the LabKey server that is being requested against. This includes, but is not limited to,
39+
the domain, container_path, and if the server is using SSL.
40+
:param domain:
41+
:param container_path:
42+
:param context_path:
43+
:param use_ssl:
44+
:return:
45+
"""
3746
server_context = dict(domain=domain, container_path=container_path, context_path=context_path)
3847

3948
if use_ssl:
@@ -55,13 +64,13 @@ def create_server_context(domain, container_path, context_path=None, use_ssl=Tru
5564
return server_context
5665

5766

58-
def build_url(controller, action, server_context, container_path=None):
67+
def build_url(server_context, controller, action, container_path=None):
5968
"""
6069
Builds a URL from a controller and an action. Users the server context to determine domain,
6170
context path, container, etc.
71+
:param server_context: A LabKey server context. See utils.create_server_context.
6272
:param controller: The controller to use in building the URL
6373
:param action: The action to use in building the URL
64-
:param server_context:
6574
:param container_path:
6675
:return:
6776
"""

samples/load_batch_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
print("Load an Assay batch from the server")
1111
assay_id = 1168 # provide one from your server
1212
batch_id = 95 # provide one from your server
13-
run_group = load_batch(assay_id, batch_id, server_context)
13+
run_group = load_batch(server_context, assay_id, batch_id)
1414

1515
if run_group is not None:
1616
print("Batch Id: " + str(run_group.id))

samples/save_batch_example.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
dataRows = [
1414
{
1515
# ColumnName : Value
16-
"SampleId": "Monkey 1"
17-
, "TimePoint": "2008/11/02 11:22:33"
18-
, "DoubleData": 4.5
19-
, "HiddenData": "another data point"
16+
"SampleId": "Monkey 1",
17+
"TimePoint": "2008/11/02 11:22:33",
18+
"DoubleData": 4.5,
19+
"HiddenData": "another data point"
2020
}, {
21-
"SampleId": "Monkey 2"
22-
, "TimePoint": "2008/11/02 14:00:01"
23-
, "DoubleData": 3.1
24-
, "HiddenData": "fozzy bear"
21+
"SampleId": "Monkey 2",
22+
"TimePoint": "2008/11/02 14:00:01",
23+
"DoubleData": 3.1,
24+
"HiddenData": "fozzy bear"
2525
}, {
26-
"SampleId": "Monkey 3"
27-
, "TimePoint": "2008/11/02 14:00:01"
28-
, "DoubleData": 1.5
29-
, "HiddenData": "jimbo"
26+
"SampleId": "Monkey 3",
27+
"TimePoint": "2008/11/02 14:00:01",
28+
"DoubleData": 1.5,
29+
"HiddenData": "jimbo"
3030
}
3131
]
3232

@@ -43,7 +43,7 @@
4343
batch.properties['PropertyName'] = 'Property Value'
4444

4545
# Execute save api
46-
RESULT = save_batch(assay_id, batch, server_context)
47-
# RESULT = save_batches(assay_id, [batch1, batch2], server_context)
46+
RESULT = save_batch(server_context, assay_id, batch)
47+
# RESULT = save_batches(server_context, assay_id, [batch1, batch2])
4848
print(RESULT.id)
4949

0 commit comments

Comments
 (0)