GithubHelp home page GithubHelp logo

Comments (11)

Lancetnik avatar Lancetnik commented on June 12, 2024

Hi, @jonatasoli! There is no any error: you are trying to send the message to nowhere: .publish(message, queue) requires a message body with the first argument.
So, in your case body is RabbitQueue('test', arguments={'m': username}) and you send the message with "" routing key to default exchange.

If you try to send message like

publish(queue=RabbitQueue('test', arguments={'m': username})

You will get a message serialization error.

If you will try to send correct message

publish({"username": "john"}, queue=RabbitQueue('test', arguments={'m': username})

You will get the Exception('test')

So, all exceptions are printing correctly

from propan.

jonatasoli avatar jonatasoli commented on June 12, 2024

Found With set only queue

import logging
import sys
from propan.brokers.rabbit import RabbitQueue
from propan.fastapi import RabbitRouter
from pydantic import BaseModel

import sentry_sdk
from dynaconf import settings
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger

from endpoints.v1.users import user


task_cart_router = RabbitRouter(settings.BROKER_URL)

class Incoming(BaseModel):
    username: str

def call():
    return True

@task_cart_router.event(RabbitQueue("test"))
async def hello(m: Incoming, d = Depends(call)):
    logger.info(f" log test {m.username}")
    return m.username

    return { "response": f"Hello, {m.username}!" }



app = FastAPI(lifespan=task_cart_router.lifespan_context)

app.include_router(task_cart_router)

origins = [
    '*',
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

@user.post('/test/{username}')
async def test(username: str):
    task_response = await task_cart_router.broker.publish({"username": username}, queue=RabbitQueue('test'))

    return { "response": f"Hiiii, {username}! {str(task_response.body)}" }

app.include_router(user)
app.include_router(task_cart_router)

If you want I create a simple example with this example

https://github.com/Lancetnik/Propan/tree/main/examples/http_frameworks_integrations

from propan.

Lancetnik avatar Lancetnik commented on June 12, 2024

I can't understand your problem to be honest. You will have no any messages until you doesn't trigger your POST hook. So, what you expect in your console at startup if there no any messages yet?
When messages published, all works correct, I checked it with your code example.

from propan.

Lancetnik avatar Lancetnik commented on June 12, 2024

If you want to publish smth at startup, use router.after_startup hook

from propan.

jonatasoli avatar jonatasoli commented on June 12, 2024

My example I try exec the event via swagger this example but, after the original code the message send to rabbitmq but, don't receive the message in event or dont show the logs.

from propan.

Lancetnik avatar Lancetnik commented on June 12, 2024

@jonatasoli the following example works fine for me

from propan.brokers.rabbit import RabbitQueue
from propan.fastapi import RabbitRouter
from pydantic import BaseModel

from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger


task_cart_router = RabbitRouter()

class Incoming(BaseModel):
    username: str

def call():
    return True

@task_cart_router.event(RabbitQueue("test"))
async def hello(m: Incoming, d = Depends(call)):
    logger.info(f" log test {m.username}")
    return m.username

app = FastAPI(lifespan=task_cart_router.lifespan_context)

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

@app.post('/test/{username}')
async def test(username: str):
    task_response = await task_cart_router.broker.publish({"username": username}, queue=RabbitQueue('test'))

    return { "response": f"Hiiii, {username}! {str(task_response)}" }

app.include_router(task_cart_router)

from propan.

Lancetnik avatar Lancetnik commented on June 12, 2024

But if you want to catch the event return, you need to use callback=True flag

from propan.

jonatasoli avatar jonatasoli commented on June 12, 2024

Yep this mode is found to me too, I think is great up this example with sending and receiving the fastapi example in repository is:

from contextlib import asynccontextmanager

from fastapi import FastAPI
from propan import RabbitBroker

broker = RabbitBroker("amqp://guest:guest@localhost:5672/")

app = FastAPI()


@asynccontextmanager
async def lifespan(app: FastAPI):
    await broker.start()
    yield
    await broker.close()


@broker.handle("test")
async def base_handler(body):
    print(body)


@app.get("/")
def read_root():
    return {"Hello": "World"}

from propan.

jonatasoli avatar jonatasoli commented on June 12, 2024

I resolve my problem so I think close this issue, but I think a good ideia add a simple mode this example to examples galery, I send PR if you think is a good ideia also.

from propan.

Lancetnik avatar Lancetnik commented on June 12, 2024

@jonatasoli
There are two FastAPI examples already:

  1. raw: https://github.com/Lancetnik/Propan/blob/main/examples/http_frameworks_integrations/fastapi.py
  2. router: https://github.com/Lancetnik/Propan/blob/main/examples/http_frameworks_integrations/native_fastapi.py

Is it the same thing you want to add?

from propan.

jonatasoli avatar jonatasoli commented on June 12, 2024

Yes I think add one more with this example.

from propan.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.