Ashwani
Ashwani

Reputation: 1480

Calling internal api from another api function

I have implemented two endpoints:

Post -  /users/  #endpoint to add a user
Post - /confirmemail/  #endpoint to confirm email

Now I have function implemented for both endpoints, But I am thinking of calling the email endpoint after adding the user, directly. How can I achieve this in Fastapi?

Upvotes: 3

Views: 3724

Answers (1)

Grey Li
Grey Li

Reputation: 12762

If one of your functionality will be used by multiple endpoints, you may need to extract it into a separate function (decoupling), for example:

def send_confirm_email():
    pass

Then call it in different endpoints:

from .utils import send_confirm_email

@app.post("/users")
def add_user():
    # ...
    send_confirm_email()
    return {"message": "User added, confirm email sent."}

@app.post("/confirmemail")
def confirm_email():
    send_confirm_email()
    return {"message": "confirm email sent."}

Upvotes: 3

Related Questions