VOOZH about

URL: https://www.nuget.org/packages/Formula.SimpleAPIClient/

⇱ NuGet Gallery | Formula.SimpleAPIClient 1.8.0




Formula.SimpleAPIClient 1.8.0

dotnet add package Formula.SimpleAPIClient --version 1.8.0
 
 
NuGet\Install-Package Formula.SimpleAPIClient -Version 1.8.0
 
 
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="Formula.SimpleAPIClient" Version="1.8.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Formula.SimpleAPIClient" Version="1.8.0" />
 
Directory.Packages.props
<PackageReference Include="Formula.SimpleAPIClient" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Formula.SimpleAPIClient --version 1.8.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Formula.SimpleAPIClient, 1.8.0"
 
 
#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.
#:package Formula.SimpleAPIClient@1.8.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Formula.SimpleAPIClient&version=1.8.0
 
Install as a Cake Addin
#tool nuget:?package=Formula.SimpleAPIClient&version=1.8.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Formula.SimpleAPIClient

Easily and securely consume API's from c#.

Goals

  • Provide an easy way of accessing API's behind various authentication schemes
    • OAuth2 - (initially using Client Credentials Grant).
    • Others ...
  • Apply Transient Exception Handling
    • Configurable - Try / Retry logic
  • Apply Circuit Breaker patterns
    • You should be able to gracefully handle catastrophic service failures

Example of use

This example is against a resource based RESTful API, protected by OAuth2 as a resource server.
For building API's like this, see the following projects.

using System;
using System.Threading.Tasks;
using Formula.SimpleAPIClient;
using System.Collections.Generic;

namespace Formula.SimpleAPIClient.Examples.ConsoleApp
{
 /// <summary>
 /// A basic model we hope to receive from our API
 /// </summary>
 public class ProductTypeModel
 {
 public int Id { get; set; }

 public String ProductType { get; set; }
 }

 /// <summary>
 /// A wrapper around an API we wish to consume
 /// </summary>
 public class APIClient
 {
 protected OAuth2Client client = null;

 /// <summary>
 /// In our constructor we will wire up an OAuth2 Client
 /// As we will be calling an API that is secured.
 /// 
 /// All of the token negotiation will be handled for us
 /// </summary>
 public APIClient()
 {
 var config = new OAuth2ConfigModel() {
 AuthServerAddress = "http://localhost:5000",
 ClientId = "my-client-id",
 ClientSecret = "my-client-secret",
 Scope = null,
 TokenExpirationThresholdSeconds = 60 // Fetch new token if we are within 60 seconds of expiration
 };

 this.client = new OAuth2Client(config);
 }

 /// <summary>
 /// Here is where we will provide a function to get product types from 
 /// an API endpoint
 /// </summary>
 /// <returns>A list of product types</returns>
 public async Task<List<ProductTypeModel>> GetProductTypes()
 {
 // Our goal is to return a list of product types
 List<ProductTypeModel> productTypes = null;
 var endpointUri = "http://localhost:6100/test/";

 // Get the data from the the API and we are done
 // Your API endpoint might not return a payload like this, but for this example,
 // The API endpoint will return an envelope of type SimpleAPIResponse and the data within the envelop will
 // be a list of product types
 var resultsOfAPICall = await this.client.GetAsTypeAsync<SimpleAPIResponse<List<ProductTypeModel>>>(endpointUri);

 // There are many things that could have happened prevending us from making it
 // to our API.. We aren't authorized.. Services are down.. Network problems etc..
 // We have an opportunity to gracefully handle these
 if (resultsOfAPICall.IsSuccessful)
 {
 // We now know we made it to our API endpoint, 
 // let's unpack the data it returned
 // This will be whatever your API call returns as it's payload
 var payloadFromEndpoint = resultsOfAPICall.Data;

 // Your payload may vary, and may be a string or another complex type.. In this case our API returns 
 // a custom response envelope (built using Formula.SimpleAPI), so let's check it.
 if (payloadFromEndpoint.IsSuccessful)
 {
 // If the API endpoint says it did it's work successfully
 // So, let's unpack the products
 productTypes = payloadFromEndpoint.Data;
 }
 else
 {
 Console.WriteLine("Handle the specific of a failure as defined by your API");
 }
 }
 // We had some sort of failure and never made it to our API
 else
 {
 // Why did it fail?
 Console.WriteLine($"Failed to access the API! - {resultsOfAPICall.Message}");

 Console.WriteLine("");
 Console.WriteLine("Details:");

 // Display the details (which is a key value pair)
 foreach(var detail in resultsOfAPICall.Details)
 {
 Console.WriteLine($"{detail.Key} = {detail.Value}");
 }
 }

 // Now that we are done playing, let's return the product types back to whatever called us as promised.
 return productTypes;
 }

 }

 class Program
 {
 static async Task Main(string[] args)
 {
 var apiClient = new APIClient();

 var productTypes = await apiClient.GetProductTypes();
 if (productTypes != null)
 {
 foreach(var productType in productTypes)
 {
 Console.WriteLine(productType.ProductType);
 }
 }
 }
 }
}

Packages / Projects Used

Product Versions Compatible and additional computed target framework versions.
.NET net8.0 net8.0 is compatible.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Formula.SimpleAPIClient:

Package Downloads
Formula.SimpleAPIClient.Examples.ConsoleApp

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.8.0 12,721 3/28/2024
1.6.0 14,616 8/23/2022
1.1.1 2,116 8/4/2022
1.1.0 5,211 5/27/2021
1.0.0 1,605 9/1/2020