Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cake-bot
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Oleksandr Barabash
cake-bot
Commits
0c60fbfc
Commit
0c60fbfc
authored
Oct 28, 2022
by
Oleksandr Barabash
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
database init added on startup. Small fixes, app_factory added
parent
b369a553
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
60 additions
and
19 deletions
+60
-19
app.py
app.py
+44
-13
config.py
config.py
+0
-1
cosmos_client.py
utils/cosmos_client.py
+16
-5
No files found.
app.py
View file @
0c60fbfc
...
...
@@ -20,7 +20,7 @@ from marshmallow import EXCLUDE, ValidationError
from
bots
import
TeamsMessagingExtensionsActionPreviewBot
from
bots.exceptions
import
ConversationNotFound
,
DataParsingError
from
config
import
AppConfig
,
COSMOS_CLIENT
,
TeamsAppConfig
,
TOKEN_HELPER
,
\
LOG_FILE
CosmosDBConfig
from
entities.json.admin_user
import
AdminUser
from
entities.json.notification
import
Notification
from
utils.cosmos_client
import
ItemNotFound
...
...
@@ -212,24 +212,55 @@ async def v1_auth(request: Request) -> Response:
return
Response
(
status
=
HTTPStatus
.
FORBIDDEN
)
APP
=
web
.
Application
(
middlewares
=
[
error_middleware
])
APP
.
router
.
add_post
(
"/api/v1/messages"
,
v1_messages
)
APP
.
router
.
add_post
(
"/api/v1/notification"
,
v1_notification
)
APP
.
router
.
add_get
(
"/api/v1/notification/{notification_id}"
,
v1_get_notification
)
APP
.
router
.
add_get
(
"/api/v1/initiations/{notification_id}"
,
v1_get_initiations
)
APP
.
router
.
add_get
(
"/api/v1/health-check"
,
v1_health_check
)
APP
.
router
.
add_get
(
"/{}"
.
format
(
TeamsAppConfig
.
zip_name
),
get_app_zip
)
APP
.
router
.
add_post
(
"/api/v1/auth"
,
v1_auth
)
async
def
init_db_containers
():
""" To speed up the process we have to create containers first """
COSMOS_CLIENT
.
create_container
(
CosmosDBConfig
.
Conversations
.
DATABASE
,
CosmosDBConfig
.
Conversations
.
CONTAINER
,
CosmosDBConfig
.
Conversations
.
PARTITION_KEY
)
COSMOS_CLIENT
.
create_container
(
CosmosDBConfig
.
Notifications
.
DATABASE
,
CosmosDBConfig
.
Notifications
.
CONTAINER
,
CosmosDBConfig
.
Notifications
.
PARTITION_KEY
)
COSMOS_CLIENT
.
create_container
(
CosmosDBConfig
.
Acknowledges
.
DATABASE
,
CosmosDBConfig
.
Acknowledges
.
CONTAINER
,
CosmosDBConfig
.
Acknowledges
.
PARTITION_KEY
)
COSMOS_CLIENT
.
create_container
(
CosmosDBConfig
.
Initiations
.
DATABASE
,
CosmosDBConfig
.
Initiations
.
CONTAINER
,
CosmosDBConfig
.
Initiations
.
PARTITION_KEY
)
async
def
app_factory
(
bot
):
""" Create the app """
await
init_db_containers
()
app
=
web
.
Application
(
middlewares
=
[
error_middleware
])
app
.
router
.
add_post
(
"/api/v1/messages"
,
v1_messages
)
app
.
router
.
add_post
(
"/api/v1/notification"
,
v1_notification
)
app
.
router
.
add_get
(
"/api/v1/notification/{notification_id}"
,
v1_get_notification
)
app
.
router
.
add_get
(
"/api/v1/initiations/{notification_id}"
,
v1_get_initiations
)
app
.
router
.
add_get
(
"/api/v1/health-check"
,
v1_health_check
)
app
.
router
.
add_get
(
"/{}"
.
format
(
TeamsAppConfig
.
zip_name
),
get_app_zip
)
app
.
router
.
add_post
(
"/api/v1/auth"
,
v1_auth
)
bot
.
add_web_app
(
app
)
bot
.
add_cosmos_client
(
COSMOS_CLIENT
)
BOT
.
add_web_app
(
APP
)
BOT
.
add_cosmos_client
(
COSMOS_CLIENT
)
return
app
if
__name__
==
"__main__"
:
init_logging
()
try
:
web
.
run_app
(
APP
,
host
=
"0.0.0.0"
,
port
=
app_config
.
PORT
)
web
.
run_app
(
app_factory
(
BOT
)
,
host
=
"0.0.0.0"
,
port
=
app_config
.
PORT
)
except
Exception
as
error
:
raise
error
config.py
View file @
0c60fbfc
...
...
@@ -10,7 +10,6 @@ from utils.token_helper import TokenHelper
PROJECT_ROOT_PATH
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
"__file__"
))
ASSETS_PATH
=
os
.
path
.
join
(
PROJECT_ROOT_PATH
,
"assets"
)
CARDS_PATH
=
os
.
path
.
join
(
ASSETS_PATH
,
"cards"
)
LOG_FILE
=
os
.
path
.
join
(
PROJECT_ROOT_PATH
,
"log.txt"
)
class
Auth
:
...
...
utils/cosmos_client.py
View file @
0c60fbfc
...
...
@@ -78,18 +78,29 @@ class CosmosClient:
return
await
self
.
execute_blocking
(
bl
)
async
def
get
_container
(
self
,
database_id
:
str
,
container_id
:
str
,
partition_key
:
Any
,
**
kwargs
)
->
ContainerProxy
:
"""
Get or c
reate container """
async
def
create
_container
(
self
,
database_id
:
str
,
container_id
:
str
,
partition_key
:
Any
,
**
kwargs
)
->
ContainerProxy
:
"""
C
reate container """
db
=
await
self
.
get_db
(
database_id
)
def
bl
()
->
ContainerProxy
:
""" Get Notifications container blocking """
try
:
return
db
.
get_container_client
(
container_id
)
except
exceptions
.
CosmosResourceNotFoundError
:
return
db
.
create_container
(
container_id
,
partition_key
,
**
kwargs
)
except
exceptions
.
CosmosResourceNotFoundError
:
pass
return
await
self
.
execute_blocking
(
bl
)
async
def
get_container
(
self
,
database_id
:
str
,
container_id
:
str
,
partition_key
:
Any
,
**
kwargs
)
->
ContainerProxy
:
""" Get or create container """
db
=
await
self
.
get_db
(
database_id
)
def
bl
()
->
ContainerProxy
:
""" Get Notifications container blocking """
return
db
.
get_container_client
(
container_id
)
return
await
self
.
execute_blocking
(
bl
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment