项目结构
~~~sh
├── static #静态资源
├── templates
├── fisher.py
~~~
## 首先先构建搜索关键字的视图函数
~~~python
from flask import Flask
app = Flask(__name__)
@app.route('/book/search/<q>/<page>')
def search(q,page):
"""
q:代表普通关键字 或 isbn
isbn :
isbn10:由10位数字组成,其中可能包含'-'
isbn13:由13位数字组成
key:
"""
key_or_isbn = 'key'
if len(q) == 13 and q.isdigit():
key_or_isbn = 'isbn'
short_q = q.replace('-', '')
if len(q) == 10 and len(short_q) and short_q.isdigit():
key_or_isbn = 'isbn'
return key_or_isbn
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
~~~
项目结构
~~~sh
├── static #静态资源
├── templates
├── fisher.py
├── helper.py
~~~
然后把判断逻辑抽取出来,放到一个新文件里。
~~~python
#helper.py
def is_isbn_or_key(word):
"""
q:代表普通关键字 或 isbn
isbn :
isbn10:由10位数字组成,其中可能包含'-'
isbn13:由13位数字组成
key:
"""
key_or_isbn = 'key'
if len(word) == 13 and word.isdigit():
key_or_isbn = 'isbn'
short_word = word.replace('-', '')
if len(word) == 10 and len(short_word) and short_word.isdigit():
key_or_isbn = 'isbn'
return key_or_isbn
~~~
在主函数里导入`hellper.py`,调用方法返回判断结果
项目结构
~~~sh
├── static #静态资源
├── templates
├── fisher.py
├── helper.py
├── HttpRequest.py
~~~
## HTTP请求
创建一个`HttpRequest.py`
~~~python
# HttpRequest.py
import requests
class HTTP:
def get(self,url,return_json=True):
req = requests.get(url)
if return_json:
return req.json()
else:
return req.text
~~~
这样显然不够健壮,应该加入判断,如果请求返回`404`的时候的处理方法
~~~python
# HttpRequest.py
import requests
class HTTP:
def get(self,url,return_json=True):
req = requests.get(url)
if req.status_code == 200:
if return_json:
return req.json()
else:
return req.text
else :
if return_json:
return {}
else:
return ''
~~~
这样之后确实是逻辑上达到了要求,但是比较冗长,还可以简短一些,并且我们并没有用到`self`,所以可以使用`@staticemthod`装饰成静态方法
~~~python
# HttpRequest.py
import requests
class HTTP:
@staticmethod
def get(url,return_json=True):
req = requests.get(url)
if req.status_code != 200:
return {} if return_json else ''
return req.json() if return_json else req.text
~~~
## 从API中获取数据
~~~sh
├── static #静态资源
├── templates
├── fisher.py
├── helper.py
├── HttpRequest.py
├── YuShu_Book.py
~~~
~~~python
#YuShu_Book.py
from HttpRequest import HTTP
class YuShuBook:
isbn_url = 'http://t.yushu.im/v2/book/isbn/{}'
keyword_url = 'http://t.yushu.im/v2/book/search?q={}&start={}&count={}'
@classmethod
def search_by_isbn(cls,isbn):
url = cls.isbn_url.format(isbn)
result = HTTP.get(url)
#接收一个json数据
return result
@classmethod
def search_by_keyword(cls,keyword):
url = cls.keyword_url.format(keyword,start=15,count=0)
result = HTTP.get(url)
return result
~~~
## 重构fisher.py
~~~python
from flask import Flask,jsonify
from yushu_book import YuShuBook
from helper import is_isbn_or_key
app = Flask(__name__)
@app.route('/book/search/<q>/<page>')
def search(q,page):
isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = YuShuBook.search_by_isbn(q)
else:
result = YuShuBook.search_by_keyword(q)
return jsonify(result) # 因为Respone只允许接收字符串,元组,Response对象,使用jsonify 可以把dict处理成flask.wrappers.Response,就可以成为相应体
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
~~~
## 将识图函数拆分到单独文件中
用`BulePrint`将拆分的文件注册到`app`上,达到拆分的效果
重构了很多东西,所以把每个包里的东西都会列出来
~~~sh
├── app
│ ├── web
│ │ ├── __init_.py
│ │ └── book.py
│ └── __init__.py
├── static
├── templates
├── fisher.py
├── helper.py
├── HttpRequest.py
├── YuShu_Book.py
~~~
首先把和书有关的视图函数抽出来,注册一个`web`蓝图到`app`上
~~~python
# web/book.py
from flask import jsonify
from flask import Blueprint
from helper import is_isbn_or_key
from yushu_book import YuShuBook
web = Blueprint('web',__name__)
@web.route('/book/search/<q>/<page>')
def search(q,page):
isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = YuShuBook.search_by_isbn(q)
else:
result = YuShuBook.search_by_keyword(q)
return jsonify(result)
~~~
然后,将`app`对象初始化重构,把实例化`flask`对象,放到`__init__`里,
然后将蓝本注册到`app`上,并且把实例化的`app`对象返回
~~~python
# app/__init__.py
from flask import Flask
def create_app():
app = Flask(__name__)
register_blueprint(app)
return app
def register_blueprint(app):
from app.web.book import web
app.register_blueprint(web)
~~~
主函数里调用运行`app`
~~~python
#fisher.py
from app import create_app
app = create_app()
@app.route('/')
def hello_world():
return 'Hello fisher!'
if __name__ == '__main__':
app.run()
~~~