No description
| .nuke | ||
| build | ||
| src | ||
| .drone.yml | ||
| .editorconfig | ||
| .gitignore | ||
| build.cmd | ||
| build.ps1 | ||
| build.sh | ||
| Readme.md | ||
API Client library
Simple project to server 2 purposes
- Provide a way of doing API calls to a JSON REST API with caching
- Test the personal nuget server (BaGet) with a simple package
Usage
ApiClient
ApiClient is an abstract class designed to be inherited from for your actual API calls. It requires an instance of CacheDurations to be passed in to the constructor.
To use, simply inherit from ApiClient and use Post for posting data and Get for getting data. Both Post and Get are generic methods and will deserialise and return the type you specify. They both take anonymous objects for their query parameters.
Signatures:
protected async Task<T> Post<T>(string url, object value, object query = null);
protected async Task<T> Get<T>(string url, object query = null);
Example:
public async Task<IEnumerable<BlogPost>> GetLatestPostsAsync(int count = 0, int offset = 0) => await Get<IEnumerable<BlogPost>>("getlatestposts", new{count, offset});
public async Task<BlogPost> SavePostAsync(BlogPostSubmission post) => await Post<BlogPost>("savepost", post);
CacheDurations
Create an instance of CacheDurations with your various as you would with a dictionary initialiser:
new CacheDurations
{
{
"class", new Dictionary<string, int>
{
{"method", 1}
}
}
};
For MVC
In your Startup.cs add this to your ConfigureServices method:
services.AddSingleton(Configuration.GetSection("cacheDurations").Get<CacheDurations>());
Settings take the following format in appsettings.json:
"cacheDurations": {
"default": {
"default": 30 // default cache time for all calls
},
"BlogApi": { // override default cache time for class
"default": 60, // default for this class
"GetPostByUrlAsync": 120, // override for this method
}
}