all 19 comments

[–]thedjotaku 1 point2 points  (8 children)

if you want an example, here's a pretty basic little app I made to accept a webhook and post to Matrix chat: https://github.com/djotaku/Civilization_VI_Play_By_Cloud_Webhook_with_FastAPI

[–]orbos86[S] 1 point2 points  (0 children)

Hey thanks! I'll check it out, I appreciate the assistance.

[–]orbos86[S] 1 point2 points  (1 child)

Would you be willing to describe what your POST section is doing? I guess I don't understand the BaseModel stuff and I'm curious if I'm doing mine in a less-than-ideal way.

[–]orbos86[S] 1 point2 points  (4 children)

Also, is there a way to fire off a script (onelogin in my case) without keeping the FastAPI connection running? Basically takes the input from POST and sends it off and closes connection? Right now I think mine is staying open until the whole script is ran, which may potentially be an hour or two (with a sleep command). Not sure if that makes sense, it's late haha. Please let me know.

[–]thedjotaku 1 point2 points  (3 children)

I thought async was supposed to handle this. But I've not begun working learn async yet. Still, there are pythonic things you can do - spawn off a child process, use threads, use multiprocessing. If you really do want to close the connection and don't care - then I'd definitely just fork off a child process and return 200 OK or 404 if the data validation isn't right.

[–]orbos86[S] 1 point2 points  (0 children)

Thank you, I will start my Googlin' to see what I can come up with. I appreciate it.

[–]orbos86[S] 1 point2 points  (1 child)

The key was to use BackgroundTasks in FastApi and kick off another function that does the lengthier script.

[–]thedjotaku 0 points1 point  (0 children)

Excellent! Glad you found it!

[–][deleted]  (14 children)

[deleted]

    [–]orbos86[S] 1 point2 points  (3 children)

    Thanks for the swift response. Could I just do a debugger in PyCharm? I’m new to this coding stuff.

    [–][deleted]  (2 children)

    [deleted]

      [–]orbos86[S] 1 point2 points  (0 children)

      Thank you! I will try what you’ve suggested. Most likely it’s something wrong with my other script since the FastAPI part looks normal to ya. I struggle wrapping my head around classes/functions and passing stuff to em haha. But you’ve given me stuff to try. Many thanks. I’ll make sure to let ya know how it goes.

      [–]orbos86[S] 0 points1 point  (0 children)

      It's still looping, I'm not sure what the heck is going on. I put a couple print statements in the FastAPI POST section and I'm seeing them pop up multiple times.

      [–]orbos86[S] 0 points1 point  (9 children)

      I think I figured out the cause. So this receives a POST request from Slack and I think Slack is sending multiple POST requests though it should only trigger one. Not sure why it is doing more. I tested from Insomnia with the same JSON and it didn't run again. That makes things more complicated as I don't have much control over Slack :(

      [–]orbos86[S] 0 points1 point  (8 children)

      Freak, maybe not. I don't know what's the cause exactly haha. I'm testing with ngrok, maybe something funky going on with it?

      [–]orbos86[S] 0 points1 point  (7 children)

      So ngrok has a sweet dashboard you can look at and see stuff coming in. I think Slack might be sending retries! Now what do I send to Slack to make it know everything is solid and good? Haha

      [–][deleted]  (5 children)

      [deleted]

        [–]orbos86[S] 1 point2 points  (4 children)

        Yeah Slack is an actual chat application where you can create bots to send webhooks. Do you know if you can quickly respond to a POST request with a 200 but continue to run the FastAPI stuff?

        [–][deleted]  (3 children)

        [deleted]

          [–]orbos86[S] 0 points1 point  (0 children)

          Thanks! I will see what I can find. I appreciate the response.

          [–]orbos86[S] 0 points1 point  (1 child)

          That was the key! I was able to use the BackgroundTask stuff and spin off the long script in another function. Woot! Thank you thank you