-
Notifications
You must be signed in to change notification settings - Fork 4
DEVEXP-494 Can now connect to MarkLogic Cloud #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
| from urllib.parse import urljoin | ||
|
|
||
| import requests | ||
| from requests.auth import AuthBase | ||
|
|
||
| # See https://requests.readthedocs.io/en/latest/user/advanced/#custom-authentication | ||
|
|
||
|
|
||
| class MarkLogicCloudAuth(AuthBase): | ||
| def __init__(self, base_url: str, api_key: str, verify): | ||
| self._base_url = base_url | ||
| self._verify = verify | ||
| self._generate_token(api_key) | ||
|
|
||
| def _generate_token(self, api_key: str): | ||
| response = requests.post( | ||
| urljoin(self._base_url, "/token"), | ||
| data={"grant_type": "apikey", "key": api_key}, | ||
| verify=self._verify, | ||
| ) | ||
|
|
||
| if response.status_code != 200: | ||
| message = f"Unable to generate token; status code: {response.status_code}" | ||
| message = f"{message}; cause: {response.text}" | ||
| raise ValueError(message) | ||
|
|
||
| self._access_token = response.json()["access_token"] | ||
|
|
||
| def __call__(self, r): | ||
| r.headers["Authorization"] = f"Bearer {self._access_token}" | ||
| return r | ||
This file contains hidden or 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 hidden or 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,85 @@ | ||
| import pytest | ||
|
|
||
| from marklogic.client import Client | ||
|
|
||
| """ | ||
| This module is intended for manual testing where the cloud_config fixture | ||
| in conftest.py is modified to have a real API key and not "changeme" as a value. | ||
| """ | ||
|
|
||
| DEFAULT_BASE_PATH = "/ml/test/marklogic/manage" | ||
|
|
||
|
|
||
| def test_base_path_doesnt_end_with_slash(cloud_config): | ||
| if cloud_config["key"] == "changeme": | ||
| return | ||
|
|
||
| client = _new_client(cloud_config, DEFAULT_BASE_PATH) | ||
| _verify_client_works(client) | ||
|
|
||
|
|
||
| def test_base_path_ends_with_slash(cloud_config): | ||
| if cloud_config["key"] == "changeme": | ||
| return | ||
|
|
||
| client = _new_client(cloud_config, DEFAULT_BASE_PATH + "/") | ||
| _verify_client_works(client) | ||
|
|
||
|
|
||
| def test_base_url_used_instead_of_host(cloud_config): | ||
| if cloud_config["key"] == "changeme": | ||
| return | ||
|
|
||
| base_url = f"https://{cloud_config['host']}" | ||
| client = Client( | ||
| base_url, cloud_api_key=cloud_config["key"], base_path=DEFAULT_BASE_PATH | ||
| ) | ||
| _verify_client_works(client) | ||
|
|
||
|
|
||
| def test_invalid_host(): | ||
| with pytest.raises(ValueError) as err: | ||
| Client( | ||
| host="marklogic.com", | ||
| cloud_api_key="doesnt-matter-for-this-test", | ||
| base_path=DEFAULT_BASE_PATH, | ||
| ) | ||
| assert str(err.value).startswith( | ||
| "Unable to generate token; status code: 403; cause: " | ||
| ) | ||
|
|
||
|
|
||
| def test_invalid_api_key(cloud_config): | ||
| if cloud_config["key"] == "changeme": | ||
| return | ||
|
|
||
| with pytest.raises(ValueError) as err: | ||
| Client( | ||
| host=cloud_config["host"], | ||
| cloud_api_key="invalid-api-key", | ||
| base_path=DEFAULT_BASE_PATH, | ||
| ) | ||
| assert ( | ||
| 'Unable to generate token; status code: 401; cause: {"statusCode":401,"errorMessage":"API Key is not valid."}' | ||
| == str(err.value) | ||
| ) | ||
|
|
||
|
|
||
| def _new_client(cloud_config, base_path: str) -> Client: | ||
| return Client( | ||
| host=cloud_config["host"], | ||
| cloud_api_key=cloud_config["key"], | ||
| base_path=base_path, | ||
| ) | ||
|
|
||
|
|
||
| def _verify_client_works(client): | ||
| # Verify that the request works regardless of whether the path starts with a slash | ||
| # or not. | ||
| _verify_search_response(client.get("v1/search?format=json")) | ||
| _verify_search_response(client.get("/v1/search?format=json")) | ||
|
|
||
|
|
||
| def _verify_search_response(response): | ||
| assert 200 == response.status_code | ||
| assert 1 == response.json()["start"] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need any error handling here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I forget if a 4xx gets thrown right away - I'll add a test case for it.