Completed
Pull Request — master (#554)
by
unknown
03:07
created

WechatRequest.get()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
1
import requests
2
3
from lib.wechatutil.gettoken import gettoken
4
5
6
class WechatRequest:
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 errmsg
0 ignored issues
show
Bug introduced by
Raising NoneType while only classes or instances are allowed
Loading history...
37