Skip to content

Commit 8601e72

Browse files
authoredOct 22, 2020
Add APIWrapper, Remove Python 2 Support (#42)
- container.create: rename folderType arg to folder_type, rename isWorkbook arg to is_workbook - Remove support for Python 2.x - Add Type annotations - We don't have 100% of our API methods typed yet, but we are getting there - Format code with Black (a static code formatter) - remove build_url helper - it was just a single line wrapper around server_context - remove create_server_context - It was just a wrapper around ServerContext, you can replace all usages of create_server_context with ServerContext - Add APIWrapper - This wraps all of the supported APIs so you don't need to pass around a server_context - Removed various "from_data" methods - They were all simple one line wrappers around Class constructors that were not needed if you were using any you may update your code e.g. "SomeClass.from_data(data)" can be changed to "SomeClass(**data)" - Remove unsupported modules - Update example code
1 parent 11d143f commit 8601e72

33 files changed

+3414
-2580
lines changed
 

‎CHANGE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
LabKey Python Client API News
33
+++++++++++
44

5+
What's New in the LabKey 2.0.0 package
6+
==============================
7+
8+
*Release date: 10/22/2020*
9+
- container.create: rename folderType arg to folder_type, rename isWorkbook arg to is_workbook
10+
- Remove support for Python 2.x
11+
- Add Type annotations
12+
- We don't have 100% of our API methods typed yet, but we are getting there
13+
- Format code with Black (a static code formatter)
14+
- remove build_url helper
15+
- it was just a single line wrapper around server_context
16+
- remove create_server_context
17+
- It was just a wrapper around ServerContext, you can replace all usages of create_server_context with ServerContext
18+
- Add APIWrapper
19+
- This wraps all of the supported APIs so you don't need to pass around a server_context
20+
- Removed various "from_data" methods
21+
- They were all simple one line wrappers around Class constructors that were not needed if you were using any you
22+
may update your code e.g. "SomeClass.from_data(data)" can be changed to "SomeClass(**data)"
23+
- Remove unsupported modules
24+
- Update example code
25+
526
What's New in the LabKey 1.4.1 package
627
==============================
728

‎README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,18 @@ Sample code is available in the [samples](https://github.com/LabKey/labkey-api-p
8080
The following gets data from the Users table on your local machine:
8181

8282
```python
83-
from labkey.utils import create_server_context
84-
from labkey.query import select_rows
83+
from labkey.api_wrapper import APIWrapper
8584

86-
print("Create a server context")
85+
print("Create an APIWrapper")
8786
labkey_server = 'localhost:8080'
8887
project_name = 'ModuleAssayTest' # Project folder name
8988
contextPath = 'labkey'
9089
schema = 'core'
9190
table = 'Users'
91+
api = APIWrapper(labkey_server, project_name, contextPath, use_ssl=False)
9292

93-
server_context = create_server_context(labkey_server, project_name, contextPath, use_ssl=False)
93+
result = api.query.select_rows(schema, table)
9494

95-
result = select_rows(server_context, schema, table)
9695
if result is not None:
9796
print(result['rows'][0])
9897
print("select_rows: Number of rows returned: " + str(result['rowCount']))
@@ -101,11 +100,17 @@ else:
101100
```
102101

103102
## Supported Versions
104-
Python 2.6+ and 3.4+ are fully supported.
103+
Python 3.6+ is fully supported.
105104
LabKey Server v15.1 and later.
106105

107106
## Contributing
108-
This package is maintained by [LabKey](http://www.labkey.com/). If you have any questions or need support, please use the [LabKey Server developer support forum](https://www.labkey.org/home/developer/forum/project-start.view).
107+
This package is maintained by [LabKey](http://www.labkey.com/). If you have any questions or need support, please use
108+
the [LabKey Server developer support forum](https://www.labkey.org/home/developer/forum/project-start.view).
109+
110+
When contributing changes please use `Black` to format your code. To run Black follow these instructions:
111+
1. Install black: `pip install black`
112+
2. Run black: `black .`
113+
3. Commit the newly formatted code.
109114

110115
### Testing
111116
If you are looking to contribute please run the tests before issuing a PR. The tests can be initiated by running

‎labkey/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#
1616
from labkey import domain, query, experiment, security, utils
1717

18-
__title__ = 'labkey'
19-
__version__ = '1.4.1'
20-
__author__ = 'LabKey'
21-
__license__ = 'Apache License 2.0'
18+
__title__ = "labkey"
19+
__version__ = "2.0.0"
20+
__author__ = "LabKey"
21+
__license__ = "Apache License 2.0"

‎labkey/api_wrapper.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from .container import ContainerWrapper
2+
from .domain import DomainWrapper
3+
from .experiment import ExperimentWrapper
4+
from .query import QueryWrapper
5+
from .security import SecurityWrapper
6+
from .server_context import ServerContext
7+
8+
9+
class APIWrapper:
10+
"""
11+
Wrapper for all of the supported API methods in the Python Client API. Makes it easier to use
12+
the supported API methods without having to manually pass around a ServerContext object.
13+
"""
14+
15+
def __init__(
16+
self,
17+
domain,
18+
container_path,
19+
context_path=None,
20+
use_ssl=True,
21+
verify_ssl=True,
22+
api_key=None,
23+
disable_csrf=False,
24+
):
25+
self.server_context = ServerContext(
26+
domain=domain,
27+
container_path=container_path,
28+
context_path=context_path,
29+
use_ssl=use_ssl,
30+
verify_ssl=verify_ssl,
31+
api_key=api_key,
32+
disable_csrf=disable_csrf,
33+
)
34+
self.container = ContainerWrapper(self.server_context)
35+
self.domain = DomainWrapper(self.server_context)
36+
self.experiment = ExperimentWrapper(self.server_context)
37+
self.query = QueryWrapper(self.server_context)
38+
self.security = SecurityWrapper(self.server_context)

‎labkey/container.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,72 @@
1-
from labkey.utils import json_dumps, ServerContext
1+
from .server_context import ServerContext
2+
from labkey.utils import json_dumps
23

34

4-
def create(server_context, name, container_path=None, description=None, folderType=None, isWorkbook=None, title=None):
5-
# type: (ServerContext, str, str, str, str, bool, str) -> dict
5+
def create(
6+
server_context: ServerContext,
7+
name: str,
8+
container_path: str = None,
9+
description: str = None,
10+
folder_type: str = None,
11+
is_workbook: bool = None,
12+
title: str = None,
13+
) -> dict:
614
"""
715
Create a container in LabKey.
816
917
:param server_context: A LabKey server context. See utils.create_server_context.
1018
:param name: The name of the container.
1119
:param container_path: the path of where you want to create the container.
1220
:param description: a description for the container.
13-
:param folderType: the desired folder type for the container.
14-
:param isWorkbook: sets whether the container is a workbook.
21+
:param folder_type: the desired folder type for the container.
22+
:param is_workbook: sets whether the container is a workbook.
1523
:param title: the title for the container.
1624
:return:
1725
"""
18-
headers = {'Content-Type': 'application/json'}
19-
url = server_context.build_url('core', 'createContainer.api', container_path)
26+
headers = {"Content-Type": "application/json"}
27+
url = server_context.build_url("core", "createContainer.api", container_path)
2028
payload = {
21-
'description': description,
22-
'folderType': folderType,
23-
'isWorkbook': isWorkbook,
24-
'name': name,
25-
'title': title,
29+
"description": description,
30+
"folderType": folder_type,
31+
"isWorkbook": is_workbook,
32+
"name": name,
33+
"title": title,
2634
}
2735
return server_context.make_request(url, json_dumps(payload), headers=headers)
2836

2937

30-
def delete(server_context, container_path=None):
31-
# type: (ServerContext, str)
38+
def delete(server_context: ServerContext, container_path: str = None) -> any:
3239
"""
3340
Deletes a container at the given container_path, or at the server_context's container path
3441
:param server_context: A LabKey server context. See utils.create_server_context.
3542
:param container_path: The path of the container to delete.
3643
:return:
3744
"""
38-
headers = {'Content-Type': 'application/json'}
39-
url = server_context.build_url('core', 'deleteContainer.api', container_path)
45+
headers = {"Content-Type": "application/json"}
46+
url = server_context.build_url("core", "deleteContainer.api", container_path)
4047
return server_context.make_request(url, None, headers=headers)
48+
49+
50+
class ContainerWrapper:
51+
"""
52+
Wrapper for all of the API methods exposed in the container module. Used by the APIWrapper class.
53+
"""
54+
55+
def __init__(self, server_context: ServerContext):
56+
self.server_context = server_context
57+
58+
def create(
59+
self,
60+
name: str,
61+
container_path: str = None,
62+
description: str = None,
63+
folder_type: str = None,
64+
is_workbook: bool = None,
65+
title: str = None,
66+
):
67+
return create(
68+
self.server_context, name, container_path, description, folder_type, is_workbook, title
69+
)
70+
71+
def delete(self, container_path: str = None):
72+
return delete(self.server_context, container_path)

0 commit comments

Comments
 (0)
Please sign in to comment.