py2http usage examples
#15
Unanswered
thorwhalen
asked this question in
General
Replies: 1 comment
-
Dispatching objects.The webservice: """
Demoing class and instance dispatching
"""
from py2http.service import run_app
class Datasets:
def __init__(self, dataset_name: str):
self.dataset_name = dataset_name
def search(self, query: str):
return f"Searching {self.dataset_name} for {query}"
def store(self, key: str, value: str):
return f"Storing {value} under {key} in {self.dataset_name}"
from functools import lru_cache
# To make Datasets construction cached one can do this:
# TODO: Use flattener to dispatch mk_datasets_instance(...).search(...)
@lru_cache(maxsize=5)
def mk_datasets_instance(dataset_name):
return Datasets(dataset_name)
default_dataset = mk_datasets_instance('default')
from py2http.service import mk_app
app = mk_app(
[
dict(
endpoint=default_dataset,
name='default_dataset',
attr_names=['search'],
),
dict(
endpoint=Datasets,
name='datasets',
attr_names=['search', 'store'],
),
],
publish_openapi=True,
publish_swagger=True,
)
if __name__ == '__main__':
app.run()Run that python file et then, somewhere else, do, for the demo/test: import requests
from functools import partial
def mk_post_request(url, data=None):
if data is None:
return partial(mk_post_request, url)
return requests.post(url, json=data)
datasets = mk_post_request("http://127.0.0.1:8080/datasets")
r = datasets(
{"_obj_id": None, "_attr_name": None, "dataset_name": "apple"}
)
obj_id = r.json()
print(f"{obj_id=}")
r = datasets(
{"_obj_id": obj_id, "_attr_name": "search", "query": "my query"}
)
assert r.json() == 'Searching apple for my query'
r = datasets(
{"_obj_id": obj_id, "_attr_name": "store", "key": "my_key", "value": "my_value"}
)
assert r.json() == 'Storing my_value under my_key in apple' |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Usage examples for
py2http.Beta Was this translation helpful? Give feedback.
All reactions