-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/larksuite/oapi-sdk-python i…
…nto main
- Loading branch information
Showing
5 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# -*- coding: UTF-8 -*- | ||
|
||
from .server import * | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
fastapi | ||
uvicorn[standard] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |