jsonQM
A simple tool to easily make your API endpoint json queries more versatile.
pip install jsonQM
Quick Start
The example model explains how to set up a model and query that model.
tests/ExampleModel.py
from jsonQM import *
# Make the model.
# Declare a model serializer
ms = ModelSerializer() # Each model must have its own serializer
class MyModel(Model):
def __init__(self) -> None:
# define the structure of the query model:
self.model = {
# we have a single query scope/section called functions
"functions":{},
"msg":"test" # non functional attributes also need to be included in the json query if you wish for them to be present in the response (see Example query 3 below)
}
# sync/add the model attribute functions to the model
ms.sync(self)
# Define attributes of the model (argument 2) along with the scope/section they are found in (argument 1)
@ms.add(["functions"], "repeater") # This attribute key is "repeater" and can be found in the "functions" dict
def repeater(self, arg:int):
# when the attribute is queried it will run this code and return the value
return ["repeat" for _ in range(arg)]
# You can use anything as the attribute key that is a valid python dictionary key.
@ms.add(["functions"], 7)# Keep in mind that if used with json, you are limited to what is a valid json key.
def number_7(self):
return "abc"
@ms.token()# this marks this attribute as the model's token
@ms.add([], "token")
def token(self, tok:str):
#insert some logic to compare token to tokens in database
return tok == "token" # The token function must return true for the token to be valid.
@ms.requires_token()# this marks this attribute as requiring the token attribute to return true
@ms.add([], "secret")
def secret(self):
return "My super secret message" # if the token is false this will return 'errot:token:invalid' if the token is missing from the query this will return 'error:token:missing'
#LOGIC
if __name__ == "__main__":
# If you are experiencing bugs related to concurrency, you should reinstantiate your model per thread/concurrent task where it is being used.
# note that instantiation may require many itterations depending on the ammount of attributes you've added and the length of their parent path.
# (parent path is the first list argument in `ms.add(["some","dictionary","key","path"], "attribute name")`)
model_instance = MyModel()
# If you can avoid instantiating the model more than once for optimal performance.
#Example query 1
print(model_instance.get({
"functions":{
# programmed attribute values should be a list containing the function arguments.
"repeater":[5],
7:[]
}
}))
# prints:
# {'functions': {'repeater': ['repeat', 'repeat', 'repeat', 'repeat', 'repeat'], 7: 'abc'}}
#Example query 2
print(model_instance.get({
"functions":{
# The model will only run/return attributes which have been specified
"repeater":[5]
}
}))
# prints:
# {'functions': {'repeater': ['repeat', 'repeat', 'repeat', 'repeat', 'repeat']}}
#Example query 3
print(model_instance.get({
"token":["token"],
"secret":[]
}))
# prints:
# {'token': True, 'secret': 'My super secret message'}
#Example query 3
print(model_instance.get({
"token":["abc"],
"secret":[]
}))
# prints:
# {'token': False, 'secret': 'error:token:invalid'}
#Example query 3
print(model_instance.get({
"secret":[]
}))
# prints:
# {'secret': 'error:token:missing'}
#Example query 3
print(model_instance.get({
"msg":1
}))
# prints:
# {'msg': 'test'}
Metadata
Release files for jsonQM 0.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsonQM-0.3.4.tar.gz | 5.2 kB | Without provenance | Details for jsonQM-0.3.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jsonQM-0.3.4-py3-none-any.whl | Python 3 | none | any | Details for jsonQM-0.3.4-py3-none-any.whl |
Total release size: 10.4 kB
Release files / jsonQM-0.3.4.tar.gz
| Download URL | jsonQM-0.3.4.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
48b4703bc85059a20bf3edda4ef966e53f605ec61fd92435cbca36c0820c3d57
|
|
BLAKE2b-256 checksum How to use checksums |
2f95e6b256be876a9ad380711221a8381593a65d74424789d59dc943be82b79c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.2
|
Release files / jsonQM-0.3.4-py3-none-any.whl
| Download URL | jsonQM-0.3.4-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
53bff3f25bb2b306d9f31c86c04920570b1fcd5e33109d1d5b61ea6545940ddb
|
|
BLAKE2b-256 checksum How to use checksums |
853e3e6ff571efa0edb1028c7c7714f5cd48c8ef36551e33bc2ab1a93687704d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.2
|
Security for jsonQM 0.3.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.