当前位置:网站首页>Automatic generation of client code from flask server code -- Introduction to flask native stubs Library
Automatic generation of client code from flask server code -- Introduction to flask native stubs Library
2022-07-03 15:27:00 【Likianta Me】
flask-native-stubs Project inspiration swagger Of codegen Tools . It is used to automatically generate the corresponding client interface function from the server's routing function .
You will experience something like “ Local function call ” As fast and natural . As follows :
Server code :
from flask_native_stubs import app, auto_route @auto_route() def hello_world(a: int, b: bool, c: str) -> str: print('received from client side:', a, b, c) return 'hello from server side!' app.run(host='127.0.0.1', port=8080)
Client code :
# client side from flask_native_stubs import setup_client # notes : take flask_native_stubs Auto generated code ( If called "server_stubs.py") Put it where it can # Enough place to import . from my_lib.server_stubs import hello_world # To configure host and port port . setup_client(host='127.0.0.1', port=8080) # It's like calling a local function . No matter how it is written , still ide Type tips , Code completion, etc # can , It can be implemented completely according to the habits of ordinary function calls ! response = hello_world(a=123, b=True, c='hello from client!') print(response) # -> 'hello from server side!'
Use flask_native_stubs Auto generated code ( Referenced in the above code block “server_stubs.py”) What does it look like :
from flask_native_stubs.stubgen import magic_route # This is one that doesn't have any function " Pseudofunction " def hello_world(a: int, b: bool, c: str) -> str: ... # Here are three points . # Last use magic_route Magic method of this module hello_world Function grafted to request # Decorator . [magic_route(x) for x in ('hello_world',)]
Run a screenshot :
What is a stub file (stubs)
“stub” Quote from java j2ee Of stub Concept :
Invokes an object on a remote host for masking clients , You must provide some way to simulate local objects , Such local objects are called stubs (stub). The stub is responsible for receiving the local method call , And delegate them to their concrete implementation objects .
stay python in , Also have stub The concept of , Mainly used for the enhanced experience of type checking . ( With python The type annotation is becoming more and more perfect , Some people think that stub Its role in this regard has been weakened .)
We use the shell of the original function on the server side , The generated shapes are similar but do not have any real business logic “ Pseudo function ”, Call it stubs, In terms of concept and purpose of use, it is consistent with the above .
How to generate stub file (stubs)
Generate flask-native-stubs The stub file method of is very simple . Just put the app.run
Change it to app.generate_stubs
that will do :
# server side
from flask_native_stubs import app, auto_route
@auto_route()
def hello_world(a: int, b: bool, c: str) -> str:
print('received from client side:', a, b, c)
return 'hello from server side!'
# app.generate_stubs(output_dir: str, filenames_map: dict = None)
# args:
# output_dir: In which directory will the generated stub file be placed .
# filenames_map: ( Optional ) Customize input and output file names . Key is the input file path , Value is lose
# Out of the file path .
# Paths can use relative or absolute paths .
# When using relative paths , The input is relative to the current working directory ; The output is relative to # output_dir Parameters .
app.generate_stubs(
output_dir='../lib',
filenames_map={
'server.py': 'server_stubs.py'
}
)
Copy the generated stub file to the client project , Put it where the client can import . ( Like joining in sys.path
, Or join PYTHONPATH In the environment variables .)
边栏推荐
- 高并发下之redis锁优化实战
- Popular understanding of decision tree ID3
- 视觉上位系统设计开发(halcon-winform)
- Summary of JVM knowledge points
- leetcode_ Power of Four
- XWiki安装使用技巧
- Jvm-03-runtime data area PC, stack, local method stack
- Creation and destruction of function stack frames
- [probably the most complete in Chinese] pushgateway entry notes
- The markdown file obtains the pictures of the network and stores them locally and modifies the URL
猜你喜欢
详解指针进阶1
Jvm-02-class loading subsystem
秒杀系统2-Redis解决分布式Session问题
Visual upper system design and development (Halcon WinForm) -5 camera
Unity function - unity offline document download and use
What are the composite types of Blackhorse Clickhouse, an OLAP database recognized in the industry
Jvm-06-execution engine
Creation and destruction of function stack frames
redis缓存穿透,缓存击穿,缓存雪崩解决方案
Detailed explanation of string function and string function with unlimited length
随机推荐
Redis cache penetration, cache breakdown, cache avalanche solution
Finally, someone explained the financial risk management clearly
Halcon与Winform学习第二节
Use of Tex editor
使用JMeter对WebService进行压力测试
How are integer and floating-point types stored in memory
Seckill system 3- product list and product details
Kubernetes带你从头到尾捋一遍
Kubernetes advanced training camp pod Foundation
Functional modules and application scenarios covered by the productization of user portraits
Summary of JVM knowledge points
The markdown file obtains the pictures of the network and stores them locally and modifies the URL
Win10 enterprise 2016 long term service activation tutorial
Jvm-09 byte code introduction
需要知道的字符串函数
GCC cannot find the library file after specifying the link library path
视觉上位系统设计开发(halcon-winform)-3.图像控件
How to use annotations such as @notnull to verify and handle global exceptions
Nppexec get process return code
Stress test WebService with JMeter