Skip to content

Commit

Permalink
Use optionaldict instead of the ugly NotNoneDict utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed May 9, 2015
1 parent 132913c commit 90d2c40
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 45 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
six>=1.8.0
requests>=2.4.3
xmltodict>=0.9.0
optionaldict>=0.1.0
21 changes: 12 additions & 9 deletions wechatpy/client/api/customservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import time
import datetime

from wechatpy.utils import to_binary, NotNoneDict
from optionaldict import optionaldict
from wechatpy.utils import to_binary
from wechatpy.client.api.base import BaseWeChatAPI


Expand Down Expand Up @@ -129,10 +130,11 @@ def create_session(self, openid, account, text=None):
:param text: 附加信息,可选
:return: 返回的 JSON 数据包
"""
data = NotNoneDict()
data['openid'] = openid
data['kf_account'] = account
data['text'] = text
data = optionaldict(
openid=openid,
kf_account=account,
text=text
)
return self._post(
'customservice/kfsession/create',
data=data
Expand All @@ -149,10 +151,11 @@ def close_session(self, openid, account, text=None):
:param text: 附加信息,可选
:return: 返回的 JSON 数据包
"""
data = NotNoneDict()
data['openid'] = openid
data['kf_account'] = account
data['text'] = text
data = optionaldict(
openid=openid,
kf_account=account,
text=text
)
return self._post(
'customservice/kfsession/close',
data=data
Expand Down
11 changes: 6 additions & 5 deletions wechatpy/client/api/merchant/order.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from optionaldict import optionaldict
from wechatpy.client.api.base import BaseWeChatAPI
from wechatpy.utils import NotNoneDict


class MerchantOrder(BaseWeChatAPI):
Expand All @@ -16,10 +16,11 @@ def get(self, order_id):
return res['order']

def get_by_filter(self, status=None, begin_time=None, end_time=None):
filter_dict = NotNoneDict()
filter_dict['status'] = status
filter_dict['begintime'] = begin_time
filter_dict['endtime'] = end_time
filter_dict = optionaldict(
status=status,
begintime=begin_time,
endtime=end_time
)

res = self._post(
'merchant/order/getbyfilter',
Expand Down
4 changes: 2 additions & 2 deletions wechatpy/client/api/semantic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from wechatpy.utils import NotNoneDict
from optionaldict import optionaldict

from wechatpy.client.api.base import BaseWeChatAPI

Expand Down Expand Up @@ -32,7 +32,7 @@ def search(self,
if isinstance(category, (tuple, list)):
category = ','.join(category)

data = NotNoneDict()
data = optionaldict()
data['query'] = query
data['category'] = category
data['uid'] = uid
Expand Down
14 changes: 7 additions & 7 deletions wechatpy/client/api/shakearound.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from datetime import datetime

import six
from optionaldict import optionaldict

from wechatpy.utils import NotNoneDict
from wechatpy.client.api.base import BaseWeChatAPI


Expand Down Expand Up @@ -34,7 +34,7 @@ def apply_device_id(self, quantity, reason, poi_id=None, comment=None):
:param comment: 可选,备注,不超过15个汉字或30个英文字母
:return: 申请的设备信息
"""
data = NotNoneDict()
data = optionaldict()
data['quantity'] = quantity
data['apply_reason'] = reason
data['poi_id'] = poi_id
Expand All @@ -59,7 +59,7 @@ def update_device(self, device_id=None, uuid=None, major=None,
:param comment: 设备的备注信息,不超过15个汉字或30个英文字母。
:return: 返回的 JSON 数据包
"""
data = NotNoneDict()
data = optionaldict()
data['comment'] = comment
data['device_identifier'] = {
'device_id': device_id,
Expand All @@ -86,7 +86,7 @@ def bind_device_location(self, poi_id, device_id=None, uuid=None,
:param minor: minor
:return: 返回的 JSON 数据包
"""
data = NotNoneDict()
data = optionaldict()
data['poi_id'] = poi_id
data['device_identifier'] = {
'device_id': device_id,
Expand All @@ -112,7 +112,7 @@ def search_device(self, identifiers=None, apply_id=None,
:param count: 待查询的设备个数
:return: 设备列表
"""
data = NotNoneDict()
data = optionaldict()
data['begin'] = begin
data['count'] = count
data['apply_id'] = apply_id
Expand All @@ -137,7 +137,7 @@ def add_page(self, title, description, icon_url, comment=None):
:param comment: 可选,页面的备注信息,不超过15个字
:return: 页面信息
"""
data = NotNoneDict()
data = optionaldict()
data['title'] = title
data['description'] = description
data['icon_url'] = icon_url
Expand All @@ -164,7 +164,7 @@ def update_page(self, page_id, title, description,
:param comment: 可选,页面的备注信息,不超过15个字
:return: 页面信息
"""
data = NotNoneDict()
data = optionaldict()
data['page_id'] = page_id
data['title'] = title
data['description'] = description
Expand Down
5 changes: 3 additions & 2 deletions wechatpy/enterprise/client/api/agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from optionaldict import optionaldict

from wechatpy.client.api.base import BaseWeChatAPI
from wechatpy.utils import NotNoneDict


class WeChatAgent(BaseWeChatAPI):
Expand Down Expand Up @@ -44,7 +45,7 @@ def set(self,
:param is_report_enter: 是否上报用户进入应用事件
:return: 返回的 JSON 数据包
"""
agent_data = NotNoneDict()
agent_data = optionaldict()
agent_data['agentid'] = agent_id
agent_data['name'] = name
agent_data['description'] = description
Expand Down
6 changes: 4 additions & 2 deletions wechatpy/enterprise/client/api/batch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from optionaldict import optionaldict

from wechatpy.client.api.base import BaseWeChatAPI
from wechatpy.utils import NotNoneDict, to_text
from wechatpy.utils import to_text


class WeChatBatch(BaseWeChatAPI):
Expand All @@ -22,7 +24,7 @@ def invite_user(self, url, token, encoding_aes_key, user_ids=None,
:param invite_tips: 可选,推送到微信上的提示语
:return: 返回的 JSON 数据包
"""
data = NotNoneDict()
data = optionaldict()
data['callback'] = {
'url': url,
'token': token,
Expand Down
7 changes: 4 additions & 3 deletions wechatpy/enterprise/client/api/department.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from optionaldict import optionaldict

from wechatpy.client.api.base import BaseWeChatAPI
from wechatpy.utils import NotNoneDict


class WeChatDepartment(BaseWeChatAPI):
Expand All @@ -15,7 +16,7 @@ def create(self, name, parent_id=1, order=None, id=None):
:param parent_id: 父亲部门 id ,根部门 id 为 1
:return: 返回的 JSON 数据包
"""
department_data = NotNoneDict()
department_data = optionaldict()
department_data['name'] = name
department_data['parentid'] = parent_id
department_data['order'] = order
Expand All @@ -36,7 +37,7 @@ def update(self, id, name=None, parent_id=None, order=None):
:param order: 在父部门中的次序,从 1 开始,数字越大排序越靠后
:return: 返回的 JSON 数据包
"""
department_data = NotNoneDict()
department_data = optionaldict()
department_data['id'] = id
department_data['name'] = name
department_data['parentid'] = parent_id
Expand Down
5 changes: 3 additions & 2 deletions wechatpy/enterprise/client/api/message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from wechatpy.utils import NotNoneDict
from optionaldict import optionaldict

from wechatpy.client.api.base import BaseWeChatAPI


Expand Down Expand Up @@ -76,7 +77,7 @@ def send_voice(self, agent_id, user_ids, media_id,

def send_video(self, agent_id, user_ids, media_id, title=None,
description=None, party_ids='', tag_ids='', safe=0):
video_data = NotNoneDict()
video_data = optionaldict()
video_data['media_id'] = media_id
video_data['title'] = title
video_data['description'] = description
Expand Down
9 changes: 5 additions & 4 deletions wechatpy/enterprise/client/api/user.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
from optionaldict import optionaldict

from wechatpy.client.api.base import BaseWeChatAPI
from wechatpy.utils import NotNoneDict


class WeChatUser(BaseWeChatAPI):
Expand All @@ -13,7 +14,7 @@ def create(self, user_id, name, department=None, position=None,
创建成员
详情请参考 http://qydev.weixin.qq.com/wiki/index.php?title=管理成员
"""
user_data = NotNoneDict()
user_data = optionaldict()
user_data['userid'] = user_id
user_data['name'] = name
user_data['gender'] = gender
Expand All @@ -37,7 +38,7 @@ def update(self, user_id, name=None, department=None, position=None,
更新成员
详情请参考 http://qydev.weixin.qq.com/wiki/index.php?title=管理成员
"""
user_data = NotNoneDict()
user_data = optionaldict()
user_data['userid'] = user_id
user_data['name'] = name
user_data['gender'] = gender
Expand Down Expand Up @@ -101,7 +102,7 @@ def invite(self, user_id, tips=None):
邀请成员关注
详情请参考 http://qydev.weixin.qq.com/wiki/index.php?title=管理成员
"""
data = NotNoneDict()
data = optionaldict()
data['userid'] = user_id
data['invite_tips'] = tips
return self._post(
Expand Down
9 changes: 0 additions & 9 deletions wechatpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ def __setattr__(self, key, value):
self[key] = value


class NotNoneDict(dict):
"""A dictionary only store non none values"""

def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
if value is None:
return
return dict_setitem(self, key, value)


class WeChatSigner(object):
"""WeChat data signer"""

Expand Down

0 comments on commit 90d2c40

Please sign in to comment.