Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@
LabKey Python Client API News
+++++++++++

What's New in the LabKey 2.0.0 package
==============================

*Release date: 10/22/2020*
- 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

What's New in the LabKey 1.4.1 package
==============================

Expand Down
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,18 @@ Sample code is available in the [samples](https://github.com/LabKey/labkey-api-p
The following gets data from the Users table on your local machine:

```python
from labkey.utils import create_server_context
from labkey.query import select_rows
from labkey.api_wrapper import APIWrapper

print("Create a server context")
print("Create an APIWrapper")
labkey_server = 'localhost:8080'
project_name = 'ModuleAssayTest' # Project folder name
contextPath = 'labkey'
schema = 'core'
table = 'Users'
api = APIWrapper(labkey_server, project_name, contextPath, use_ssl=False)

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

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

## Supported Versions
Python 2.6+ and 3.4+ are fully supported.
Python 3.6+ is fully supported.
LabKey Server v15.1 and later.

## Contributing
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).
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).

When contributing changes please use `Black` to format your code. To run Black follow these instructions:
1. Install black: `pip install black`
2. Run black: `black .`
3. Commit the newly formatted code.

### Testing
If you are looking to contribute please run the tests before issuing a PR. The tests can be initiated by running
Expand Down
8 changes: 4 additions & 4 deletions labkey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
from labkey import domain, query, experiment, security, utils

__title__ = 'labkey'
__version__ = '1.4.1'
__author__ = 'LabKey'
__license__ = 'Apache License 2.0'
__title__ = "labkey"
__version__ = "2.0.0"
__author__ = "LabKey"
__license__ = "Apache License 2.0"
38 changes: 38 additions & 0 deletions labkey/api_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from .container import ContainerWrapper
from .domain import DomainWrapper
from .experiment import ExperimentWrapper
from .query import QueryWrapper
from .security import SecurityWrapper
from .server_context import ServerContext


class APIWrapper:
"""
Wrapper for all of the supported API methods in the Python Client API. Makes it easier to use
the supported API methods without having to manually pass around a ServerContext object.
"""

def __init__(
self,
domain,
container_path,
context_path=None,
use_ssl=True,
verify_ssl=True,
api_key=None,
disable_csrf=False,
):
self.server_context = ServerContext(
domain=domain,
container_path=container_path,
context_path=context_path,
use_ssl=use_ssl,
verify_ssl=verify_ssl,
api_key=api_key,
disable_csrf=disable_csrf,
)
self.container = ContainerWrapper(self.server_context)
self.domain = DomainWrapper(self.server_context)
self.experiment = ExperimentWrapper(self.server_context)
self.query = QueryWrapper(self.server_context)
self.security = SecurityWrapper(self.server_context)
64 changes: 48 additions & 16 deletions labkey/container.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,72 @@
from labkey.utils import json_dumps, ServerContext
from .server_context import ServerContext
from labkey.utils import json_dumps


def create(server_context, name, container_path=None, description=None, folderType=None, isWorkbook=None, title=None):
# type: (ServerContext, str, str, str, str, bool, str) -> dict
def create(
server_context: ServerContext,
name: str,
container_path: str = None,
description: str = None,
folder_type: str = None,
is_workbook: bool = None,
title: str = None,
) -> dict:
"""
Create a container in LabKey.

:param server_context: A LabKey server context. See utils.create_server_context.
:param name: The name of the container.
:param container_path: the path of where you want to create the container.
:param description: a description for the container.
:param folderType: the desired folder type for the container.
:param isWorkbook: sets whether the container is a workbook.
:param folder_type: the desired folder type for the container.
:param is_workbook: sets whether the container is a workbook.
:param title: the title for the container.
:return:
"""
headers = {'Content-Type': 'application/json'}
url = server_context.build_url('core', 'createContainer.api', container_path)
headers = {"Content-Type": "application/json"}
url = server_context.build_url("core", "createContainer.api", container_path)
payload = {
'description': description,
'folderType': folderType,
'isWorkbook': isWorkbook,
'name': name,
'title': title,
"description": description,
"folderType": folder_type,
"isWorkbook": is_workbook,
"name": name,
"title": title,
}
return server_context.make_request(url, json_dumps(payload), headers=headers)


def delete(server_context, container_path=None):
# type: (ServerContext, str)
def delete(server_context: ServerContext, container_path: str = None) -> any:
"""
Deletes a container at the given container_path, or at the server_context's container path
:param server_context: A LabKey server context. See utils.create_server_context.
:param container_path: The path of the container to delete.
:return:
"""
headers = {'Content-Type': 'application/json'}
url = server_context.build_url('core', 'deleteContainer.api', container_path)
headers = {"Content-Type": "application/json"}
url = server_context.build_url("core", "deleteContainer.api", container_path)
return server_context.make_request(url, None, headers=headers)


class ContainerWrapper:
"""
Wrapper for all of the API methods exposed in the container module. Used by the APIWrapper class.
"""

def __init__(self, server_context: ServerContext):
self.server_context = server_context

def create(
self,
name: str,
container_path: str = None,
description: str = None,
folder_type: str = None,
is_workbook: bool = None,
title: str = None,
):
return create(
self.server_context, name, container_path, description, folder_type, is_workbook, title
)

def delete(self, container_path: str = None):
return delete(self.server_context, container_path)
Loading