首页 > 其他分享 >通用的API返回类

通用的API返回类

时间:2023-12-07 14:15:26浏览次数:34  
标签:返回 Status 通用 ApiErrorCode Success ApiResponse ApiResponseStatus API public

 

public enum ApiResponseStatus
{
    Success,
    Error
}

public enum ApiErrorCode
{
    InvalidRequest = 1000,
    Forbidden = 2000,
    NotFound = 3000,
    InternalServerError = 4000
}

public class ApiResponse<T>
{
    public ApiResponseStatus Status { get; set; }
    public string Message { get; set; }
    public T Data { get; set; }
    public ApiErrorCode ErrorCode { get; set; }

    public ApiResponse()
    {
        Status = ApiResponseStatus.Success;
        Message = "Success";
    }

    public ApiResponse(T data)
    {
        Status = ApiResponseStatus.Success;
        Message = "Success";
        Data = data;
    }

    public ApiResponse(string message, ApiErrorCode errorCode = ApiErrorCode.InternalServerError)
    {
        Status = ApiResponseStatus.Error;
        Message = message;
        ErrorCode = errorCode;
    }

    public void SetError(string message, ApiErrorCode errorCode = ApiErrorCode.InternalServerError)
    {
        Status = ApiResponseStatus.Error;
        Message = message;
        ErrorCode = errorCode;
    }

    public void SetData(T data)
    {
        Status = ApiResponseStatus.Success;
        Message = "Success";
        Data = data;
    }

    public static ApiResponse<T> CreateError(string message, ApiErrorCode errorCode = ApiErrorCode.InternalServerError)
    {
        return new ApiResponse<T>
        {
            Status = ApiResponseStatus.Error,
            Message = message,
            ErrorCode = errorCode
        };
    }

    public static ApiResponse<T> CreateSuccess(T data)
    {
        return new ApiResponse<T>
        {
            Status = ApiResponseStatus.Success,
            Message = "Success",
            Data = data
        };
    }
}

 

标签:返回,Status,通用,ApiErrorCode,Success,ApiResponse,ApiResponseStatus,API,public
From: https://www.cnblogs.com/ZkbFighting/p/17881850.html

相关文章

  • 淘宝详情API接口在电商行业中的重要性及实时数据获取实现
    一、引言随着电子商务的快速发展,电商平台上的商品数量呈现爆炸性增长。为了满足用户的需求,提供丰富、多样的商品信息,淘宝等电商平台推出了详情API接口。本文旨在探讨淘宝详情API接口在电商行业中的重要性,以及如何实现实时数据获取。二、淘宝详情API接口的重要性商品信息丰富化:通过......
  • .net core DataTable.Load()方法,返回的行缺少,少于reader读出的行
     我分析的原因是,datatable模式的schema默认是根据查询的sql来的。起因是我写的sql中带有主键的列,查出来有很多重复值,然后dt.load会默认把主键重复的行给合并掉,所以最终查询出来的都是主键不重复的数据行,重复的行都被合并掉了,所以就缺失了。另外,dt.load有个重载版本,我的理解是......
  • Using Redis with FastAPI
    UsingRediswithFastAPIhttps://developer.redis.com/develop/python/fastapi/https://github.com/fanqingsong/fastapi-redis-tutorialFastAPIisaPythonwebframeworkbasedontheStarlettemicroframework.Withdeepsupportforasyncio,FastAPIisindeedv......
  • 构建一个语音转文字的WebApi服务
    构建一个语音转文字的WebApi服务简介由于业务需要,我们需要提供一个语音输入功能,以便更方便用户的使用,所以我们需要提供语音转文本的功能,下面我们将讲解使用Whisper将语音转换文本,并且封装成WebApi提供web服务给前端调用。创建项目第一步打开Vscode,选择一个目录作为工作空间,然......
  • 创建用户及设置密码等,设置sudoers普通用户sudo权限
    [root@sourcefiles]#grep-iroot /etc/sudoers##therootuser,withoutneedingtherootpassword.##Allowroottorunanycommandsanywhereroot  ALL=(ALL)   ALLaaa192.168.8.101=(root)ALLyy192.168.8.101=(root)ALLxx192.168.8.101=(root)......
  • 【漏洞分析】Reflection Token 反射型代币攻击事件通用分析思路
    在本篇文章中,我将通过一个攻击事件引出ReflectionToken攻击事件的一个通用分析思路。关于ReflectionToken的其他案例分析,可以参考BEVO代币攻击事件分析及复现一文。TomInu攻击事件TomInuToken是一个反射型代币reflectiontoken,于2023-01-26遭到黑客攻击,攻击者获利355......
  • 金蝶云星空使用webapi查询单据附件的主键
    业务需求:查询采购价目表的附件 详细操作 一、查询单据附件查看账套单据附件 查询采购价目表的单据内码和单据体内码SELECTa.FNUMBER,a.FID,b.FENTRYID,b.FSEQFROMt_PUR_PriceListaLEFTJOINt_PUR_PriceListEntrybONa.FID=b.FIDWHEREa.FNUMBER='CGJM......
  • 微信社群机器人API
    请求URL:http://域名地址/sendHeadImage请求方式:POST请求头Headers:Content-Type:application/jsonAuthorization:Authorization值(登录获取码信息接口中返回的认证信息值)参数:参数名必选类型说明wId是string登录实例标识path是string图片url链接返回数据:参数名类型说明codestring1000成......
  • 使用百度翻译API 实现中译英+英译中
    2、完成百度翻译相关功能代码并测试调用,要求可以实现中文翻译成英文,英文翻译成中文      3、完成百度翻译GUI相关功能代码并测试调用,要求可以实现中文翻译成英文,英文翻译成中文   、    ......
  • Apipost版IDEA插件
    Apipost-Helper是由Apipost推出的IDEA插件,写完接口可以进行快速调试,且支持搜索接口、根据method跳转接口,还支持生成标准的API文档,注意:这些操作都可以在代码编辑器内独立完成,非常好用!这里给大家介绍一下Apipost-Helper的安装和使用安装在IDEA编辑器插件中心输入Apipost搜索安装:......