Zanway578.Usda.Fdc.Api 1.0.0

dotnet add package Zanway578.Usda.Fdc.Api --version 1.0.0
                    
NuGet\Install-Package Zanway578.Usda.Fdc.Api -Version 1.0.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="Zanway578.Usda.Fdc.Api" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Zanway578.Usda.Fdc.Api" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Zanway578.Usda.Fdc.Api" />
                    
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 Zanway578.Usda.Fdc.Api --version 1.0.0
                    
#r "nuget: Zanway578.Usda.Fdc.Api, 1.0.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 Zanway578.Usda.Fdc.Api@1.0.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=Zanway578.Usda.Fdc.Api&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Zanway578.Usda.Fdc.Api&version=1.0.0
                    
Install as a Cake Tool

USDA FDC API Wrapper for .NET

Build status

Purpose

The USDA FDC API (Swaggerhub docs) is a great resource for finding information on food, including how it's been lab tested and it's nutritional information. There is the option on the swaggerhub page to download client libraries for a variety of languages. While convenient, I found several issues with the C# one:

  1. The generated code is messy and calling API endpoints is unintuitive.
  2. Lack of enumeration types. Many of the endpoints call for strings that have a fixed number of options (e.g. SortBy, SortOrder, DataType, etc.). As a lover of strong static typing, I didn't like this and wished there was code included to allow for enum types.
  3. Lots of pointers floating around in the generated code. I like to avoid pointers in C# unless necessary.
  4. If there are any bugs in the generated code, it will be a mess to debug.

Hence, I present an API wrapper that is much easier to use, with the following features:

  1. A single API client class FdcClient used to contact the API end point.
  2. Ability to inject as a singleton instance in any .NET API/Service/Razor/Etc. application.
  3. Enumeration types supplied with custom JsonConverters to help with serializing/deserializing JSON.
  4. Zero depedencies - written using only System libraries.

How to Integrate into Application

  1. Clone respository.
  2. Either build and add .dll as a reference or add the .csproj file as a project reference in Visual Studio.

Simple Example

Here's a very basic use of the API client to illustrate how it works:

using Usda.Fdc.Api;
using Usda.Fdc.Api.Models;
using Usda.Fdc.Api.Models.Enums;

FdcClient.GlobalApiKey = "{YOUR API KEY}";

// Must inject HTTP client as a dependency
var client = new HttpClient();

// Create objecrt containing criteria for foods we want to search
var listCriteria = new FoodListCriteria()
{
    DataType = new[] { FdcDataType.Foundation },
    SortBy = FdcSortBy.LowercaseDescription,
    SortOrder = FdcSortOrder.Desc,
    PageNumber = 1,
    PageSize = 25
};

var fdcClient = new FdcClient(client);

var foods = await fdcClient.GetFoodList(listCriteria);
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

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
1.0.0 230 7/2/2024