Skip to content

Commit

Permalink
Merge pull request #137 from JoshYuJump/main
Browse files Browse the repository at this point in the history
Tested bali 3.2.1
  • Loading branch information
JoshYuJump authored Jul 5, 2022
2 parents eaec9e7 + afa434f commit 611c954
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 26 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

## [3.3.0] UNRELEASED

## [3.2.1] UNRELEASED
## [3.2.1] 2022-07-05
### Added
- Event handler support class style
- Added bali.Environments

### Changed
- Removed dependency python-jose[cryptography]==3.2.0
Expand Down
2 changes: 1 addition & 1 deletion bali/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bali.decorators import event_handler, init_handler
from bali.resources import Resource, ModelResource

__version__ = '3.2.2'
__version__ = '3.2.1'


class Schema(BaseModel):
Expand Down
4 changes: 3 additions & 1 deletion bali/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ def serve():

def _launch_event(self):
from .events import handle
from bali import init_handler
from bali import init_handler, __version__

event_handler = self.kwargs.get('event_handler')
if not event_handler:
raise Exception('event_handler not provided')
init_handler(event_handler)
logger.info('Bali v%s Event started.', __version__)
while True:
handle()

Expand Down
11 changes: 10 additions & 1 deletion examples/legacy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Settings(BaseSettings):
SERVER_NAME: str = 'bali-test'
SERVER_NAME: str = 'legacy'

DATABASE_SERVER: str = '127.0.0.1'
DATABASE_USER: str = 'root'
Expand All @@ -25,6 +25,15 @@ def assemble_db_connection(cls, v: Optional[str], values: Dict[str, Any]) -> Any
CACHE_ADDRESS: str = '127.0.0.1'
CACHE_PASSWORD: str = '123456'

AMQP_SERVER_ADDRESS: str = 'amqp://127.0.0.1:5672'

AMQP_CONFIGS = {
'default': {
'AMQP_SERVER_ADDRESS': AMQP_SERVER_ADDRESS,
'EXCHANGE_TYPE': 'fanout',
}
}


settings = Settings()

Expand Down
16 changes: 13 additions & 3 deletions examples/legacy/event_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
from bali.decorators import event_handler


@event_handler('hello')
def hello_handle(event):
pass
class EventHandler:
@event_handler('HelloSaid')
def handle_hello_said(self, event):
self.prepare(event)
print('handle event: ', event)

@event_handler('HiSaid')
def handle_hi_said(self, event):
self.prepare(event)
print('handle event: ', event)

def prepare(self, event):
print('prepare event: ', event)
41 changes: 41 additions & 0 deletions examples/legacy/event_trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os

from bali.core import _settings
from bali.events import Event, dispatch

amqp_uri = os.getenv('AMQP_SERVER_ADDRESS', default='amqp://127.0.0.1:5672')


_settings.AMQP_CONFIGS = {
'default': {
'AMQP_SERVER_ADDRESS': amqp_uri,
'EXCHANGE_NAME': 'ms.events',
'EXCHANGE_TYPE': 'fanout',
}
}


class HelloSaidEvent(Event):
"""Hello said event"""
type: str = 'HelloSaid'
name: str = ''


class HiSaidEvent(Event):
"""Hi said event"""
type: str = 'HiSaid'
name: str = ''


def run():
event = HelloSaidEvent(name='Jack')
dispatch(event)
print('Dispatched event: ', event)

event = HiSaidEvent(name='Jerry')
dispatch(event)
print('Dispatched event: ', event)


if __name__ == '__main__':
run()
9 changes: 3 additions & 6 deletions examples/legacy/main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# noinspection PyUnresolvedReferences
import config
# import grpc_server
import grpc_server_async
from bali.core import Bali
import event_handler
from event_handler import EventHandler
from v1.app import router
from fastapi_pagination import LimitOffsetPage, add_pagination, paginate


app = Bali(
base_settings=None,
Expand All @@ -16,9 +13,9 @@
}],
backend_cors_origins=['http://127.0.0.1'],
rpc_service=grpc_server_async,
event_handler=event_handler
event_handler=EventHandler
)
app.settings(title='Bali Example')
app.settings(title='legacy')

if __name__ == "__main__":
app.start()
2 changes: 1 addition & 1 deletion examples/legacy/resources/async_greeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bali.decorators import action
from bali.resources import Resource
from bali.schemas import GetRequest, ListRequest
from ..permissions import IsAuthenticated
from permissions import IsAuthenticated

GREETERS = [{'id': i, 'content': 'Hi, number %s' % i} for i in range(10)]

Expand Down
2 changes: 1 addition & 1 deletion examples/legacy/resources/greeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bali.decorators import action
from bali.resources import Resource
from bali.schemas import GetRequest, ListRequest
from ..permissions import IsAuthenticated
from permissions import IsAuthenticated

GREETERS = [{'id': i, 'content': 'Hi, number %s' % i} for i in range(10)]

Expand Down
11 changes: 6 additions & 5 deletions examples/legacy/resources/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
from bali.decorators import action
from bali.resources import Resource
from bali.schemas import ListRequest
from ..models import Item
from ..permissions import IsAuthenticated
from ..schemas import ItemModel
from models import Item
from permissions import IsAuthenticated
from schemas import ItemModel

from sqlalchemy.future import select



class QFilter(BaseModel):
name: str

Expand All @@ -38,7 +37,9 @@ def get(self, pk=None):
@action()
def list(self, schema_in: ListRequest = None):
time.sleep(2)
return Item.query().filter(*get_filters_expr(Item, **schema_in.filters))
return Item.query().filter(
*get_filters_expr(Item, **schema_in.filters)
)

@action()
async def list_async(self, schema_in: ListRequest = None):
Expand Down
6 changes: 3 additions & 3 deletions examples/legacy/resources/item_model_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from pydantic import BaseModel

from bali.resources import ModelResource
from ..models import Item
from ..permissions import IsAuthenticated
from ..schemas import ItemModel
from models import Item
from permissions import IsAuthenticated
from schemas import ItemModel


class QFilter(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions examples/legacy/v1/app.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from fastapi_pagination import LimitOffsetPage, paginate

from bali.core import APIRouter, cache_memoize
from ..models import Item
from ..resources import (
from models import Item
from resources import (
GreeterResource,
AsyncGreeterResource,
ItemResource,
ItemModelResource,
)
from ..schemas import ItemModel
from schemas import ItemModel

router = APIRouter()

Expand Down

0 comments on commit 611c954

Please sign in to comment.