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" />
<PackageReference Include="Zanway578.Usda.Fdc.Api" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Zanway578.Usda.Fdc.Api&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
USDA FDC API Wrapper for .NET
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:
- The generated code is messy and calling API endpoints is unintuitive.
- 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.
- Lots of pointers floating around in the generated code. I like to avoid pointers in C# unless necessary.
- 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:
- A single API client class
FdcClientused to contact the API end point. - Ability to inject as a singleton instance in any .NET API/Service/Razor/Etc. application.
- Enumeration types supplied with custom JsonConverters to help with serializing/deserializing JSON.
- Zero depedencies - written using only System libraries.
How to Integrate into Application
- Clone respository.
- 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 | Versions 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 |