编写此Python函数的更好方法是使用If条件

编写此Python函数的更好方法是使用If条件,python,if-statement,python-requests,Python,If Statement,Python Requests,使用Python2.7和requests模块,我有一个函数,我与其他几个函数一起调用它来进行api调用。但是,我必须为一个特定的函数做一个例外,并返回请求对象……如果work\u around\u for\u image\u custom\u list为真。这看起来像是一个丑陋的破解/变通,我想知道如何重新编写它来补偿如果变通为图片定制列表。例如,将其设为类并让每个函数创建一个对象来使用它是否更好?如果是这样,如果r.status\u code==200,我将如何超越?扩展我的评论: def d

使用Python2.7和requests模块,我有一个函数,我与其他几个函数一起调用它来进行api调用。但是,我必须为一个特定的函数做一个例外,并返回请求对象……如果
work\u around\u for\u image\u custom\u list
为真。这看起来像是一个丑陋的破解/变通,我想知道如何重新编写它来补偿如果变通为图片定制列表。例如,将其设为类并让每个函数创建一个对象来使用它是否更好?如果是这样,如果r.status\u code==200,我将如何超越

扩展我的评论:

def do_request(url, token, json_data=None,
               mode="get", work_around_for_image_custom_list=False):

    """Uploads a file. """
    header_collection = {"X-Auth-Token": token}
    if json_data is not None:
        header_collection['Content-Type'] = 'application/json'
    try:
        if mode == "delete":
            # this looks ugly, but there is absolutely no way to
            # get requests to do DELETE when there is a blank JSON
            # included
            r = requests.delete(url, headers=header_collection, timeout=10)
        else:
            r = getattr(requests, mode)(url, data=json.dumps(json_data),
                                        headers=header_collection, timeout=10)
        if r.status_code == 200:
            #This looks ugly also, but has to be for a particular function that calls it
            if work_around_for_image_custom_list:
                return r
            else:
            http_info = (json.dumps(r.json(), indent=2), r.status_code)
        else:
            http_info = (r.text, r.status_code)
        return http_info
    except requests.exceptions.ConnectionError:
        print "Connection Error! Http status Code {}".format(r.status_code)
        sys.exit()
    except (requests.exceptions.RequestException,
            requests.exceptions.HTTPError):
        print "Ambiguous Error! Http status Code {}".format(r.status_code)
        sys.exit()
然后:

现在您可以根据需要调用
do_raw_request
do_request

请注意,我更改了返回值,因此它总是返回一个元组,否则您必须开始检查类型,以了解您是否有状态文本或响应对象。

为什么不使用上述函数,除非它总是返回
r
和另一个使用第一个函数生成
http\u info
结果的函数基于
r
它从第一个得到。然后调用任何相关的函数。我考虑过这样做,但是调用它的函数都会有重复的json.dumps代码(r.json(),indent=2)
def do_raw_request(url, token, json_data=None, mode="get"):
    """Uploads a file. """
    header_collection = {"X-Auth-Token": token}
    if json_data is not None:
        header_collection['Content-Type'] = 'application/json'
    try:
        if mode == "delete":
            # this looks ugly, but there is absolutely no way to
            # get requests to do DELETE when there is a blank JSON
            # included
            r = requests.delete(url, headers=header_collection, timeout=10)
        else:
            r = getattr(requests, mode)(url, data=json.dumps(json_data),
                                        headers=header_collection, timeout=10)

        if r.status_code == 200:
                return r, r.status_code
        return r.text, r.status_code

    except requests.exceptions.ConnectionError:
        print "Connection Error! Http status Code {}".format(r.status_code)
        sys.exit()
    except (requests.exceptions.RequestException,
            requests.exceptions.HTTPError):
        print "Ambiguous Error! Http status Code {}".format(r.status_code)
        sys.exit()
def do_request(url, token, json_data=None, mode="get"):
    res, code = do_raw_request(url, token, json_data, mode)
    if code == 200:
       return (json.dumps(r.json(), indent=2), r.status_code)
    return res, code