apidoc用于写http restful接口文档注意事项

来源:互联网 发布:ubuntu默认视频播放器 编辑:程序博客网 时间:2024/06/03 05:23
由于postman被墙了,用apidoc来写restful接口文档,并附上test case,感觉很不错。
在使用中,这个apidoc跟postman相比,还是有几点区别:
一、restful风格的url,最后不能是'/'结尾。
比如,消息列表的url,应该是:
http://127.0.0.1:8000/api/messages,GET
不能写成http://127.0.0.1:8000/api/messages/,GET
二、query参数,要支持如下风格:
http://127.0.0.1:8000/api/messages/list?page_index=&page_size=&is_read=&notification_type=&set_read_flag=&warehouse_id=&message_status=&box_type=
这就是说,在实现url方法时,如果是传进来空值,就要自行添加一个default值。


page_index = param_utils.get_param_by_request(request.GET, "page_index", 1, int)
def get_param_by_request(params, param_name, default_val=None, _type=None):
    try:
        if param_name in params:
            _val = params[param_name]
        else:
            _val = default_val
        if _type:
            _val = _type(_val)
        return _val
    except Exception as e:
        return default_val
同时再说一个bug。
bug现象:
ValueError: invalid literal for int() with base 10: ''
这个话的意思是,对int变量赋值时出错,就是下面这段代码:
record.copy_status_ori = int(kwargs['status_ori'])
因为这个字段在model中定义为:IntegerField
copy_status_ori = models.IntegerField(choices=ORIGINAL_ORDER_STATUS,
                                          verbose_name='订单原始状态(copy)',
                                          null=True)
 if 'status_ori' in kwargs:
  # print('status_ori=', kwargs['status_ori'])
    if kwargs['status_ori'] is not None and isinstance(kwargs['status_ori'], int):
       record.copy_status_ori = int(kwargs['status_ori'])

       record.copy_status_ori_value = find_name(int(kwargs['status_ori']), ORIGINAL_ORDER_STATUS)