-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 517-clean-up-requirements
- Loading branch information
Showing
7 changed files
with
161 additions
and
20 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
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 |
---|---|---|
@@ -1,19 +1,37 @@ | ||
"""DLite storage plugin for fetching instances with HTTP GET requests. | ||
This simple plugin was initially added to support the entity service (see | ||
https://github.com/SINTEF/dlite-entities-service), but may have other uses. | ||
""" | ||
import json | ||
import requests | ||
|
||
import dlite | ||
from dlite.options import Options | ||
|
||
|
||
class http(dlite.DLiteStorageBase): | ||
"""DLite storage plugin that fetches entities with http GET.""" | ||
|
||
def open(self, uri, options=None): | ||
"""Opens `uri`.""" | ||
|
||
def load(self, id): | ||
"""Returns instance retrieved from HTTP GET on `id`.""" | ||
r = requests.get(id) | ||
content = json.loads(r.content) | ||
if "detail" in content: | ||
raise dlite.DLiteError(content["detail"]) | ||
return dlite.Instance.from_json(content) | ||
"""DLite storage plugin that fetches instances with HTTP GET.""" | ||
|
||
def open(self, location, options=None): | ||
"""Opens `location`. | ||
Arguments: | ||
location: web address to access | ||
options: Supported options: | ||
- `single`: Whether the input is assumed to be in single- | ||
entity form. The default (`"auto"`) will try to infer | ||
it automatically. | ||
""" | ||
self.options = Options("single=auto") | ||
|
||
r = requests.get(location) | ||
self.content = json.loads(r.content) | ||
if "detail" in self.content: | ||
raise dlite.DLiteStorageOpenError(content["detail"]) | ||
|
||
def load(self, id=None): | ||
"""Returns instance retrieved from HTTP GET.""" | ||
s = self.options.single | ||
single = s if s == "auto" else dlite.asbool(s) | ||
return dlite.Instance.from_dict(self.content, id=id, single=single) |
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ set(python-tests | |
test_minio | ||
test_template | ||
test_template-jinja | ||
test_http | ||
) | ||
|
||
include(FindPythonModule) | ||
|
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,51 @@ | ||
try: | ||
import requests | ||
except ModuleNotFoundError: | ||
print("requests not installed, skipping test") | ||
raise SystemExit(44) # skip test | ||
|
||
import dlite | ||
|
||
|
||
url = "https://raw.githubusercontent.com/SINTEF/dlite/master/storages/python/tests-python/input/test_meta.json" | ||
|
||
meta = dlite.Instance.from_location("http", url) | ||
|
||
assert str(meta) == """ | ||
{ | ||
"uri": "http://onto-ns.com/meta/0.1/TestEntity", | ||
"description": "test entity with explicit meta", | ||
"dimensions": { | ||
"L": "first dim", | ||
"M": "second dim", | ||
"N": "third dim" | ||
}, | ||
"properties": { | ||
"myblob": { | ||
"type": "blob3" | ||
}, | ||
"mydouble": { | ||
"type": "float64", | ||
"unit": "m", | ||
"description": "A double following a single character..." | ||
}, | ||
"myfixstring": { | ||
"type": "string3", | ||
"description": "A fix string." | ||
}, | ||
"mystring": { | ||
"type": "string", | ||
"description": "A string pointer." | ||
}, | ||
"myshort": { | ||
"type": "uint16", | ||
"description": "An unsigned short integer." | ||
}, | ||
"myarray": { | ||
"type": "int32", | ||
"shape": ["L", "M", "N"], | ||
"description": "An array string pointer." | ||
} | ||
} | ||
} | ||
""".strip() |