Coder Social home page Coder Social logo

fauna / fauna-python Goto Github PK

View Code? Open in Web Editor NEW
6.0 9.0 3.0 1.04 MB

Python driver for Fauna v10

Home Page: https://fauna.com

License: Other

Makefile 0.36% Python 97.96% Shell 1.58% Dockerfile 0.10%
database fauna faunadb python serverless client clients driver drivers nosql

fauna-python's People

Contributors

adambollen avatar clbray avatar cleve-fauna avatar cynicaljoy avatar erickpintor avatar fauna-chase avatar freels avatar jrodewig avatar marrony avatar pnwpedro avatar ptpaterson avatar sprsquish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fauna-python's Issues

Unable to easily re-ecode fauna query results to JSON due to special objects being returned

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:

  • provide a flag on the query level or client level or both to return raw, JSON serializable dictionaries instead of any of the special object types.
  • provide a subclass of json.JSONEncoder to use with 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.
  • provide a way on each model type, such as Document, Module, etc to get a JSON serializable dictionary

"Circular reference detected" for non-circular references

The 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)

HTTPX read timeout is set to infinite by default

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

Client `query_timeout` leads to 400 invalid request error

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

Default client get secret from env vars limits compatibility with cloud runtimes

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.