2016-09-30 68 views
0

我试图了解从这个doc蟒蛇装饰。并正在编写我自己的装饰器来处理API异常。访问参数

但是在我的装饰器中,我没有得到如何在自定义装饰器中访问参数(method,apidata)。

我知道,我没有通过任何地方method因为我没有得到该到哪里,这样我可以在我的装饰接受它。

这里是我的装饰:

import requests as r 
import json 
def handle_api_exceptions(http_api_call): 
    """ """ 

    def wrapper(*args, **kwargs): 
     """ """ 
     response = {} 
     try: 
      response = http_api_call(*args, **kwargs) 
      if response.ok: 
       result = response.json() 
       if result.get('code', 200) in INVALID_STATUS_CODES: #INVALID_STATUS_CODES = [1801, 1803, 1806,... ] 
        response = {"data":{}, "status":False} 
       else: 
        return result 
      else: 
       capture_failed_requests(method, api, data, response, 
            error=None, reason=response.reason) 
       return {} 
     except r.exceptions.ConnectionError as e: 
      capture_failed_requests(method, api, data, response, error=e, 
             reason="Connection Aborted") 
      return {} 
     except json.decoder.JSONDecodeError as e: 
      capture_failed_requests(method, api, data, response, error=e, 
             reason="Invalid Response") 
      return {} 
     except r.exceptions.ReadTimeout as e: 
      capture_failed_requests(method, api, data, response, error=e, 
             reason="Request Timed Out") 
      return {} 
     except Exception as e: 
      capture_failed_requests(method, api, data, response, error=e, 
             reason="Internal Server Error") 
     return {} 
    return wrapper 

定制GET,POST API请求:

@handle_api_exceptions 
def get(self, api, data, token=None): 
    """ """ 
    if token:data.update(self.get_token(token)) 
    response = r.get(api, data, verify=self.config.SSL_VERIFY, 
         timeout=self.config.REQUEST_TIMEOUT) 
    return response 

@handle_api_exceptions 
def post(self, api, data, token=None): 
    """ """ 
    if token: 
     data.update(self.get_secret_token(token)) 
    response = r.post(api, data, verify=self.config.SSL_VERIFY, 
         timeout=self.config.REQUEST_TIMEOUT) 
    return response   


def post_call(self): 
    """ """ 
    api = "http://192.168.0.24/api/v1/reset/" 
    data = {"k1":[], "k2":{}, "id":123456} #-- Some Key val 
    return self.post(api, data, token="SOME_SECRET_TOKEN") 

查询是:如何通过methodapidatacapture_failed_requests()

+0

你不可错过'method'按照目前编写的,但'data'和'api'只是在'args'。 – jonrsharpe

+0

@jonrsharpe:我可以从某处传递硬编码方法=“GET”或method =“POST”吗? – Laxmikant

+0

是的,你可以传递给装饰直接:'@handle_api_exceptions(方法= 'GET')'。请记住:http://stackoverflow.com/q/5929107/3001761 – jonrsharpe

回答

2

data and api are in args inside wrappermethod您必须单独提供,例如通过参数化修饰器(参见例如python decorators with parameters)。你可以这样做,如下:

def handle_api_exceptions(method): 
    def decorator(http_api_call): 
     def wrapper(api, data, *args, **kwargs): 
      ... 

和你的装饰将成为例如

@handle_api_exceptions(method='GET') 
def get(self, api, data, token=None): 
    ... 

或者,你可以使用method = http_api_call.__name__(这会给你'get'),并避免方法名的嵌套和重复额外的一层。


请注意,我已经删除了空文档字符串 - 无论是写一个文档字符串实际(我喜欢Google-style,但情况因人而异),或者没有一个在所有。如果你掉毛规则需要文档字符串,这是因为谁设置它要你写有用的,不只是它欺骗。

+0

谢谢,我现在明白了。还有一个谷歌风格建议+1,赞赏:) – Laxmikant