Skip to content
Discussion options

You must be logged in to vote

I came up with this, but i dont know if it's the correct way of doing it:

from mycompany import MailClient

def _client():
    with MailClient({'username': 'john', 'password': 'secretsauce'}) as dep:
        yield dep


class Mail:
    def __init__(self, bt, client):
        self.background_tasks = bt
        self.client= client

    @staticmethod
    def inject(background_tasks: BackgroundTasks, client=Depends(_client)):
        return Mail(background_tasks, client)

    def send(self, *args, **kwargs):
        self.background_tasks.add_task(self.client.send, *args, **kwargs)

Now i can just

from tasks import Mailer

app = FastAPI()

@app.get("/send")
async def send_mail(mail: Mail = Depends

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@ghandic
Comment options

@thurse93
Comment options

Answer selected by thurse93
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants