https://github.com/carlosbrunetti/respify
Respify is a .NET library designed to simplify the creation of standardized API responses. It provides a set of helper methods and response classes to ensure consistent and clear communication between your API and its consumers. Key features include support for paginated and non-paginated responses, as well as custom responses.
https://github.com/carlosbrunetti/respify
api api-response csharp custom-responses dotnet dotnet-core non-paginated-responses open-source paginated-responses standardized-responses
Last synced: 5 months ago
JSON representation
Respify is a .NET library designed to simplify the creation of standardized API responses. It provides a set of helper methods and response classes to ensure consistent and clear communication between your API and its consumers. Key features include support for paginated and non-paginated responses, as well as custom responses.
- Host: GitHub
- URL: https://github.com/carlosbrunetti/respify
- Owner: carlosbrunetti
- License: mit
- Created: 2024-12-21T15:56:19.000Z (over 1 year ago)
- Default Branch: develop
- Last Pushed: 2025-01-14T13:37:08.000Z (over 1 year ago)
- Last Synced: 2025-01-14T14:43:12.732Z (over 1 year ago)
- Topics: api, api-response, csharp, custom-responses, dotnet, dotnet-core, non-paginated-responses, open-source, paginated-responses, standardized-responses
- Language: C#
- Homepage:
- Size: 421 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
[](https://www.nuget.org/packages/Respify/)
[](https://www.nuget.org/packages/Respify/)
[](https://sonarcloud.io/dashboard?id=carlosbrunetti_Respify)
[](https://sonarcloud.io/dashboard?id=carlosbrunetti_Respify)
[](https://sonarcloud.io/dashboard?id=carlosbrunetti_Respify)
[](https://sonarcloud.io/dashboard?id=carlosbrunetti_Respify)
[](https://sonarcloud.io/dashboard?id=carlosbrunetti_Respify)
## Installation
To install Respify, you can add it to your project via NuGet Package Manager:
```sh
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
```csharp
using Respify;
using Respify.helpers;
var cars = new List();
var paginatedResponse = new PaginatedResponse>(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
```csharp
using Respify;
using Respify.helpers;
var cars = new List();
var nonPaginatedResponse = new NonPaginatedResponse>(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.
```csharp
using Respify;
using Respify.helpers;
var response = ResponseHelper.Failure(Data: null, Message: "Failure", StatusCode: 400);
```
Or
You also can generate failure responses with optional 'Message' with a list of errors.
```csharp
using Respify;
using Respify.helpers;
var errors = new List();
var response = ResponseHelper.Failure(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.
```csharp
using Respify;
using Respify.helpers;
int id = 1;
var response = ResponseHelper.CreateResponse(Data: id, Message: "message", StatusCode: 201, Success: true, Errors: null);
```
Or
```csharp
var response = ResponseHelper.CreateResponse(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.
```csharp
var response = new RespifyResponse("data", "Operation successful", true, 200);
var result = response.ToResult();
```
### Asynchronous Response Conversion
```csharp
var response = new RespifyResponse("data", "Operation successful", true, 200);
var result = await response.ToResultAsync();
```
## Json Output
### Paginated Response
```json
{
"data": {
"items": [],
"count": 0,
"pageNumber": 1,
"pageSize": 15,
"orderBy": "Make",
"sortBy": "desc"
},
"message": "Success",
"success": true,
"errors": null
}
```
### Non-Paginated Response
```json
{
"data": {
"items": [],
"count": 0
},
"message": "Success",
"success": true,
"errors": null
}
```
### Custom Response
```json
{
"data": 1,
"message": "Created",
"success": true,
"errors": null
}
```
### Failure Response
```json
{
"data": null,
"message": "",
"success": false,
"errors": ["Error 1", "Error 2"]
}
```
## Classes
### NonPaginatedData
Represents non-paginated data.
```csharp
///
/// Represents a non-paginated response.
///
/// The type of the items in the response.
public class NonPaginatedResponse : INonPaginatedResponse
{
///
/// Initializes a new instance of the class.
///
/// The items in the response.
/// The total count of items in the response.
public NonPaginatedResponse(T items, int total) => (Items, Count) = (items, total);
///
/// Gets or sets the items in the response.
///
public T Items { get; set; }
///
/// Gets or sets the count of items in the response.
///
public int Count { get; set; }
}
```
### PaginatedData
Represents paginated data.
```csharp
///
/// Represents a paginated response.
///
/// The type of the items in the response.
public class PaginatedResponse : IPaginatedResponse
{
///
/// Initializes a new instance of the class.
///
/// The items in the response.
/// The total count of items in the response.
/// The current page number.
/// The size of the page.
/// The field by which the items are ordered.
/// The field by which the items are sorted.
public PaginatedResponse(T items, int total, int page, int pageSize, string orderBy, string sort)
{
Items = items;
Count = total;
PageNumber = page;
PageSize = pageSize;
OrderBy = orderBy;
SortBy = sort;
}
///
/// Gets or sets the items in the response.
///
public T Items { get; set; }
///
/// Gets or sets the count of items in the response.
///
public int Count { get; set; }
///
/// Gets or sets the current page number.
///
public int PageNumber { get; set; }
///
/// Gets or sets the size of the page.
///
public int PageSize { get; set; }
///
/// Gets or sets the field by which the items are ordered.
///
public string OrderBy { get; set; }
///
/// Gets or sets the field by which the items are sorted.
///
public string SortBy { get; set; }
}
```
### RespifyResponse
Represents a standardized API response.
```csharp
///
/// Represents a standardized response object used in the Respify framework.
///
/// The type of the data being returned in the response.
public class RespifyResponse : IRespifyResponse
{
///
/// Initializes a new instance of the class.
///
/// The data being returned in the response.
/// The message associated with the response.
/// A value indicating whether the response indicates a successful operation.
/// The HTTP status code associated with the response.
/// A list of errors associated with the response.
public RespifyResponse(T? data, string? message, bool success, int statusCode, List? errors)
{
Data = data;
Message = message;
Success = success;
StatusCode = statusCode;
Errors = errors;
}
///
/// Initializes a new instance of the class.
///
/// The data being returned in the response.
/// The message associated with the response.
/// A value indicating whether the response indicates a successful operation.
/// The HTTP status code associated with the response.
public RespifyResponse(T? data, string message, bool success, int statusCode)
{
Data = data;
Message = message;
Success = success;
StatusCode = statusCode;
}
///
/// Gets or sets the data being returned in the response.
///
public T? Data { get; set; }
///
/// Gets or sets the message associated with the response.
///
public string? Message { get; set; }
///
/// Gets or sets a value indicating whether the response indicates a successful operation.
///
public bool Success { get; set; }
///
/// Gets or sets the HTTP status code associated with the response.
///
[JsonIgnore]
public int StatusCode { get; set; }
///
/// Gets or sets a list of errors associated with the response.
///
public List? Errors { get; set; }
///
/// Converts the response to an .
///
/// An representing the response.
public ObjectResult ToResult()
{
return new ObjectResult(this)
{
StatusCode = this.StatusCode
};
}
///
/// Asynchronously converts the response to an .
///
/// A representing the asynchronous operation, with an as the result.
public async Task ToResultAsync()
{
return await Task.FromResult(new ObjectResult(this)
{
StatusCode = this.StatusCode
});
}
}
```
### ResponseHelper
Provides helper methods to generate standardized API responses.
```csharp
using Respify.Interfaces;
namespace Respify.helpers;
///
/// Provides helper methods to generate standardized API responses.
///
public static class ResponseHelper
{
///
/// Generates a success response for paginated data.
///
/// The type of the data items.
/// The paginated response data.
/// The success message.
/// The HTTP status code (default is 200).
/// A standardized success response.
public static RespifyResponse> Success(PaginatedResponse data, string message, int statusCode = 200)
{
return new RespifyResponse>(data, message, true, statusCode, null);
}
///
/// Generates a success response for non-paginated data.
///
/// The type of the data items.
/// The non-paginated response data.
/// The success message.
/// The HTTP status code (default is 200).
/// A standardized success response.
public static RespifyResponse> Success(NonPaginatedResponse data, string message, int statusCode = 200)
{
return new RespifyResponse>(data, message, true, statusCode, null);
}
///
/// Creates a custom response.
///
/// The type of the data.
/// The response data.
/// The response message.
/// The HTTP status code.
/// Indicates if the response is successful.
/// A list of errors (if any).
/// A standardized response.
public static RespifyResponse CreateResponse(T? data, string? message, int statusCode, bool success, List? errors)
{
return new RespifyResponse(data, message, success, statusCode, errors);
}
///
/// Generates a failure response with optional data.
///
/// The type of the data.
/// The response data (optional).
/// The failure message.
/// The HTTP status code (default is 400).
/// A standardized failure response.
public static RespifyResponse Failure(T? data, string message, int statusCode = 400)
{
return new RespifyResponse(data, message, false, statusCode);
}
///
/// Generates a failure response with optional data and a list of errors.
///
/// The type of the data.
/// The response data (optional).
/// The failure message.
/// A list of errors.
/// The HTTP status code (default is 400).
/// A standardized failure response.
public static RespifyResponse Failure(T? data, string message, List errors, int statusCode = 400)
{
return new RespifyResponse(data, message, false, statusCode, errors);
}
}
```
## 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.