Total Complexity | 7 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | import requests |
||
6 | class WechatRequest(object): |
||
7 | |||
8 | defaultheader = {'content-type': 'application/json; charset=utf8'} |
||
9 | |||
10 | def __init__(self, corpId, corpSecret): |
||
11 | self.corpID = corpId |
||
12 | self.corpSecret = corpSecret |
||
13 | self.token = '' |
||
14 | self.session = requests.session() |
||
15 | self.refreshtoken() |
||
16 | |||
17 | def get(self, url, customparams, customheader=None): |
||
18 | header = self.defaultheader |
||
19 | params = {"access_token": self.token} |
||
20 | params.update(customparams) |
||
21 | if customheader is not None: |
||
22 | header.update(customheader) |
||
23 | response = self.session.get(url, params=params, headers=header) |
||
24 | return response.json() |
||
25 | |||
26 | def post(self, url, data, customheader=None): |
||
27 | header = self.defaultheader |
||
28 | if customheader is not None: |
||
29 | header.update(customheader) |
||
30 | response = self.session.post(url + "?access_token=" + self.token, data=data, headers=header) |
||
31 | return response.json() |
||
32 | |||
33 | def refreshtoken(self): |
||
34 | rc, self.token, errmsg = gettoken(self.corpID, self.corpSecret, self.session) |
||
35 | if rc != 0: |
||
36 | raise ValueError(errmsg) |
||
37 |