Basic Usage

Let's start from the begining! The WebApiClient is a generic type, so we need to provide the type that the client will manage. For the sake of simplicity, we'll use the following class for all examples in this site:

public class Restaurant
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public int? CountryId { get; set; }
}

Object Initialization

In order to create the client object, we have to inform a WebApiClientOptions object, which provides the basic information the client needs to make calls to the WebApi. In this example, we are creating a WebApiClient object using an WebApiClientOptions object with its default values:

WebApiClientOptions options = new WebApiClientOptions("http://localhost/testapi", "restaurants");

using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
{
	//Do stuff
}

Alternatively, you can explicit define the options you want:

WebApiClientOptions options = new WebApiClientOptions() {
		BaseAddress = "http://localhost/testapi",
		ContentType = ContentType.Json,
		Timeout = 30000,
		Controller = "restaurants"
	};

using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
{
	//Do stuff
}

GetOneAsync

Once we have a WebApiClient object properly created, we can start calling the api. The GetOneAsync method is used to retrieve just one object:

public async Task GetRestaurant(int id)
{
	using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
	{
		//returns the restaurant with id equals to the id provided, using the default GET action method on the api
		var restaurant1 = await client.GetOneAsync(id);
		
		//returns the restaurant with id equals to the id provided, using the "GetRestaurant" action method on the api
		var restaurant2 = await client.GetOneAsync(id, "GetRestaurant");
		
		//if your action method expects more than 1 parameter, you can provide an anonymous type
		var restaurant3 = await client.GetOneAsync(new { countryId = 1, regionId = 1 }, "GetCheapestRestaurantByCountryAndRegion");
		
		//the action is not needed unless you are using an action based routed rest service
		var restaurant4 = await client.GetOneAsync(new { countryId = 1, regionId = 1 });
		
		//you can call methods with no parameters as well
		var restaurant5 = await client.GetOneAsync();
	}
}

GetManyAsync

If you want to retrieve a list of objects, call GetManyAsync.
The WebApiClient methods have a consistent parameter naming convention and position, as you can see below:

public async Task GetRestaurants()
{
	using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
	{
		//returns a list of restaurants, using the default GET action method on the api
		var restaurants1 = await client.GetManyAsync();
		
		//returns a list of restaurants, using the "GetRestaurants" action method on the api
		var restaurants2 = await client.GetManyAsync("GetRestaurants");
		
		//if your action method expects more than 1 parameter, you can provide an anonymous type
		var restaurants3 = await client.GetManyAsync(new { countryId = 1, regionId = 1 }, "GetRestaurantsByCountryAndRegion");
		
		//the action is not needed unless you are using an action based routed rest service
		var restaurants4 = await client.GetManyAsync(new { countryId = 1, regionId = 1 });
		
		//you can call methods with no parameters as well
		var restaurants5 = await client.GetManyAsync();
	}
}

EditAsync

To edit an object, call EditAsync:

public async Task EditRestaurant()
{
	using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
	{
        //gets the restaurant we need to edit
		var rest = await client.GetOneAsync(1);
        
        //changes some properties
        rest.Name = "New restaurant name";

        //calls the default PUT action method on the api. Returns the edited restaurant, but this is optional
        var updated = await client.EditAsync(rest);
		
		//calls the PUT action method "EditRestaurant" on the api
		await client.EditAsync(rest, "EditRestaurant");
	}
}

CreateAsync

To Create an object, call CreateAsync:

public async Task CreateRestaurant()
{
	using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
	{
		//Creates a restaurant object
		Restaurant rest = new Restaurant();
		rest.Name = "New Restaurant";
		rest.CountryId = 1;
		rest.Address = "1234, Road Street";

        //calls the default POST action method on the api. Returns the created restaurant, but this is optional
		var ret = await client.CreateAsync(rest);

		//calls the POST action method "CreateRestaurant" on the api
		await client.CreateAsync(rest, "CreateRestaurant");
	}
}

DeleteAsync

To Delete an object, call DeleteAsync:

public async Task DeleteRestaurant()
{
	using(WebApiClient<Restaurant> client = new WebApiClient<Restaurant>(options))
	{
        //calls the default DELETE action method on the api
		await client.DeleteAsync(1);

		//calls the DELETE action method "DeleteRestaurant" on the api
		await client.DeleteAsync(1, "DeleteRestaurant");
	}
}