ApiClientTemplate is a dotnet project template that provides a simple way to create RESTful APIs client in C# applications.
If you like this project please give a star or a cup of coffee =)
To install use dotnet cli:
dotnet tool install --global ApiClientTemplate
now you can create a new project using the template
dotnet new apiclient -n <ProjectName>
Write your request methods in 'YourApiClient (Client)' class
Below examples how to use your client
var yourApiClient = new YourApiClient(new YourApiClientOptions()
{
ApiKey = Environment.GetEnvironmentVariable("MY_API_KEY")
});
In your secrets.json or other settings.json
"YourApiClientOptions": {
//"ApiKey": "Your api key goes here",
//"ApiBaseAddress": "Your api base address goes here"
},
serviceCollection.AddYourApiClient();
or using Environment Variable
serviceCollection.AddYourApiClient(settings => { settings.ApiKey = Environment.GetEnvironmentVariable("MY_API_KEY"); });
NOTE: do NOT put your API key directly to your source code.
After injecting your service you will be able to get it from service provider
var yourApiClient = serviceProvider.GetRequiredService<YourApiClient>();
or injecting in the constructor of your class
public class MyService
{
private readonly YourApiClient _apiClient;
public NewsService(YourApiClient apiClient)
{
_apiClient = apiClient;
}
}
For diagnostic there are 'DelegatingHandler'-s such as 'LoggingHandler' and 'ExceptionHandler'. You can always extend them