wechatpy.client.api.merchant   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 427
Duplicated Lines 0 %

Test Coverage

Coverage 57.29%

Importance

Changes 0
Metric Value
eloc 175
dl 0
loc 427
rs 9.76
c 0
b 0
f 0
ccs 51
cts 89
cp 0.5729
wmc 33

32 Methods

Rating   Name   Duplication   Size   Complexity  
A WeChatMerchant.get() 0 6 1
A WeChatMerchant.delete() 0 6 1
A WeChatMerchant.add_shelf() 0 10 1
A WeChatMerchant.del_express() 0 11 1
A WeChatMerchant.get_all_express() 0 9 1
A WeChatMerchant.get_all_groups() 0 8 1
A WeChatMerchant.get_shelf() 0 11 1
A WeChatMerchant.query_order() 0 15 1
A WeChatMerchant.reduce_stock() 0 15 1
A WeChatMerchant.update() 0 6 1
A WeChatMerchant.get_category_property() 0 11 1
A WeChatMerchant.update_shelf() 0 12 1
A WeChatMerchant.get_express() 0 11 1
A WeChatMerchant.__init__() 0 11 1
A WeChatMerchant.upload_image() 0 15 2
A WeChatMerchant.create() 0 5 1
A WeChatMerchant.get_all_shelves() 0 8 1
A WeChatMerchant.update_product_status() 0 7 1
A WeChatMerchant.get_group() 0 11 1
A WeChatMerchant.update_group_property() 0 12 1
A WeChatMerchant.add_express() 0 10 1
A WeChatMerchant.get_category_sku() 0 11 1
A WeChatMerchant.update_express() 0 12 1
A WeChatMerchant.get_order() 0 11 1
A WeChatMerchant.del_group() 0 11 1
A WeChatMerchant.del_shelf() 0 11 1
A WeChatMerchant.set_delivery() 0 12 1
A WeChatMerchant.get_by_status() 0 6 1
A WeChatMerchant.get_subcategories() 0 11 1
A WeChatMerchant.add_group() 0 10 1
A WeChatMerchant.add_stock() 0 15 1
A WeChatMerchant.update_group_product() 0 12 1
1
# -*- coding: utf-8 -*-
2 10
from __future__ import absolute_import, unicode_literals
3 10
from wechatpy.client.api.base import BaseWeChatAPI
4
5 10
from wechatpy.client.api.merchant.category import MerchantCategory
6 10
from wechatpy.client.api.merchant.stock import MerchantStock
7 10
from wechatpy.client.api.merchant.express import MerchantExpress
8 10
from wechatpy.client.api.merchant.group import MerchantGroup
9 10
from wechatpy.client.api.merchant.shelf import MerchantShelf
10 10
from wechatpy.client.api.merchant.order import MerchantOrder
11 10
from wechatpy.client.api.merchant.common import MerchantCommon
12
13
14 10
class WeChatMerchant(BaseWeChatAPI):
15 10
    API_BASE_URL = 'https://api.weixin.qq.com/'
16
17 10
    def __init__(self, *args, **kwargs):
18 10
        super(WeChatMerchant, self).__init__(*args, **kwargs)
19
20
        # sub APIs
21 10
        self.category = MerchantCategory(self._client)
22 10
        self.stock = MerchantStock(self._client)
23 10
        self.express = MerchantExpress(self._client)
24 10
        self.group = MerchantGroup(self._client)
25 10
        self.shelf = MerchantShelf(self._client)
26 10
        self.order = MerchantOrder(self._client)
27 10
        self.common = MerchantCommon(self._client)
28
29 10
    def create(self, product_data):
30
        """增加商品"""
31
        return self._post(
32
            'merchant/create',
33
            data=product_data
34
        )
35
36 10
    def delete(self, product_id):
37
        """删除商品"""
38
        return self._post(
39
            'merchant/del',
40
            data={
41
                'product_id': product_id
42
            }
43
        )
44
45 10
    def update(self, product_id, product_data):
46
        """修改商品"""
47
        product_data['product_id'] = product_id
48
        return self._post(
49
            'merchant/update',
50
            data=product_data
51
        )
52
53 10
    def get(self, product_id):
54
        """查询商品"""
55
        return self._post(
56
            'merchant/get',
57
            data={
58
                'product_id': product_id
59
            }
60
        )
61
62 10
    def get_by_status(self, status):
63
        """获取指定状态的所有商品"""
64
        return self._post(
65
            'merchant/getbystatus',
66
            data={
67
                'status': status
68
            }
69
        )
70
71 10
    def update_product_status(self, product_id, status):
72
        """商品上下架"""
73
        return self._post(
74
            'merchant/modproductstatus',
75
            data={
76
                'product_id': product_id,
77
                'status': status
78
            }
79
        )
