<s> import json <EOL> import time <EOL> import requests <EOL> from pywechat . excepts import WechatError <EOL> class Basic ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , app_id , app_secret ) : <EOL> """<STR_LIT>""" <EOL> self . __app_id = app_id <EOL> self . __app_secret = app_secret <EOL> self . __access_token = self . access_token <EOL> self . __token_expires_at = None <EOL> @ property <EOL> def access_token ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . __access_token and self . __token_expires_at : <EOL> if self . __token_expires_at - time . time ( ) > <NUM_LIT> : <EOL> return self . __access_token <EOL> self . _grant_access_token ( ) <EOL> return self . __access_token <EOL> def _send_request ( self , method , url , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not kwargs . get ( '<STR_LIT>' ) : <EOL> kwargs [ '<STR_LIT>' ] = { <EOL> "<STR_LIT>" : self . access_token <EOL> } <EOL> if kwargs . get ( '<STR_LIT:data>' ) : <EOL> data = json . dumps ( kwargs [ '<STR_LIT:data>' ] ) . encode ( '<STR_LIT:utf-8>' ) <EOL> kwargs [ "<STR_LIT:data>" ] = data <EOL> request = requests . request ( <EOL> method = method , <EOL> url = url , <EOL> ** kwargs <EOL> ) <EOL> request . raise_for_status ( ) <EOL> json_data = request . json ( ) <EOL> self . _check_wechat_error ( json_data ) <EOL> return json_data <EOL> @ classmethod <EOL> def _check_wechat_error ( cls , json_data ) : <EOL> """<STR_LIT>""" <EOL> errcode = json_data . get ( '<STR_LIT>' ) <EOL> if errcode and errcode != <NUM_LIT:0> : <EOL> raise WechatError ( errcode , json_data . get ( '<STR_LIT>' ) ) <EOL> def _grant_access_token ( self ) : <EOL> """<STR_LIT>""" <EOL> url = '<STR_LIT>' <EOL> params = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : self . __app_id , <EOL> "<STR_LIT>" : self . __app_secret <EOL> } <EOL> json_data = self . _send_request ( '<STR_LIT>' , url , params = params ) <EOL> self . __access_token = json_data . get ( '<STR_LIT>' ) <EOL> self . __token_expires_at = int ( <EOL> time . time ( ) ) + json_data . get ( '<STR_LIT>' ) <EOL> return json_data <EOL> def _get_wechat_server_ips ( self ) : <EOL> """<STR_LIT>""" <EOL> url = "<STR_LIT>" <EOL> params = { <EOL> "<STR_LIT>" : self . access_token <EOL> } <EOL> json_data = self . _send_request ( '<STR_LIT>' , url , params = params ) <EOL> return json_data </s>
