暂无描述

Robert Marshall b56b633232 Update all the things 1 年之前
.nuke b56b633232 Update all the things 1 年之前
build b56b633232 Update all the things 1 年之前
src b56b633232 Update all the things 1 年之前
.drone.yml b56b633232 Update all the things 1 年之前
.editorconfig 35fc3df32e Move ApiClient out of Website repo 4 年之前
.gitignore b56b633232 Update all the things 1 年之前
Readme.md b1f604ea75 Add readme 4 年之前
build.cmd b56b633232 Update all the things 1 年之前
build.ps1 b56b633232 Update all the things 1 年之前
build.sh b56b633232 Update all the things 1 年之前

Readme.md

Build Status

API Client library

Simple project to server 2 purposes

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
	}
}