Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/larksuite/oapi-sdk-python i…
Browse files Browse the repository at this point in the history
…nto main
  • Loading branch information
zhaomingqiang committed Jan 8, 2022
2 parents 8f853f3 + bad9ecf commit 70fda5b
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sample/event/fastapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# -*- coding: UTF-8 -*-

from .server import *

45 changes: 45 additions & 0 deletions sample/event/fastapi/native_event_server_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# -*- coding: UTF-8 -*-

import uvicorn

from larksuiteoapi.event import handle_event, set_event_callback
from larksuiteoapi.model import OapiHeader, OapiRequest

from fastapi import FastAPI, Request, Response

from larksuiteoapi import Config, DOMAIN_FEISHU, DefaultLogger, LEVEL_DEBUG

# for Cutome APP(企业自建应用)
app_settings = Config.new_internal_app_settings_from_env()

# for memory store and logger(level=debug)
conf = Config(DOMAIN_FEISHU, app_settings, log_level=LEVEL_DEBUG)

app = FastAPI()


def app_open_event_handle(ctx, conf, event):
# type: (Context, Config, dict) -> None
print(ctx.get_request_id())
print(conf.app_settings)
print(event)


# set event type 'app_status_change' handle
set_event_callback(conf, 'app_open', app_open_event_handle)


@app.post('/webhook/event')
async def webhook_event(request: Request, resp: Response):
oapi_request = OapiRequest(uri=request.url.path, body=await request.body(), header=OapiHeader(request.headers))
oapi_resp = handle_event(conf, oapi_request)
resp.headers['Content-Type'] = oapi_resp.content_type
resp.body = oapi_resp.body
resp.status_code = oapi_resp.status_code
return resp


# 设置 "开发者后台" -> "事件订阅" 请求网址 URL:https://domain/webhook/event
# startup event http server, port: 8000
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)
2 changes: 2 additions & 0 deletions sample/event/fastapi/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fastapi
uvicorn[standard]
46 changes: 46 additions & 0 deletions sample/event/fastapi/sdk_event_server_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- coding: UTF-8 -*-

import uvicorn

from larksuiteoapi.event import handle_event
from larksuiteoapi.service.contact.v3 import UserUpdatedEventHandler, UserUpdatedEvent
from larksuiteoapi.model import OapiHeader, OapiRequest

from fastapi import FastAPI, Request, Response

from larksuiteoapi import Config, Context, DOMAIN_FEISHU, DefaultLogger, LEVEL_DEBUG

# for Cutome APP(企业自建应用)
app_settings = Config.new_internal_app_settings_from_env()

# for memory store and logger(level=debug)
conf = Config(DOMAIN_FEISHU, app_settings, log_level=LEVEL_DEBUG)


def user_update_handle(ctx, conf, event):
# type: (Context, Config, UserUpdatedEvent) ->None
print(ctx.get_request_id())
print(event.header)
print(event.event)
pass


# set event type 'contact.user.updated_v3' handle
UserUpdatedEventHandler.set_callback(conf, user_update_handle)

app = FastAPI()

@app.api_route('/webhook/event', methods=['GET', 'POST'])
async def webhook_event(request: Request, resp: Response):
oapi_request = OapiRequest(uri=request.url.path, body=await request.body(), header=OapiHeader(request.headers))
oapi_resp = handle_event(conf, oapi_request)
resp.headers['Content-Type'] = oapi_resp.content_type
resp.body = oapi_resp.body
resp.status_code = oapi_resp.status_code
return resp


# 设置 "开发者后台" -> "事件订阅" 请求网址 URL:https://domain/webhook/event
# startup event http server, port: 8000
if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)
83 changes: 83 additions & 0 deletions sample/event/fastapi/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: UTF-8 -*-
import attr
import uvicorn

from larksuiteoapi.event import handle_event, set_event_callback
from larksuiteoapi.event.model import BaseEvent
from larksuiteoapi.model import OapiHeader, OapiRequest
from larksuiteoapi.service.contact.v3 import UserUpdatedEventHandler, UserUpdatedEvent

from fastapi import FastAPI, Request, Response

from sample.config.config import test_config_with_memory_store, test_config_with_redis_store
from larksuiteoapi import Config, Context, DOMAIN_FEISHU, DOMAIN_LARK_SUITE

# for Cutome APP(企业自建应用)

app_settings = Config.new_internal_app_settings_from_env()

# for redis store and logger(level=debug)
conf = test_config_with_redis_store(DOMAIN_FEISHU, app_settings)


# for memory store and logger(level=debug)
# conf = test_config_with_memory_store(DOMAIN_FEISHU, app_settings)


@attr.s
class ApplicationAppStatusChangeEventData(object):
app_id = attr.ib(type=str, default='')
tenant_key = attr.ib(type=str, default='')
type = attr.ib(type=str, default='')
status = attr.ib(type=str, default='')


@attr.s
class ApplicationAppStatusChangeEvent(BaseEvent):
event = attr.ib(type=ApplicationAppStatusChangeEventData, default=None)


def app_status_change_event_handler(ctx, conf, event):
# type: (Context, Config, ApplicationAppStatusChangeEvent) -> None
print(ctx.get_request_id())
print(conf.app_settings)
print(event.event.type)


def user_change(ctx, conf, event):
# type: (Context, Config, dict) -> None
print(ctx.get_header().items())
print(ctx.get_request_id())
print(conf.app_settings)
print(event)


set_event_callback(conf, 'app_status_change', app_status_change_event_handler, ApplicationAppStatusChangeEvent)

set_event_callback(conf, 'user_update', user_change)


def user_update(ctx, conf, event):
# type: (Context, Config, UserUpdatedEvent) ->None
print(ctx.get_request_id())
print(event.header)
print(event.event)
pass


UserUpdatedEventHandler.set_callback(conf, user_update)

app = FastAPI()

@app.api_route('/webhook/event', methods=['GET', 'POST'])
async def webhook_event(request: Request, resp: Response):
oapi_request = OapiRequest(uri=request.url.path, body=await request.body(), header=OapiHeader(request.headers))
oapi_resp = handle_event(conf, oapi_request)
resp.headers['Content-Type'] = oapi_resp.content_type
resp.body = oapi_resp.body
resp.status_code = oapi_resp.status_code
return resp


if __name__ == '__main__':
uvicorn.run(app, host="0.0.0.0", port=8000)

0 comments on commit 70fda5b

Please sign in to comment.