WeChatMenu.add_conditional()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 60
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.125

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 2
dl 0
loc 60
rs 10
c 0
b 0
f 0
ccs 1
cts 2
cp 0.5
crap 1.125

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3
4 10
from wechatpy.exceptions import WeChatClientException
5 10
from wechatpy.client.api.base import BaseWeChatAPI
6
7
8 10
class WeChatMenu(BaseWeChatAPI):
9
10 10
    def get(self):
11
        """
12
        查询自定义菜单。
13
        详情请参考
14
        http://mp.weixin.qq.com/wiki/16/ff9b7b85220e1396ffa16794a9d95adc.html
15
16
        :return: 返回的 JSON 数据包
17
18
        使用示例::
19
20
            from wechatpy import WeChatClient
21
22
            client = WeChatClient('appid', 'secret')
23
            menu = client.menu.get()
24
25
        """
26 10
        try:
27 10
            return self._get('menu/get')
28
        except WeChatClientException as e:
29
            if e.errcode == 46003:
30
                # menu not exist
31
                return None
32
            else:
33
                raise e
34
35 10
    def create(self, menu_data):
36
        """
37
        创建自定义菜单 ::
38
39
            from wechatpy import WeChatClient
40
41
            client = WeChatClient("appid", "secret")
42
            client.menu.create({
43
                "button":[
44
                    {
45
                        "type":"click",
46
                        "name":"今日歌曲",
47
                        "key":"V1001_TODAY_MUSIC"
48
                    },
49
                    {
50
                        "type":"click",
51
                        "name":"歌手简介",
52
                        "key":"V1001_TODAY_SINGER"
53
                    },
54
                    {
55
                        "name":"菜单",
56
                        "sub_button":[
57
                            {
58
                                "type":"view",
59
                                "name":"搜索",
60
                                "url":"http://www.soso.com/"
61
                            },
62
                            {
63
                                "type":"view",
64
                                "name":"视频",
65
                                "url":"http://v.qq.com/"
66
                            },
67
                            {
68
                                "type":"click",
69
                                "name":"赞一下我们",
70
                                "key":"V1001_GOOD"
71
                            }
72
                        ]
73
                    }
74
                ]
75
            })
76
77
        详情请参考
78
        https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
79
80
        :param menu_data: Python 字典
81
82
        :return: 返回的 JSON 数据包
83
        """
84 10
        return self._post(
85
            'menu/create',
86
            data=menu_data
87
        )
88
89 10
    def update(self, menu_data):
90
        """
91
        更新自定义菜单 ::
92
93
            from wechatpy import WeChatClient
94
95
            client = WeChatClient("appid", "secret")
96
            client.menu.update({
97
                "button":[
98
                    {
99
                        "type":"click",
100
                        "name":"今日歌曲",
101
                        "key":"V1001_TODAY_MUSIC"
102
                    },
103
                    {
104
                        "type":"click",
105
                        "name":"歌手简介",
106
                        "key":"V1001_TODAY_SINGER"
107
                    },
108
                    {
109
                        "name":"菜单",
110
                        "sub_button":[
111
                            {
112
                                "type":"view",
113
                                "name":"搜索",
114
                                "url":"http://www.soso.com/"
115
                            },
116
                            {
117
                                "type":"view",
118
                                "name":"视频",
119
                                "url":"http://v.qq.com/"
120
                            },
121
                            {
122
                                "type":"click",
123
                                "name":"赞一下我们",
124
                                "key":"V1001_GOOD"
125
                            }
126
                        ]
127
                    }
128
                ]
129
            })
130
131
        详情请参考
132
        https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013
133
134
        :param menu_data: Python 字典
135
136
        :return: 返回的 JSON 数据包
137
        """
138 10
        return self.create(menu_data)
139
140 10
    def delete(self):
141
        """
142
        删除自定义菜单。
143
        详情请参考
144
        http://mp.weixin.qq.com/wiki/16/8ed41ba931e4845844ad6d1eeb8060c8.html
145
146
        :return: 返回的 JSON 数据包
147
148
        使用示例::
149
150
            from wechatpy import WeChatClient
151
152
            client = WeChatClient('appid', 'secret')
153
            res = client.menu.delete()
154
155
        """
156 10
        return self._get('menu/delete')
157
158 10
    def get_menu_info(self):
159
        """
160
        获取自定义菜单配置
161
        详情请参考
162
        http://mp.weixin.qq.com/wiki/17/4dc4b0514fdad7a5fbbd477aa9aab5ed.html
163
164
        :return: 返回的 JSON 数据包
165
166
        使用示例::
167
168
            from wechatpy import WeChatClient
169
170
            client = WeChatClient('appid', 'secret')
171
            menu_info = client.menu.get_menu_info()
172
173
        """
174 10
        return self._get('get_current_selfmenu_info')
175
176 10
    def add_conditional(self, menu_data):
177
        """
178
        创建个性化菜单 ::
179
180
            from wechatpy import WeChatClient
181
182
            client = WeChatClient("appid", "secret")
183
            client.menu.add_conditional({
184
                "button":[
185
                    {
186
                        "type":"click",
187
                        "name":"今日歌曲",
188
                        "key":"V1001_TODAY_MUSIC"
189
                    },
190
                    {
191
                        "type":"click",
192
                        "name":"歌手简介",
193
                        "key":"V1001_TODAY_SINGER"
194
                    },
195
                    {
196
                        "name":"菜单",
197
                        "sub_button":[
198
                            {
199
                                "type":"view",
200
                                "name":"搜索",
201
                                "url":"http://www.soso.com/"
202
                            },
203
                            {
204
                                "type":"view",
205
                                "name":"视频",
206
                                "url":"http://v.qq.com/"
207
                            },
208
                            {
209
                                "type":"click",
210
                                "name":"赞一下我们",
211
                                "key":"V1001_GOOD"
212
                            }
213
                        ]
214
                    }
215
                ],
216
                "matchrule":{
217
                  "group_id":"2",
218
                  "sex":"1",
219
                  "country":"中国",
220
                  "province":"广东",
221
                  "city":"广州",
222
                  "client_platform_type":"2"
223
                }
224
            })
225
226
        详情请参考
227
        http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
228
229
        :param menu_data: Python 字典
230
231
        :return: 返回的 JSON 数据包
232
        """
233
        return self._post(
234
            'menu/addconditional',
235
            data=menu_data
236
        )
237
238 10
    def del_conditional(self, menu_id):
239
        """
240
        删除个性化菜单
241
242
        详情请参考
243
        http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
244
245
        :param menu_id: 菜单ID
246
247
        :return: 返回的 JSON 数据包
248
249
        使用示例::
250
251
            from wechatpy import WeChatClient
252
253
            client = WeChatClient('appid', 'secret')
254
            res = client.menu.del_conditional('menu_id')
255
256
        """
257
        return self._post(
258
            'menu/delconditional',
259
            data={'menuid': menu_id}
260
        )
261
262 10
    def try_match(self, user_id):
263
        """
264
        测试个性化菜单匹配结果
265
266
        详情请参考
267
        http://mp.weixin.qq.com/wiki/0/c48ccd12b69ae023159b4bfaa7c39c20.html
268
269
        :param user_id: 可以是粉丝的OpenID,也可以是粉丝的微信号。
270
271
        :return: 该接口将返回菜单配置
272
273
        使用示例::
274
275
            from wechatpy import WeChatClient
276
277
            client = WeChatClient('appid', 'secret')
278
            res = client.menu.try_match('openid')
279
280
        """
281
        return self._post(
282
            'menu/trymatch',
283
            data={'user_id': user_id}
284
        )
285