80
81 10
    def get_subcategories(self, cate_id):
82
        """
83
        获取指定分类的所有子分类
84
85
        :param cate_id: 大分类ID(根节点分类id为1)
86
        :return: 返回的 JSON 数据包
87
        """
88
        return self._post(
89
            'merchant/category/getsub',
90
            data={
91
                'cate_id': cate_id
92
            }
93
        )
94
95 10
    def get_category_sku(self, cate_id):
96
        """
97
        获取指定子分类的所有SKU
98
99
        :param cate_id: 商品子分类ID
100
        :return: 返回的 JSON 数据包
101
        """
102
        return self._post(
103
            'merchant/category/getsku',
104
            data={
105
                'cate_id': cate_id
106
            }
107
        )
108
109 10
    def get_category_property(self, cate_id):
110
        """
111
        获取指定分类的所有属性
112
113
        :param cate_id: 商品子分类ID
114
        :return: 返回的 JSON 数据包
115
        """
116
        return self._post(
117
            'merchant/category/getproperty',
118
            data={
119
                'cate_id': cate_id
120
            }
121
        )
122
123 10
    def add_stock(self, product_id, sku_info, quantity):
124
        """
125
        增加库存
126
127
        :param product_id: 商品ID
128
        :param sku_info: sku信息,格式"id1:vid1;id2:vid2",如商品为统一规格,则此处赋值为空字符串即可
129
        :param quantity: 增加的库存数量
130
        :return: 返回的 JSON 数据包
131
        """
132
        return self._post(
133
            'merchant/stock/add',
134
            data={
135
                "product_id": product_id,
136
                "sku_info": sku_info,
137
                "quantity": quantity
138
            }
139
        )
140
141 10
    def reduce_stock(self, product_id, sku_info, quantity):
142
        """
143
        减少库存
144
145
        :param product_id: 商品ID
146
        :param sku_info: sku信息,格式"id1:vid1;id2:vid2",如商品为统一规格,则此处赋值为空字符串即可
147
        :param quantity: 减少的库存数量
148
        :return: 返回的 JSON 数据包
149
        """
150
        return self._post(
151
            'merchant/stock/reduce',
152
            data={
153
                "product_id": product_id,
154
                "sku_info": sku_info,
155
                "quantity": quantity
156
            }
157
        )
158
159 10
    def add_express(self, product_data):
160
        """
161
        增加邮费模板
162
163
        :param product_data: 邮费信息
164
        :return: 返回的 JSON 数据包
165
        """
166
        return self._post(
167
            'merchant/express/add',
168
            data=product_data
169
        )
170
171 10
    def del_express(self, template_id):
172
        """
173
        增加邮费模板
174
175
        :param template_id: 邮费模板ID
176
        :return: 返回的 JSON 数据包
177
        """
178
        return self._post(
179
            'merchant/express/del',
180
            data={
181
                'template_id': template_id
182
            }
183
        )
184
185 10
    def update_express(self, template_id, delivery_template):
186
        """
187
        增加邮费模板
188
189
        :param template_id: 邮费模板ID
190
        :param delivery_template: 邮费模板信息(字段说明详见增加邮费模板)
191
        :return: 返回的 JSON 数据包
192
        """
193
        delivery_template['template_id'] = template_id
194
        return self._post(
195
            'merchant/express/update',
196
            data=delivery_template
197
        )
198
199 10
    def get_express(self, template_id):
200
        """
201
        获取指定ID的邮费模板
202
203
        :param template_id: 邮费模板ID
204
        :return: 返回的 JSON 数据包
205
        """
206
        return self._post(
207
            'merchant/express/getbyid',
208
            data={
209
                'template_id': template_id
210
            }
211
        )
212
213 10
    def get_all_express(self):
214
        """
215
        获取所有邮费模板
216
217
        :param template_id: 邮费模板ID
218
        :return: 返回的 JSON 数据包
219
        """
220
        return self._get(
221
            'merchant/express/getall'
222
        )
223
224 10
    def add_group(self, group_detail):
225
        """
226
        增加分组
227
228
        :param group_detail: 商品分组信息
229
        :return: 返回的 JSON 数据包
230
        """
231
        return self._post(
232
            'merchant/group/add',
233
            data=group_detail
234
        )
235
236 10
    def del_group(self, group_id):
237
        """
238
        删除分组
239
240
        :param group_id: 商品分组ID
241
        :return: 返回的 JSON 数据包
242
        """
243
        return self._post(
244
            'merchant/group/del',
245
            data={
246
                'group_id': group_id
247
            }
248
        )
249
250 10
    def update_group_property(self, group_id, group_properties):
