Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to have a function with json return type or no return type at all act as a route? #3

Closed
csajedi opened this issue Mar 12, 2020 · 2 comments

Comments

@csajedi
Copy link

csajedi commented Mar 12, 2020

So I want to use you jsonrpc implementation for its numerous benefits, but when I try to combine it with some functions that return JSON strings I cannot seem to get the response configuration right.
When I run it with json return type I get an error, when I try to use a class implementing BaseModel and containing the json element, then it runs the server but responses error the server out.
If you want to see the code, its basically:

from pydantic import json
@api_v1.method()
def consistency(past_root:str) -> json:

which I would be fine removing the json return type, but the value it responds with to the client should absolutely be this json string. I feel that I am missing something, being a beginner at fastapi and your library as well. The code in question can be found here.

@denisSurkov
Copy link
Contributor

Check how fast API solves this problem. If you want to annotate you return's type you should use def foo() -> dict: (dict = json). So just return a dict from your view and the lib will do its job.

Hint:
It's better to return plain dict from your service function proof_from(), and then return dict from api view.

...
        # proof_from()
        proof = self.merkle.merkleProof({'checksum': digest}).serialize()
        print(proof)
        return proof
...

The result would like:

{
  "jsonrpc": "2.0",
  "id": 0,
   "result": { _your object from proof_from()_ }
}

@csajedi
Copy link
Author

csajedi commented Mar 26, 2020

Thank you, this solved my issue. great library!

@csajedi csajedi closed this as completed Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants