Skip to content

Commit

Permalink
Update enterprise batch APIs. Close #33
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Apr 14, 2015
1 parent 4ff9fce commit 2e37e81
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 8 deletions.
10 changes: 10 additions & 0 deletions tests/fixtures/enterprise/batch_getresult.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"errcode": 0,
"errmsg": "ok",
"status": 1,
"type": "replace_user",
"total": 3,
"percentage": 33,
"remaintime": 1,
"result": [{},{}]
}
5 changes: 5 additions & 0 deletions tests/fixtures/enterprise/batch_inviteuser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errcode":"0",
"errmsg":"ok",
"jobid": "IoZW03y44Zcwuz-2K6T6rHTcf1uwyVbcYu2aRALKw-U"
}
5 changes: 5 additions & 0 deletions tests/fixtures/enterprise/batch_replaceparty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errcode":"0",
"errmsg":"ok",
"jobid": "IoZW03y44Zcwuz-2K6T6rHTcf1uwyVbcYu2aRALKw-U"
}
5 changes: 5 additions & 0 deletions tests/fixtures/enterprise/batch_replaceuser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errcode":"0",
"errmsg":"ok",
"jobid": "IoZW03y44Zcwuz-2K6T6rHTcf1uwyVbcYu2aRALKw-U"
}
5 changes: 5 additions & 0 deletions tests/fixtures/enterprise/batch_syncuser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"errcode":"0",
"errmsg":"ok",
"jobid": "IoZW03y44Zcwuz-2K6T6rHTcf1uwyVbcYu2aRALKw-U"
}
49 changes: 49 additions & 0 deletions tests/test_enterprise_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,52 @@ def test_tag_list(self):
with HTTMock(wechat_api_mock):
res = self.client.tag.list()
self.assertEqual(2, len(res))

def test_batch_invite_user(self):
with HTTMock(wechat_api_mock):
res = self.client.batch.invite_user(
'http://example.com',
'123456',
'123456',
'123|456',
[123, 456],
(12, 34),
''
)
self.assertEqual(0, res['errcode'])

def test_batch_sync_user(self):
with HTTMock(wechat_api_mock):
res = self.client.batch.sync_user(
'http://example.com',
'123456',
'123456',
'12345678'
)
self.assertEqual(0, res['errcode'])

def test_batch_replace_user(self):
with HTTMock(wechat_api_mock):
res = self.client.batch.replace_user(
'http://example.com',
'123456',
'123456',
'12345678'
)
self.assertEqual(0, res['errcode'])

def test_batch_replace_party(self):
with HTTMock(wechat_api_mock):
res = self.client.batch.replace_party(
'http://example.com',
'123456',
'123456',
'12345678'
)
self.assertEqual(0, res['errcode'])

def test_batch_get_result(self):
with HTTMock(wechat_api_mock):
res = self.client.batch.get_result('123456')
self.assertEqual(0, res['errcode'])
self.assertEqual(1, res['status'])
2 changes: 2 additions & 0 deletions wechatpy/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def _request(self, method, url_or_endpoint, **kwargs):
)
res.raise_for_status()
result = res.json()
if 'errcode' in result:
result['errcode'] = int(result['errcode'])

if 'errcode' in result and result['errcode'] != 0:
errcode = result['errcode']
Expand Down
1 change: 1 addition & 0 deletions wechatpy/enterprise/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, corp_id, secret, access_token=None):
self.media = api.WeChatMedia(weak_self)
self.misc = api.WeChatMisc(weak_self)
self.agent = api.WeChatAgent(weak_self)
self.batch = api.WeChatBatch(weak_self)

def fetch_access_token(self):
""" Fetch access token"""
Expand Down
1 change: 1 addition & 0 deletions wechatpy/enterprise/client/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
from .user import WeChatUser # NOQA
from .misc import WeChatMisc # NOQA
from .agent import WeChatAgent # NOQA
from .batch import WeChatBatch # NOQA
114 changes: 106 additions & 8 deletions wechatpy/enterprise/client/api/batch.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,119 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from wechatpy.client.api.base import BaseWeChatAPI
from wechatpy.utils import NotNoneDict, to_text


class WeChatBatch(BaseWeChatAPI):

def invite_user(self):
pass
def invite_user(self, url, token, encoding_aes_key, user_ids=None,
party_ids=None, tag_ids=None, invite_tips=None):
"""
邀请成员关注
详情请参考
http://qydev.weixin.qq.com/wiki/index.php?title=异步任务接口
:param url: 企业应用接收企业号推送请求的访问协议和地址,支持http或https协议
:param token: 用于生成签名
:param encoding_aes_key: 用于消息体的加密,是AES密钥的Base64编码
:param user_ids: 可选,成员ID列表,多个接收者用‘|’分隔,最多支持1000个。
:param party_ids: 可选,部门ID列表,多个接收者用‘|’分隔,最多支持100个。
:param tag_ids: 可选,标签ID列表,多个接收者用‘|’分隔。
:param invite_tips: 可选,推送到微信上的提示语
:return: 返回的 JSON 数据包
"""
data = NotNoneDict()
data['callback'] = {
'url': url,
'token': token,
'encodingaeskey': encoding_aes_key
}
if isinstance(user_ids, (tuple, list)):
user_ids = '|'.join(map(to_text, user_ids))
if isinstance(party_ids, (tuple, list)):
party_ids = '|'.join(map(to_text, party_ids))
if isinstance(tag_ids, (tuple, list)):
tag_ids = '|'.join(map(to_text, tag_ids))
data['touser'] = user_ids
data['toparty'] = party_ids
data['totag'] = tag_ids
data['invite_tips'] = invite_tips
return self._post(
'batch/inviteuser',
data=data
)

def sync_user(self):
pass
def sync_user(self, url, token, encoding_aes_key, media_id):
"""
增量更新成员
详情请参考
http://qydev.weixin.qq.com/wiki/index.php?title=异步任务接口
def replace_user(self):
pass
:param url: 企业应用接收企业号推送请求的访问协议和地址,支持http或https协议
:param token: 用于生成签名
:param encoding_aes_key: 用于消息体的加密,是AES密钥的Base64编码
:param media_id: 上传的csv文件的media_id
:return: 返回的 JSON 数据包
"""
return self._post(
'batch/syncuser',
data={
'media_id': media_id,
'callback': {
'url': url,
'token': token,
'encodingaeskey': encoding_aes_key
}
}
)

def replace_party(self):
pass
def replace_user(self, url, token, encoding_aes_key, media_id):
"""
全量覆盖成员
详情请参考
http://qydev.weixin.qq.com/wiki/index.php?title=异步任务接口
:param url: 企业应用接收企业号推送请求的访问协议和地址,支持http或https协议
:param token: 用于生成签名
:param encoding_aes_key: 用于消息体的加密,是AES密钥的Base64编码
:param media_id: 上传的csv文件的media_id
:return: 返回的 JSON 数据包
"""
return self._post(
'batch/replaceuser',
data={
'media_id': media_id,
'callback': {
'url': url,
'token': token,
'encodingaeskey': encoding_aes_key
}
}
)

def replace_party(self, url, token, encoding_aes_key, media_id):
"""
全量覆盖部门
详情请参考
http://qydev.weixin.qq.com/wiki/index.php?title=异步任务接口
:param url: 企业应用接收企业号推送请求的访问协议和地址,支持http或https协议
:param token: 用于生成签名
:param encoding_aes_key: 用于消息体的加密,是AES密钥的Base64编码
:param media_id: 上传的csv文件的media_id
:return: 返回的 JSON 数据包
"""
return self._post(
'batch/replaceparty',
data={
'media_id': media_id,
'callback': {
'url': url,
'token': token,
'encodingaeskey': encoding_aes_key
}
}
)

def get_result(self, job_id):
"""
Expand Down

0 comments on commit 2e37e81

Please sign in to comment.