Skip to content

Commit

Permalink
Update APIDescriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed May 13, 2015
1 parent 9e075b1 commit 8c5172b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
5 changes: 5 additions & 0 deletions wechatpy/client/api/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import weakref


class APIDescriptor(object):
Expand All @@ -8,6 +9,9 @@ def __init__(self, api):
self.api = api

def __get__(self, instance, instance_type=None):
if instance is not None:
if self.api._client is None:
self.api._client = weakref.proxy(instance)
return self.api


Expand All @@ -32,3 +36,4 @@ def access_token(self):

def add_to_class(self, klass, name):
klass._api_endpoints[name] = self
setattr(klass, name, APIDescriptor(self))
10 changes: 1 addition & 9 deletions wechatpy/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from __future__ import absolute_import, unicode_literals
import time
import copy
import weakref

import six
import requests
Expand All @@ -23,7 +22,7 @@ def __new__(cls, name, bases, attrs):
if k in attrs:
continue
if isinstance(v, APIDescriptor):
attrs[k] = copy.deepcopy(v.api)
attrs[k] = copy.deepcopy(v)

cls = super(_ClientMetaClass, cls).__new__(cls, name, bases, attrs)
cls._api_endpoints = {}
Expand All @@ -42,13 +41,6 @@ def __init__(self, access_token=None):
self._access_token = access_token
self.expires_at = None

weak_self = weakref.proxy(self)
for name, api in self._api_endpoints.items():
if not isinstance(api, BaseWeChatAPI):
continue
api._client = weak_self
setattr(self, name, api)

def _request(self, method, url_or_endpoint, **kwargs):
if not url_or_endpoint.startswith(('http://', 'https://')):
api_base_url = kwargs.pop('api_base_url', self.API_BASE_URL)
Expand Down

0 comments on commit 8c5172b

Please sign in to comment.