If you're looking for a lightweight and intuitive way to consume REST APIs in C#, take a look at OpenRestClient!
What is OpenRestClient? OpenRestClient is a library designed to simplify HTTP request handling through clean, annotation-based attributes. It helps you write less boilerplate and focus more on your application's logic.
Key Features Annotation-Based Requests: Use attributes like [GetMapping], [PostMapping], etc., to map methods to API endpoints. Authentication Support: Built-in JWT Bearer token handling with [RestAuthentication]. Environment Configuration: Easily configure API hosts and debugging via environment variables. Minimal Boilerplate: Clean and maintainable REST client code. Get Started GitHub: https://github.com/Clihsman/OpenRestClient NuGet Package: Install with: bash Copiar código dotnet add package OpenRestClient Simplify your REST API consumption in C# today! 🚀
using OpenRestClient;
[RestController("users")]
public class UserService : RestApp(typeof(UserService))
{
// GET /users
[GetMapping]
public Task<List<User>?> GetUsers()
=> Call<List<User>>(nameof(GetUsers));
// GET /users?name={name}
[GetMapping]
public Task<List<User>?> GetUsers([InQuery] string name)
=> Call<List<User>>(nameof(GetUsers), name);
// GET /users/{id} (Current User as ID = 0)
[GetMapping]
public Task<User?> GetCurrentUser()
=> GetUserById(0);
// GET /users/{id}
[GetMapping]
public Task<User?> GetUserById([InField] int id)
=> Call<User>(nameof(GetUserById), id);
// POST /users
[PostMapping]
public Task<User?> CreateUser([InBody] UserRequest userRequest)
=> Call<User>(nameof(CreateUser), userRequest);
}
[RestController("auth")]
public class LoginService : RestApp
{
public LoginService() : base(typeof(LoginService)) { }
// POST /auth/signin with JWT Authentication
[PostMapping("signin")]
[RestAuthentication(AuthenticationType.JWT, AuthenticationMode.BEARER, "token")]
public Task Signin([InBody] User user)
=> Call(nameof(Signin), user);
}
class MainClass {
static async Task Main(string[] args) {
// Configuration
Environment.SetEnvironmentVariable("opendev.openrestclient.host",
"http://example:3000/api");
Environment.SetEnvironmentVariable("opendev.openrestclient.debug",
"true");
// Authenticate User
LoginService loginService = new LoginService();
await loginService.Signin(new User("email@example", "pass"));
// Fetch Users
UserService service = new UserService();
List<User> users = await service.GetUsers(); // No filter
List<User> usersByName = await service.GetUsers("John"); // Filter by name
}
}