251
        """
252
        修改分组属性
253
254
        :param group_id: 商品分组ID
255
        :param group_properties: 商品分组属性
256
        :return: 返回的 JSON 数据包
257
        """
258
        group_properties['group_id'] = group_id
259
        return self._post(
260
            'merchant/group/propertymod',
261
            data=group_properties
262
        )
263
264 10
    def update_group_product(self, group_id, product_data):
265
        """
266
        修改分组商品
267
268
        :param group_id: 商品分组ID
269
        :param product_data: 分组商品信息
270
        :return: 返回的 JSON 数据包
271
        """
272
        product_data['group_id'] = group_id
273
        return self._post(
274
            'merchant/group/productmod',
275
            data=product_data
276
        )
277
278 10
    def get_all_groups(self):
279
        """
280
        获取所有分组
281
282
        :return: 返回的 JSON 数据包
283
        """
284
        return self._get(
285
            'merchant/group/getall'
286
        )
287
288 10
    def get_group(self, group_id):
289
        """
290
        根据分组ID获取分组信息
291
292
        :param group_id: 商品分组ID
293
        :return: 返回的 JSON 数据包
294
        """
295
        return self._post(
296
            'merchant/group/getbyid',
297
            data={
298
                'group_id': group_id
299
            }
300
        )
301
302 10
    def add_shelf(self, shelf_data):
303
        """
304
        增加货架
305
306
        :param shelf_data: 货架详情信息
307
        :return: 返回的 JSON 数据包
308
        """
309
        return self._post(
310
            'merchant/shelf/add',
311
            data=shelf_data
312
        )
313
314 10
    def del_shelf(self, shelf_id):
315
        """
316
        删除货架
317
318
        :param shelf_id: 货架ID
319
        :return: 返回的 JSON 数据包
320
        """
321
        return self._post(
322
            'merchant/shelf/del',
323
            data={
324
                'shelf_id': shelf_id
325
            }
326
        )
327
328 10
    def update_shelf(self, shelf_id, shelf_data):
329
        """
330
        修改货架
331
332
        :param shelf_id: 货架ID
333
        :param shelf_data: 货架详情
334
        :return: 返回的 JSON 数据包
335
        """
336
        shelf_data['shelf_id'] = shelf_id
337
        return self._post(
338
            'merchant/shelf/mod',
339
            data=shelf_data
340
        )
341
342 10
    def get_all_shelves(self):
343
        """
344
        获取所有货架
345
346
        :return: 返回的 JSON 数据包
347
        """
348
        return self._get(
349
            'merchant/shelf/getall'
350
        )
351
352 10
    def get_shelf(self, shelf_id):
353
        """
354
        根据货架ID获取货架信息
355
356
        :param shelf_id: 货架ID
357
        :return: 返回的 JSON 数据包
358
        """
359
        return self._post(
360
            'merchant/shelf/getbyid',
361
            data={
362
                'shelf_id': shelf_id
363
            }
364
        )
365
366 10
    def get_order(self, order_id):
367
        """
368
        根据订单ID获取订单详情
369
370
        :param order_id: 订单ID
371
        :return: 返回的 JSON 数据包
372
        """
373
        return self._post(
374
            'merchant/order/getbyid',
375
            data={
376
                'order_id': order_id
377
            }
378
        )
379
380 10
    def query_order(self, status=None, begintime=None, endtime=None):
381
        """
382
        根据订单状态/创建时间获取订单详情
383
384
        :param status: 订单状态(不带该字段-全部状态, 2-待发货, 3-已发货, 5-已完成, 8-维权中, )
385
        :param begintime: 订单创建时间起始时间(不带该字段则不按照时间做筛选)
386
        :param endtime: 订单创建时间终止时间(不带该字段则不按照时间做筛选)
387
        :return: 返回的 JSON 数据包
388
        """
389
        return self._post(
390
            'merchant/order/getbyfilter',
391
            data={
392
                'status': status,
393
                'begintime': begintime,
394
                'endtime': endtime
395
            }
396
        )
397
398 10
    def set_delivery(self, order_id, delivery_data):
399
        """
400
        修改货架
401
402
        :param order_id: 订单ID
403
        :param delivery_data: 商品物流信息
404
        :return: 返回的 JSON 数据包
405
        """
406
        delivery_data['order_id'] = order_id
407
        return self._post(
408
            'merchant/shelf/setdeliverymod',
409
            data=delivery_data
410
        )
411
412 10
    def upload_image(self, media_file):
413
        """
414
        上传图片
415
416
        :param media_file: 要上传的文件,一个 File-object
417
        :return: 上传成功时返回图片 URL
418
        """
419
        res = self._post(
420
            url='merchant/common/upload_img',
421
            files={
422
                'media': media_file
423
            },
424
            result_processor=lambda x: x['url']
425
        )
426
        return res
427