api-shop 1.15.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
api_shop/__init__.py ADDED
@@ -0,0 +1,4 @@
1
+ from .api_shop import *
2
+
3
+ name = 'api_shop'
4
+ __version__ = "1.15.0"
@@ -0,0 +1,297 @@
1
+ Metadata-Version: 1.1
2
+ Name: api-shop
3
+ Version: 1.6.1
4
+ Summary: RESTful api shop for django or flask
5
+ Home-page: https://github.com/pcloth/api-shop
6
+ Author: pcloth
7
+ Author-email: pcloth@gmail.com
8
+ License: BSD License
9
+ Description-Content-Type: UNKNOWN
10
+ Description: api-shop
11
+
12
+ English Documents
13
+
14
+ https://github.com/pcloth/api-shop/blob/master/README.EN.MD
15
+
16
+ ======================================
17
+
18
+ 什么是api-shop:提供易用的、轻量化的restful-api接口工具包,基于django或者flask框架。
19
+
20
+ **demo 图片**
21
+ -------------
22
+
23
+ .. figure:: /static/demo.png
24
+ :alt: demo
25
+
26
+ demo
27
+ **核心功能:**
28
+ --------------
29
+
30
+ 1. 配置化api生成。
31
+ 2. 自动校验request提交的数据,并转换成制定格式。
32
+ 3. 自动生成api文档,并提供一个web页面可供查询和mock数据演示。
33
+ 4. 兼容django 和 flask
34
+ 5. 容器格式转换,data\_format.datetime格式转换类;'2019-01-18 23:25:25'
35
+ to datetime
36
+ 6. 自定义格式转换器
37
+ 7. 多国语言支持。
38
+ 8. 文档支持热重载。
39
+
40
+ **更新记录:**
41
+ --------------
42
+ > 2019-01-29
43
+ >
44
+ > ver 1.6.1
45
+ - 文档支持热重载
46
+ - 文档添加版本支持
47
+ - 优化错误提示
48
+
49
+ --------------
50
+
51
+ 2019-01-23
52
+
53
+ ver 1.6.0
54
+
55
+ - 添加多国语言支持,可以在options里指定语言或者扩展语言包。
56
+ - 文档改进
57
+
58
+ **用法:**
59
+ ----------
60
+
61
+ 1. 安装:
62
+
63
+ .. code:: sh
64
+
65
+ sudo pip install api-shop
66
+
67
+ 2. 引入:
68
+
69
+ .. code:: python
70
+
71
+ from api_shop from ApiShop,Api,data_format
72
+
73
+ +------------+---------------+------------------------------+
74
+ | 模块名字 | 功能说明 | 模块介绍 |
75
+ +============+===============+==============================+
76
+ | ApiShop | api初始化类 | 用以加载conf和options |
77
+ +------------+---------------+------------------------------+
78
+ | Api | 业务继承类 | 用来继承后写实际的业务代码 |
79
+ +------------+---------------+------------------------------+
80
+
81
+ 3. 初始化 \`\`\` python conf = [ { 'url': 'login', 'class':
82
+ 'account.views.api\_login', 'name': '账户登录', 'methods': { 'POST':
83
+ [ {'name':'username', 'type': str, 'required': True, 'min': 3, 'max':
84
+ 24, 'description': '用户名'}, {'name':'password', 'type': str,
85
+ 'required': True, 'min': 3, 'max': 24, 'description': '密码'}, ],
86
+ 'GET':` <#section>`__ } }, ]
87
+
88
+ \`\`\` > conf 配置说明 > 键 \| 值类型 \| 说明 :----------- \|
89
+ :----------- \| -----------: url \| str \|
90
+ 接口的url地址,只需要填写相对地址 class \| str,class \|
91
+ 接口实际调用的业务类(继承至Api),可以是对象,也可以是引用地址 name \|
92
+ str \| 接口的名字 methods \| dict \| 接口所能接收的methods:有GET POST
93
+ DELETE PUT PATCH
94
+
95
+ methods 配置说明
96
+
97
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
98
+ | 键 | 值类型 | 说明 |
99
+ +===============+===========+=============================================================================================================+
100
+ | name | str | 参数名,接收后在data.name |
101
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
102
+ | type | class | str,int,float,bool,list,dict,tuple等等,也支持data\_format.datetime时间格式,你也可以自定义一个类型转换器 |
103
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
104
+ | required | bool | 是否是必要值 |
105
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
106
+ | default | str | 当没有接收到时的默认值,注意,它也会被type所指定的类型转换器转换。 |
107
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
108
+ | min | int,str | 最小值/最小长度,为字符串时,会被type指定的类型转换器转换。 |
109
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
110
+ | max | int,str | 最大值/最大长度,为字符串时,会被type指定的类型转换器转换。 |
111
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
112
+ | description | str | 功能描述,给前端人员看文档的内容 |
113
+ +---------------+-----------+-------------------------------------------------------------------------------------------------------------+
114
+
115
+ 4. 配置
116
+
117
+ .. code:: python
118
+
119
+ options = {
120
+ 'base_url':'/api/',
121
+ 'bad_request': True,
122
+ 'document': BASE_DIR + '/api_shop/static/document.html',
123
+ 'lang':'en',
124
+ 'lang_pack':{}
125
+ }
126
+
127
+ options 配置说明
128
+
129
+ +----------------+-------------+----------+---------------------------------------------------------------+
130
+ | 键 | 值类型 | 默认值 | 说明 |
131
+ +================+=============+==========+===============================================================+
132
+ | base\_url | str | /api/ | 接口url前缀 |
133
+ +----------------+-------------+----------+---------------------------------------------------------------+
134
+ | bad\_request | bool | True | 如果请求不合法,是否以坏请求方式返回;否则就是全部是200返回 |
135
+ +----------------+-------------+----------+---------------------------------------------------------------+
136
+ | document | str(path) | 略 | 文档页面的html模板所在的路径,默认会有一个简易模板 |
137
+ +----------------+-------------+----------+---------------------------------------------------------------+
138
+ | lang | str | en | 多国语言支持,目前内置en, zh |
139
+ +----------------+-------------+----------+---------------------------------------------------------------+
140
+ | lang\_pack | dict | 无 | 扩展语言包,如果你想让api-shop支持更多语言 |
141
+ +----------------+-------------+----------+---------------------------------------------------------------+
142
+
143
+ lang\_pack 语言包
144
+
145
+ value 就是目标语言
146
+
147
+ .. code:: python
148
+
149
+ 'lang_pack':{
150
+ 'en': {
151
+ 'django version error': 'Django version is not compatible',
152
+ 'not flask or django': 'Currently only compatible with django and flask',
153
+ 'no attributes found': 'No attributes found: ',
154
+ 'not found in conf': 'Not found in conf: ',
155
+ 'document template not found': 'Document template not found',
156
+ 'no such interface': 'No such interface',
157
+ 'is required': 'is required',
158
+ 'parameter': 'Parameter',
159
+ 'can not be empty': 'can not be empty',
160
+ 'must be type': 'must be type',
161
+ 'minimum length': 'minimum length',
162
+ 'minimum value': 'minimum value',
163
+ 'maximum length': 'maximum length',
164
+ 'maximum value': 'maximum value',
165
+ 'The wrong configuration, methons must be loaded inside the list container.': 'The wrong configuration, methons must be loaded inside the list container.',
166
+ 'no such interface method': 'No such interface method',
167
+ }
168
+ }
169
+
170
+ 1. 自定义格式转换器
171
+
172
+ .. code:: python
173
+
174
+ # 使用自定义格式转换器的时候,min和max也会自动加载这个转换器转换了进行比较
175
+ from datetime import datetime as dt
176
+ class datetime():
177
+ '''将str转换成datetime格式'''
178
+ def __new__(self, string):
179
+ if ':' in string:
180
+ return dt.strptime(string, '%Y-%m-%d %H:%M:%S')
181
+ else:
182
+ return dt.strptime(string, '%Y-%m-%d')
183
+
184
+ 例子
185
+ ----
186
+
187
+ 1. `Django例子 <https://github.com/pcloth/api-shop/tree/master/django_demo>`__
188
+ \`\`\`python ## urls.py from api\_shop import ApiShop
189
+
190
+ 接口配置数据
191
+ ------------
192
+
193
+ conf = [ { 'url': 'login', 'class': 'account.views.api\_login',
194
+ #需要引入的api类,继承于上面说的Api接口类 'name': '账户登录', 'methods':
195
+ { 'POST': [ {'name':'username', 'type': str, 'required': True, 'min': 3,
196
+ 'max': 24, 'description': '用户名'}, {'name':'password', 'type': str,
197
+ 'required': True, 'min': 3, 'max': 24, 'description': '密码'}, ] ##
198
+ 这里可以插入更多的methods,比如GET,DELETE,POST,PATCH } }, ##
199
+ 这里可以插入更多的api接口
200
+
201
+ ]
202
+
203
+ api-shop参数设置:
204
+ ------------------
205
+
206
+ options = { 'base\_url':'/api/',# 基础url,用以组合给前端的api url
207
+ 可默认 # 'document':BASE\_DIR+'/api\_shop/static/document.html', #
208
+ 文档路由渲染的模板 可默认 'bad\_request':True, #
209
+ 参数bad\_request如果是真,发生错误返回一个坏请求给前端,否则都返回200的response,里面附带status=error和msg附带错误信息
210
+ 可默认 }
211
+
212
+ ap = ApiShop(conf,options)
213
+
214
+ app\_name='api'
215
+
216
+ urlpatterns = [ path('api\_data', ap.get\_api\_data, name='api\_data'),
217
+ # api文档需要的接口 path('document/', ap.render\_documents,
218
+ name='document'), #api文档渲染的路由 re\_path(r'([]\*)', ap.api\_entry,
219
+ name='index') # 接管api/下面其他的全部路由到api\_entry入口方法 ]
220
+
221
+ \`\`\`
222
+
223
+ .. code:: python
224
+
225
+ ## account/views.py
226
+ from api_shop from Api
227
+
228
+ class api_login(Api):
229
+ def post(self,request,data=None):
230
+ '''api登陆接口,方便微信用户绑定账户'''
231
+ username = data.username
232
+ password = data.password
233
+ user = authenticate(username=username, password=password)
234
+ if user:
235
+ login(request, user)
236
+ token = TokenBackend.make_token(user).decode('utf-8')
237
+ return JsonResponse({'status': 'success', 'msg': '执行成功', 'token': token})
238
+
239
+ return JsonResponse({'status': 'error', 'msg': '用户登录失败'})
240
+
241
+ 2. `flask例子 <https://github.com/pcloth/api-shop/tree/master/flask_demo>`__
242
+ \`\`\`python from flask import Flask,request,render\_template\_string
243
+
244
+ from werkzeug.routing import BaseConverter
245
+
246
+ from api\_shop import ApiShop,Api
247
+
248
+ class RegexConverter(BaseConverter): def **init**\ (self, map, \*args):
249
+ self.map = map self.regex = args[0]
250
+
251
+ app = Flask(\ **name**) #
252
+ 如果使用蓝图,添加正则处理器必须是在注册蓝图之前使用。
253
+ app.url\_map.converters['regex'] = RegexConverter
254
+
255
+ conf = [ { 'url': 'login', 'class': 'api.views.api\_login', 'name':
256
+ '账户登录', 'methods': { 'POST': [ {'name':'username', 'type': str,
257
+ 'required': True, 'min': 3, 'max': 24, 'description': '用户名'},
258
+ {'name':'password', 'type': str, 'required': True, 'min': 3, 'max': 24,
259
+ 'description': '密码'}, ] } }, { 'url': 'test', 'class':
260
+ 'api.views.test', 'name': '测试数据', 'methods': { 'GET':[{'name':'bb',
261
+ 'type': int, 'required': True, 'min': 0, 'max': 100, 'description':
262
+ '百分比','default':95},], 'POST': [ {'name':'add', 'type': str,
263
+ 'required': True, 'min': 3, 'max': 24, 'description': '地址'},
264
+ {'name':'bb', 'type': int, 'required': True, 'min': 0, 'max': 100,
265
+ 'description': '百分比','default':95}, {'name':'list', 'type': list,
266
+ 'description': '列表'}, ], 'DELETE':[ {'name':'id', 'type': int,
267
+ 'required': True, 'min': 1,'description': '编号'}, ] } },
268
+
269
+ ]
270
+
271
+ af = ApiShop(conf)
272
+
273
+ @app.route('/api/',methods=['GET', 'POST','PUT','DELETE','PATCH']) def
274
+ hello\_world(url): print(url) if url=='document/': return
275
+ af.render\_documents(request,url) if url=='api\_data': return
276
+ af.get\_api\_data(request,url)
277
+
278
+ ::
279
+
280
+ return af.api_entry(request,url)
281
+
282
+ if **name** == '**main**\ ': app.run(host="0.0.0.0",debug=True) \`\`\`
283
+
284
+ Keywords: api-shop,Flask-RESTful,Django REST framework,RESTful
285
+ Platform: all
286
+ Classifier: Development Status :: 4 - Beta
287
+ Classifier: Operating System :: OS Independent
288
+ Classifier: Intended Audience :: Developers
289
+ Classifier: License :: OSI Approved :: BSD License
290
+ Classifier: Programming Language :: Python
291
+ Classifier: Programming Language :: Python :: Implementation
292
+ Classifier: Programming Language :: Python :: 2.7
293
+ Classifier: Programming Language :: Python :: 3
294
+ Classifier: Programming Language :: Python :: 3.4
295
+ Classifier: Programming Language :: Python :: 3.5
296
+ Classifier: Programming Language :: Python :: 3.6
297
+ Classifier: Topic :: Software Development :: Libraries
@@ -0,0 +1,7 @@
1
+ MANIFEST.in
2
+ setup.py
3
+ api_shop/api_shop.egg-info/PKG-INFO
4
+ api_shop/api_shop.egg-info/SOURCES.txt
5
+ api_shop/api_shop.egg-info/dependency_links.txt
6
+ api_shop/api_shop.egg-info/top_level.txt
7
+ api_shop/static/document.html
@@ -0,0 +1 @@
1
+