ApiCallManager 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ApiCallManager --version 1.2.1                
NuGet\Install-Package ApiCallManager -Version 1.2.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ApiCallManager" Version="1.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ApiCallManager --version 1.2.1                
#r "nuget: ApiCallManager, 1.2.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ApiCallManager as a Cake Addin
#addin nuget:?package=ApiCallManager&version=1.2.1

// Install ApiCallManager as a Cake Tool
#tool nuget:?package=ApiCallManager&version=1.2.1                

ApiCallManager

REST API call manager

How to use ApiManager in ASP.NET Core Web API Projects

in asp.net core web api project you should add ApiManager in Service Container:

builder.Services.AddHttpClient();
builder.Services.AddScoped<ApiCallManager.IApiManager, ApiCallManager.ApiManager>();

Note that if you don't want to use HttpClientFactory, you can remove builder.Services.AddHttpClient(); line

Then Inject ApiManager service where you want and use it. In below example code ApiManager service injected into TestController:

[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{
    private IApiManager _apiManager;
    private readonly ILogger<TestController> _logger;

    public TestController(ILogger<TestController> logger, IApiManager apiManager)
    {
        _apiManager = apiManager;
        _logger = logger;
    }

    [HttpGet]
    public async Task<ActionResult> GetUsers()
    {
        var result = await _apiManager.GetAsync<List<User>>("https://gorest.co.in/public/v2/users");

        if (result.IsSuccess)
        {
            return Ok(result.Result);
        }
        else
        {
            return Problem(
                    type: result?.Problem?.Type,
                    statusCode: result?.Problem?.Status,
                    title: result?.Problem?.Title,
                    detail: result?.Problem?.Detail
                );
        }
    }
}

How to use ApiManager in Console or Windows Application Projects

in console applications or windows applications you can create a static instance of ApiManager inside a Helper class or in program global scope.

public class ClassHelper
{
    public static IApiManager apiManager;

    public static void ShowApiCallError(ValidationProblemDetails? error)
    {
        if (error != null)
        {
            MessageBox.Show($"Title: {error?.Title ?? "-"}\n\rDetails: {error?.Detail ?? "-"}", "API Call Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            MessageBox.Show($"An unknown error has occurred!", "API Call Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

Then in program startup or main form loading initial defined static ApiManager object.

private void Form1_Load(object sender, EventArgs e)
{
    ClassHelper.apiManager = new ApiManager();
}

If you use Authorization in application, after user login to application and Access Token and Refresh Token recieved from Authorization server, Set Token in ApiManger:

private async void button_login_Click(object sender, EventArgs e)
{
    string username = textBox_username.Text;
    string password = textBox_password.Text;

    var res = await LoginAsync(username, password);
    if (res != null)
    {
        ClassHelper.apiManager.SetTokens(res.AccessToken, res.RefreshToken, "https://gorest.co.in/public/v2/refreshtoken", true);
    }
}

Congratulation, you now call any web api using ApiManger easily.

public async Task<List<User>?> GetUsersAsync()
{
    var result = await ClassHelper.apiManager.GetAsync<List<User>>("https://gorest.co.in/public/v2/users");

    if (result.IsSuccess)
    {
        return result.Result;
    }
    else
    {
        ClassHelper.ShowApiCallError(result.Problem);
        return null;
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
6.1.2 180 3/11/2024
6.1.1 120 3/6/2024
6.1.0 128 3/6/2024
6.0.1 143 2/4/2024
6.0.0 245 12/10/2023
1.3.1 209 7/9/2023
1.3.0 192 7/6/2023
1.2.1 182 5/28/2023
1.2.0 171 5/23/2023
1.1.1 169 5/15/2023
1.1.0 161 5/14/2023