Python3教程Web开发实战梳理-day9(编写API)
来源:互联网 发布:淘宝买家怎么修改评价 编辑:程序博客网 时间:2024/06/05 17:45
Day9:编写API
什么是API?
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码,或理解内部工作机制的细节。
还记得第一次接触API,是在学习Android开发时。写了那么多的本地小程序,一直在自己手机上玩没什么意思,总得和这个世界有点连接。学习了简单的HTTP通信之后,利用API从网络上获取信息。但是对于计算机来说,获取的信息格式毕竟是非常固定的,所以在开发APP时我们应用API的任务一方面是通信,另一方面就是对获取的信息解析了。
主流格式是XML和JSON,其中JSON近年来最为流行。由于JSON能直接被JavaScript读取,所以,以JSON格式编写的REST风格的API具有简单、易读、易用的特点。
那么对应的,我们编写API时的任务就是在URL中给出对应格式(JSON)的数据。
如果我们想要获取一篇Blog,输入http://localhost:9000/blog/123,就可以看到id为123的Blog页面,但这个结果是HTML页面,它同时混合包含了Blog的数据和Blog的展示两个部分。对于用户来说,阅读起来没有问题,但是,如果机器读取,就很难从HTML中解析出Blog的数据。
如果一个URL返回的不是HTML,而是机器能直接解析的数据,这个URL就可以看成是一个Web API。比如,读取http://localhost:9000/api/blogs/123,如果能直接返回Blog的数据,那么机器就可以直接读取。
编写API有什么好处呢?由于API就是把Web App的功能全部封装了,所以,通过API操作数据,可以极大地把前端和后端的代码隔离,使得后端代码易于测试,前端代码编写更简单。
在之前的代码中我们已经使用过APIError的一些调用了,下面给出完整代码。
apis.py:
#!/usr/bin/env python3# -*- coding: utf-8 -*-__author__ = 'Michael Liao''''JSON API definition.'''import json, logging, inspect, functoolsclass Page(object): ''' Page object for display pages. ''' def __init__(self, item_count, page_index=1, page_size=10): ''' Init Pagination by item_count, page_index and page_size. >>> p1 = Page(100, 1) >>> p1.page_count 10 >>> p1.offset 0 >>> p1.limit 10 >>> p2 = Page(90, 9, 10) >>> p2.page_count 9 >>> p2.offset 80 >>> p2.limit 10 >>> p3 = Page(91, 10, 10) >>> p3.page_count 10 >>> p3.offset 90 >>> p3.limit 10 ''' self.item_count = item_count self.page_size = page_size self.page_count = item_count // page_size + (1 if item_count % page_size > 0 else 0) if (item_count == 0) or (page_index > self.page_count): self.offset = 0 self.limit = 0 self.page_index = 1 else: self.page_index = page_index self.offset = self.page_size * (page_index - 1) self.limit = self.page_size self.has_next = self.page_index < self.page_count self.has_previous = self.page_index > 1 def __str__(self): return 'item_count: %s, page_count: %s, page_index: %s, page_size: %s, offset: %s, limit: %s' % (self.item_count, self.page_count, self.page_index, self.page_size, self.offset, self.limit) __repr__ = __str__class APIError(Exception): ''' the base APIError which contains error(required), data(optional) and message(optional). ''' def __init__(self, error, data='', message=''): super(APIError, self).__init__(message) self.error = error self.data = data self.message = messageclass APIValueError(APIError): ''' Indicate the input value has error or invalid. The data specifies the error field of input form. ''' def __init__(self, field, message=''): super(APIValueError, self).__init__('value:invalid', field, message)class APIResourceNotFoundError(APIError): ''' Indicate the resource was not found. The data specifies the resource name. ''' def __init__(self, field, message=''): super(APIResourceNotFoundError, self).__init__('value:notfound', field, message)class APIPermissionError(APIError): ''' Indicate the api has no permission. ''' def __init__(self, message=''): super(APIPermissionError, self).__init__('permission:forbidden', 'permission', message)if __name__=='__main__': import doctest doctest.testmod()
一个API也是一个URL的处理函数,我们希望能直接通过一个@api来把函数变成JSON格式的REST API。例如,获取注册用户可以用一个API实现如下:
@get('/api/users')def api_get_users(*, page='1'): page_index = get_page_index(page) num = yield from User.findNumber('count(id)') p = Page(num, page_index) if num == 0: return dict(page=p, users=()) users = yield from User.findAll(orderBy='created_at desc', limit=(p.offset, p.limit)) for u in users: u.passwd = '******' return dict(page=p, users=users)
只要返回一个dict,后续的response这个middleware就可以把结果序列化为JSON并返回。
可以在浏览器直接测试API,例如,输入http://localhost:9000/api/users,就可以看到返回的JSON啦。
- Python3教程Web开发实战梳理-day9(编写API)
- Python3教程Web开发实战梳理-day3(编写ORM)
- Python3教程Web开发实战梳理-day4(编写Model)
- Python3教程Web开发实战梳理-day7(编写MVC)
- Python3教程Web开发实战梳理-day11(编写日志创建页MVVC)
- Python3教程Web开发实战梳理-day12(编写日志列表页)
- Python3教程Web开发实战梳理-day5(Web框架)
- Python3教程Web开发实战梳理-day6(配置文件)
- Python3教程Web开发实战梳理-day8(构建前端)
- Python3教程Web开发实战梳理-day1+day2(开发环境与基本骨架)
- Python3教程Web开发实战梳理-day10(用户注册和登录)
- Python廖雪峰实战web开发(Day3-编写ORM)
- Python廖雪峰实战web开发(day4-编写Model)
- Python廖雪峰实战web开发(Day7-编写MVC)
- Web前端开发工程师实战培训教程
- Web前端开发工程师实战培训教程
- Web前端开发工程师实战培训教程
- Python廖雪峰实战web开发(Day2-编写Web APP骨架)
- Day 10
- Ubuntu安装jdk
- 数据库调优教程——优化sql语句
- hdu 2012 素数判定
- HDOJ 5920 Ugly Problem 【模拟】
- Python3教程Web开发实战梳理-day9(编写API)
- bzoj1939: [Croatian2010] Zuma
- [NOIP模拟赛]玻璃杯
- 逻辑卷管理【Ubuntu-shell】
- C语言第一章
- hdu_4284 (Floyd求最短路+dfs遍历)
- Spark中map与flatMap
- C语言——行列倒置函数实现
- 运行Java程序的参数