restful架构:
- 每一个URI代表一种资源
- 客户端和服务器之间,传递这种资源的某种表现层
- 客户端通过四个HTTP动词,对服务器端资源进行操作,实现表现层状态转化
requests: https://requests.readthedocs.io/zh_CN/latest/user/quickstart.html
URL 解析
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
- https: 协议,http,websockets
-
qyapi.weixin.qq.com: 主机地址,DNS
- /cgi-bin/gettoken:资源地址
- ?corpid=ID&corpsecret=SECRET:参数
ppt
作业
- 实现通讯录的增删改查
- 使用 session 减少字段的重复项
Candice
(Candice)
2
实现通讯录的增删改查
并使用 session 减少字段的重复项:token,userid
import requests
from hamcrest import *
class TestWechatAccess:
# 实例化会话对象
s = requests.Session()
def setup(self):
params={
"corpid":"ww44051ca7a74ae8e3",
"corpsecret":"uhSqBpzjxIvpbfPaXuPH574dfKwPUcLzlnqOGEklghc"
}
res = self.s.get(url="https://qyapi.weixin.qq.com/cgi-bin/gettoken",params=params)
# 将token放入会话对象中
self. s.params.update({"access_token": res.json()['access_token']})
# 添加成员
def test_addMember(self):
data = {
"userid": "zhangsan",
"name": "张三",
"mobile": "13800000009",
"department": [1]
}
res = self.s.post(url=f'https://qyapi.weixin.qq.com/cgi-bin/user/create',json=data)
# 将userid放入会话对象中
self.s.params.update({"userid":"zhangsan"})
assert(self.test_getMember()['name']=="张三")
print(res.json())
# 读取成员
def test_getMember(self):
# params={
# "userid":"zhangsan"
# }
# res = self.s.get("https://qyapi.weixin.qq.com/cgi-bin/user/get",params=params)
res = self.s.get("https://qyapi.weixin.qq.com/cgi-bin/user/get")
return res.json()
print(res.json())
# 修改成员
def test_updateMember(self):
data={
"userid":"zhangsan",
"name":"王五"
}
res=self.s.post(url=f"https://qyapi.weixin.qq.com/cgi-bin/user/update",json=data)
assert (self.test_getMember()['name'] == "王五")
print(res.json())
# 删除成员
def test_deleteMember(self):
# params={
# "userid":"zhangsan"
# }
res = self.s.get(url="https://qyapi.weixin.qq.com/cgi-bin/user/delete")
assert_that(self.test_getMember()['errmsg'], starts_with('userid not found'))
print(res.json())
tzhang
(tzhang)
3
hq_199
(孓晓)
10
liuyu
(liuyu)
12
_yuge
(钱语歌)
15
16024900
(是小王ya)
17
jimmy2
(林遵明)
18