fauna / fauna-python Goto Github PK
View Code? Open in Web Editor NEWPython driver for Fauna v10
Home Page: https://fauna.com
License: Other
Python driver for Fauna v10
Home Page: https://fauna.com
License: Other
In certain instances, the FQLv10 Python driver will de-serialize a result into specific models it defines, such as Document
or Module
. This makes it difficult to easily serialize the result to JSON for storage or transport to other locations outside of fauna.
What I was hoping to be able to accomplish would be something like the following for a simple illustrative example:
result = fauna_client.query(
fql(
"""
Key.create({
role: ${role},
data: {name: ${name}, token: ${token}},
})
""",
role=role,
name=name,
token=token,
)
).data
service_client = boto3.client("secretsmanager")
service_client.put_secret_value(
SecretId=arn,
ClientRequestToken=token,
SecretString=json.dumps(result),
VersionStages=["AWSPENDING"],
)
This fails because the Document
type is not inherently JSON serializable. In digging through the repo, I find that there are encoders and decoders, but they don't seem to fully serialize or de-serialize the document into a plain dictionary like I would hope. I'm looking for a result like the following after calling json.dumps
:
{
"id": "380934557415768132",
"coll": "key",
"ts": "2023-11-09T16:12:33.665000+00:00",
"secret": "asdf",
"data": {
"name": "test",
"token": "5918c24f-0449-4577-bbae-9f675fbaae7d"
}
}
I think it would be a nice feature for this library to assist with being able to easily de-serialize an object to JSON by providing any of the following:
json.dump
, json.dumps
, json.load
, and json.loads
that knows how to serialize and de-serialize the special objects that the Python driver may return.Document
, Module
, etc to get a JSON serializable dictionaryThe driver is too aggressive at detecting circular references during encoding. If the same reference appears multiple times in an object, regardless of circular references, it fails with Failed to encode Query
, with the cause Circular reference detected
.
That is, you can do this
emptyList = []
myDict = { "value": "test", "list": myList }
documents = [myDict]
query = fql("${x}", x=documents)
But you cannot do
myList = []
myDict = { "value": "test", "list1": emptyList, "list2": emptyList }
documents = [myDict]
query = fql("${x}", x=documents)
nor
myDict = { "value": "test" }
documents = [myDict, myDict]
query = fql("${x}", x=documents)
I traced a hung client down to this following line where it appears if no timeouts are passed, the HTTP read timeout is set to None
. This, as far as I can tell from the HTTPX docs, means it will never time out.
Line: https://github.com/fauna/fauna-python/blob/main/fauna/client/client.py#LL123C18-L123C18
HTTPX timeout docs: https://www.python-httpx.org/advanced/#timeout-configuration
FQLv10 code that produced the hung client: https://forums.fauna.com/t/fqlv10-comparison-of-objects-never-completes/3652
After upgrading to fauna-python v0.7.0, I am now getting the following error when setting a timeout in the client and making requests:
E fauna.errors.errors.InvalidRequestError: 400: invalid_request
E Invalid header 'x-query-timeout-ms': Invalid number '5000.0'.
The client instantiation code is:
def create_fauna_client(secret):
"""Create the FQLv10 client and return it.
This should be called handler, or subsequently invoked functions within the handler,
so when the lambda context is frozen, the socket connections are not lost.
See: https://docs.fauna.com/fauna/current/drivers/known_issues
"""
logger.debug("Initializing FaunaDB client.")
# This timeout is currently set to half of the configured lambda timeout value. If
# the terraform code is updated, this should be revisited.
client = Client(secret=secret, query_timeout=timedelta(seconds=5))
logger.debug("FaunaDB client initialized.")
return client
Currently, one is able to install this package in an environment using Python 3.8 or older as the package setup script does not specify the python range it supports. This python version metadata should be added to the setup script.
Hi there :)
I'm trying to migrate from the faunadb to the fauna Python client. My specific use case involves running an automated process on a platform called Pipedream. I can't initiate the fauna Client, seemingly due to an issue related to the given runtime not supporting the attempt to retrieve the "FAUNA_SECRET" environment variable. To help set the context, here's the Python code I'm running in Pipedream:
from fauna.client import Client
db_client = Client(secret="abc") # actual key redacted
This is leading to the error message:
Traceback (most recent call last):
File "/nano-py/pipedream/worker.py", line 137, in execute
user_retval = handler(pd)
^^^^^^^^^^^
File "/tmp/__pdg__/dist/code/f3f82eb5747d9e59b7038eb7fc82c2dc2557fdccd09bbf910b13851e405e5b35/code.py", line 26, in handler
db_client = Client(secret=db_secret)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/__pdg__/dist/python/fauna/client/client.py", line 94, in __init__
self._set_endpoint(endpoint)
File "/tmp/__pdg__/dist/python/fauna/client/client.py", line 574, in _set_endpoint
endpoint = _Environment.EnvFaunaEndpoint()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/__pdg__/dist/python/fauna/client/utils.py", line 63, in __call__
os.environ.get(
TypeError: dict.get() takes no keyword arguments
The same code works for me locally (same versions for Python and fauna), so it seems like a Pipedream runtime problem. It'd be great if we could control this default "FAUNA_SECRET" environment variable behaviour to extend the compatibility of fauna across runtime contexts. For example, something like:
client = Client(secret=db_secret, default_key_location=False)
Please let me know if you have any follow up questions.
Thanks very much,
Zach
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.