3

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?

1
  • 2
    Why do you need to call an API if you're running in the same program? Won't a function call do get the job done? Commented Sep 15, 2020 at 23:00

1 Answer 1

3

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."}
Sign up to request clarification or add additional context in comments.

1 Comment

I have added both the endpoints in the different routers, So I was thinking of making this call generic

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.