Respify 1.1.0
dotnet add package Respify --version 1.1.0
NuGet\Install-Package Respify -Version 1.1.0
<PackageReference Include="Respify" Version="1.1.0" />
paket add Respify --version 1.1.0
#r "nuget: Respify, 1.1.0"
// Install Respify as a Cake Addin #addin nuget:?package=Respify&version=1.1.0 // Install Respify as a Cake Tool #tool nuget:?package=Respify&version=1.1.0
Respify
Respify is a C# library designed to standardize API responses. It provides a consistent structure for both paginated and non-paginated data, making it easier to handle API responses in a uniform way.
Installation
To install Respify, you can add it to your project via NuGet Package Manager:
dotnet add package Respify
Generating Success Responses
You can generate success responses for both paginated and non-paginated return object using the ResponseHelper
class.
Paginated Response
using Respify;
using Respify.helpers;
var cars = new List<Car>();
var paginatedResponse = new PaginatedResponse<List<Car>>(Items: cars, Count: cars.Count, PageNumber: 1, PageSize: 15, OrderBy: "Make", SortBy: "desc");
var response = ResponseHelper.Success(Data: paginatedResponse, Message: "Success", StatusCode: 200);
Non-Paginated Response
using Respify;
using Respify.helpers;
var cars = new List<Car>();
var nonPaginatedResponse = new NonPaginatedResponse<List<Car>>(Items: cars, Count: cars.Count);
var response = ResponseHelper.Success(Data: nonPaginatedResponse, Message: "Success", StatusCode: 200);
Generating Failure Responses
You can generate failure responses with optional return object and a list of errors.
using Respify;
using Respify.helpers;
var response = ResponseHelper.Failure<object>(Data: null, Message: "Failure", StatusCode: 400);
Or
You also can generate failure responses with optional 'Message' with a list of errors.
using Respify;
using Respify.helpers;
var errors = new List<string>();
var response = ResponseHelper.Failure<object>(Data: null, Message: null, StatusCode: 400, Errors: errors);
Creating Custom Responses
You can create custom responses with specific object, message, status code, success flag, and errors.
using Respify;
using Respify.helpers;
int id = 1;
var response = ResponseHelper.CreateResponse(Data: id, Message: "message", StatusCode: 201, Success: true, Errors: null);
Or
var response = ResponseHelper.CreateResponse<object>(Data: null, Message: "message", StatusCode: 201, Success: true, Errors: null);
Converting Response to ObjectResult
Using ToResult() and ToResultAsync() methods, you can convert the response to an ObjectResult.
var response = new RespifyResponse<string>("data", "Operation successful", true, 200);
var result = response.ToResult();
Asynchronous Response Conversion
var response = new RespifyResponse<string>("data", "Operation successful", true, 200);
var result = await response.ToResultAsync();
Json Output
Paginated Response
{
"data": {
"items": [],
"count": 0,
"pageNumber": 1,
"pageSize": 15,
"orderBy": "Make",
"sortBy": "desc"
},
"message": "Success",
"success": true,
"errors": null
}
Non-Paginated Response
{
"data": {
"items": [],
"count": 0
},
"message": "Success",
"success": true,
"errors": null
}
Custom Response
{
"data": 1,
"message": "Created",
"success": true,
"errors": null
}
Failure Response
{
"data": null,
"message": "",
"success": false,
"errors": ["Error 1", "Error 2"]
}
License
This project is licensed under the MIT License.
This README.md
file provides an overview of the Respify library, installation instructions, usage examples, and class definitions.
Respify is a C# library designed to standardize API responses. It provides a consistent structure for both paginated and non-paginated data, making it easier to handle API responses in a uniform way.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.