OpenCodeDev.RestCountries 0.9.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenCodeDev.RestCountries --version 0.9.0                
NuGet\Install-Package OpenCodeDev.RestCountries -Version 0.9.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="OpenCodeDev.RestCountries" Version="0.9.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenCodeDev.RestCountries --version 0.9.0                
#r "nuget: OpenCodeDev.RestCountries, 0.9.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.
// Install OpenCodeDev.RestCountries as a Cake Addin
#addin nuget:?package=OpenCodeDev.RestCountries&version=0.9.0

// Install OpenCodeDev.RestCountries as a Cake Tool
#tool nuget:?package=OpenCodeDev.RestCountries&version=0.9.0                

About this Project

This project is inspired on restcountries.eu by Fayder Florez.

This project is a direct .NET rewrite of REST Countries Java (Alejandro Matos).

Important Information

Pakages

OpenCodeDev.RestCountries

This .NET 8 based pakage contains the core behaviour for the RestCountries without any dependencies.

var builder = WebApplication.CreateBuilder(args);
// this will make a singleton accessible through IRestCountries.
// replace RestCountriesEmbed.GetVersion("countriesV3.1") by your local or remote verson.json file.
builder.Services.AddRestCountriesServices(RestCountriesEmbed.GetVersion("countriesV3.1"));

OpenCodeDev.RestCountries.Embedded

Since Embedded includes all available JSON versions, it increases the package size, which may not be ideal for front-end deployments. However, it provides valuable resource files (all available versions) for server-side.

var builder = WebApplication.CreateBuilder(args);
// this will make a singleton accessible through IRestCountries.
builder.Services.AddRestCountriesServices(RestCountriesEmbed.GetVersion("countriesV3.1"));

Fields

You can check the FIELDS.md file to get info on each classes.

Getting Started

using OpenCodeDev.RestCountries.Embedded;

// Replace this to the json content of the version.json or use embeded content.
string jsonContentOfVersion = RestCountriesEmbed.GetVersion("countriesV3.1");
IRestCountries restCountries = new RestCountries(jsonContentOfVersion);
var countries = restCountries.GetAll();

Check if Postal Required

using OpenCodeDev.RestCountries.Embedded;

string jsonContentOfVersion = RestCountriesEmbed.GetVersion(version);
IRestCountries restCountries = new RestCountries(jsonContentOfVersion);
var countries = restCountries.GetAll();
var firstCountry = countries.First();
if (firstCountry.IsPostalRequired()) 
    Console.WriteLine($"{firstCountry.Name.Common} does require postal of format: {firstCountry.PostalFormat!.Format}");
else
    Console.WriteLine($"{firstCountry.Name.Common} does not require a postal.");

Fetch Country by ISO-2 & Validate its postal code

using OpenCodeDev.RestCountries.Embedded;

string jsonContentOfVersion = RestCountriesEmbed.GetVersion(version);
IRestCountries restCountries = new RestCountries(jsonContentOfVersion);
var countries = restCountries.GetAll();
// ca = Canada. You could also do CCA3 with CAN.
var firstCountry = restCountries.IsValidCCA2Code("ca");
if (restCountries.IsValidCCA2Code("ca"))
    if (restCountries.GetByCCA2Code("cA")!.IsPostalValid("H1A2T2")) 
        Console.WriteLine("H1A2T2 is a valid postal for Canada");
    else
        Console.WriteLine($"ca is not a valid country iso2.");

More to Explore

Explore the other functions

using OpenCodeDev.RestCountries.Embedded;

string jsonContentOfVersion = RestCountriesEmbed.GetVersion(version);
IRestCountries restCountries = new RestCountries(jsonContentOfVersion);
var countries = restCountries.GetAll();
if (restCountries.IsValidCCA2Code("ca")) Console.WriteLine("This is valid country code.");
if (restCountries.IsValidCCA3Code("can")) Console.WriteLine("This is valid country code.");
if (restCountries.IsValidCCA2PostalCode("ca", "H1T2S2")) Console.WriteLine("This is valid country code and postal.");
if (restCountries.IsValidCCA3PostalCode("can", "H1T2S2")) Console.WriteLine("This is valid country code and postal.");
if (restCountries.IsValidCCA2Currency("ca", "CAD")) Console.WriteLine("CAD is a valid Canadian Currency.");
if (restCountries.IsValidCCA3Currency("cad", "CAD")) Console.WriteLine("CAD is a valid Canadian Currency.");
if (restCountries.IsValidCCA3Currency("cad", "CAD")) Console.WriteLine("CAD is a valid Canadian Currency.");

var country = countries.First()!;
Console.WriteLine($"Phone Prefixes are {string.Join(',', country.PhonePrefixes())}");

Similar projects

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on OpenCodeDev.RestCountries:

Package Downloads
OpenCodeDev.RestCountries.Embedded

An additional package, recommended for server use only, dependent on the C# RestCountries port, providing versioned country data for instant access and easy installation.

OpenCodeDev.RestCountries.Data

An additional package, dependent on the C# RestCountries port, providing versioned country data files (the package version matches the JSON version available) for instant access and easy installation.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.9.3 124 12/11/2024
0.9.2 115 12/11/2024
0.9.1 94 12/10/2024
0.9.0 98 12/10/2024