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

Example: native api client (without generating code) - DO NOT MERGE #11342

Closed
wants to merge 5 commits into from

Conversation

LamaAni
Copy link

@LamaAni LamaAni commented Mar 25, 2024

The following PR is an example for the issue: #8242 and #11343

In short, an FastApiClient object was added that allows the auto capture of calls to the decorated method
in which the call is either directed to the function itself or is redirected to the an api client, depending on the context.

E.g. we can create an api client in the following manner:

from fastapi import FastAPI, FastAPIClient
from fastapi.routing import APIRoute


class MyApiClient(FastAPIClient):
    # Client paramters to be added here.
    # But this dose nothing, We could implement a default api client.
    def __init__(self, host: str = "localhost:8080") -> None:
        super().__init__()

    def send(self, route: APIRoute, args: list, kwargs: dict):
        # Send the request given the route.
        # Dummy just returns the route and input values
        return route, args, kwargs

    async def send_async(self, route: APIRoute, args: list, kwargs: dict):
        # Send the aysnc request given the route.
        # Dummy just returns the route and input values
        return route, args, kwargs


api = FastAPI()


@api.get("/my_func")
def my_func(a, b):
    return a + b


client = MyApiClient()
# a call will be made to my_func.
print(my_func(2, 3))
with client:
    # A call will be made to the server, returning
    # the proper response.
    print(my_func(2, 3))
# a call will be made to my_func.
print(my_func(2, 3))

@LamaAni LamaAni changed the title Example: automated client generation for default client calls (without generating code) - DO NOT MERGE Example: native api client (without generating code) - DO NOT MERGE Mar 25, 2024
@tiangolo
Copy link
Owner

tiangolo commented Apr 2, 2024

Thanks for the interest! 🤓

This would belong in an independent package, not in FastAPI itself. It would probably use the OpenAPI generated by FastAPI, it would probably use JSON Schema to create all the models, etc.

As this would be out of scope for FastAPI, I'll go ahead and close this one, but thanks for the interest! ☕

@tiangolo tiangolo closed this Apr 2, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants