-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `hostname` property to ServerContext * Add `base_url` property to ServerContext * Add `webdav_client` method to ServerContext * This method returns a webdavclient3 Client instance * Add `webdav_path` method to ServerContext * Add docs for WebDav support * Add unit tests for ServerContext
- Loading branch information
1 parent
0000ee6
commit 7644bd6
Showing
6 changed files
with
181 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# WebDav Support | ||
|
||
Our Python API includes some convenience methods for creating "webdavclient3" clients, and building webdav file paths. | ||
|
||
### Creating a WebDav client | ||
First, make sure you have the [webdavclient3](https://github.com/ezhov-evgeny/webdav-client-python-3) library installed: | ||
|
||
```bash | ||
$ pip install webdavclient3 | ||
``` | ||
|
||
Then you can use your `APIWrapper` to create a client: | ||
|
||
```python | ||
from labkey.api_wrapper import APIWrapper | ||
|
||
domain = "localhost:8080" | ||
container = "MyContainer" | ||
api = APIWrapper(domain, container) | ||
webdav_client = api.server_context.webdav_client() | ||
``` | ||
|
||
The `webdav_client` method has a single optional argument, `webdav_options`, a dict that you can use to pass any options | ||
that you would pass to the [webdavclient3](https://github.com/ezhov-evgeny/webdav-client-python-3#webdav-api) library. | ||
If you are using API Key authentication with your APIWrapper we will automatically configure the WebDav Client to use | ||
API Key authentication with your API Key. If you are using a `.netrc` file for authentication it should automatically | ||
detect your `.netrc` file and authenticate using those credentials. | ||
|
||
|
||
### The webdav_path utility method | ||
If you are using the `webdavclient3` library you'll still need to know the appropriate WebDav path in order to access | ||
your files. We provide a utility method, `webdav_path` to make it easier to construct LabKey WebDav paths. The method | ||
takes two keyword arguments, `container_path`, and `file_name`. | ||
|
||
```python | ||
from labkey.api_wrapper import APIWrapper | ||
|
||
domain = "localhost:8080" | ||
container = "MyContainer" | ||
api = APIWrapper(domain, container) | ||
webdav_client = api.server_context.webdav_client() | ||
|
||
# Constructs a webdav path to "MyContainer" | ||
path = api.server_context.webdav_path() | ||
print(webdav_client.info(path)) | ||
# Constructs a webdav path to the "data.txt" file in "MyContainer" | ||
path = api.server_context.webdav_path(file_name='data.txt') | ||
print(webdav_client.info(path)) | ||
# Constructs a webdav path to the "data.txt" file in "other_container" | ||
path = api.server_context.webdav_path(container_path="other_container", file_name="data.txt") | ||
print(webdav_client.info(path)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from labkey.server_context import ServerContext | ||
import pytest | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def server_context(): | ||
return ServerContext("example.com", "test_container", "test_context_path") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def server_context_no_context_path(): | ||
return ServerContext("example.com", "test_container") | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def server_context_no_ssl(): | ||
return ServerContext("example.com", "test_container", "test_context_path", use_ssl=False) | ||
|
||
|
||
def test_base_url(server_context, server_context_no_context_path, server_context_no_ssl): | ||
assert server_context.base_url == "https://example.com/test_context_path" | ||
assert server_context_no_context_path.base_url == "https://example.com" | ||
assert server_context_no_ssl.base_url == "http://example.com/test_context_path" | ||
|
||
|
||
def test_build_url(server_context): | ||
assert ( | ||
server_context.build_url("query", "getQuery.api") | ||
== "https://example.com/test_context_path/test_container/query-getQuery.api" | ||
) | ||
assert ( | ||
server_context.build_url("query", "getQuery.api", "different_container") | ||
== "https://example.com/test_context_path/different_container/query-getQuery.api" | ||
) | ||
|
||
|
||
def test_webdav_path(server_context, server_context_no_context_path, server_context_no_ssl): | ||
assert server_context.webdav_path() == "/_webdav/test_container/@files" | ||
assert ( | ||
server_context.webdav_path(file_name="test.jpg") | ||
== "/_webdav/test_container/@files/test.jpg" | ||
) | ||
assert ( | ||
server_context.webdav_path("my_container/with_subfolder", "data.txt") | ||
== "/_webdav/my_container/with_subfolder/@files/data.txt" | ||
) |