In this article, Let me show you my open source Nuget package RESTCountries.NET developed in .NET Standard.
RESTCountries.NET is a .NET Standard wrapper library around the API provided by REST Countries(https://restcountries.eu).
htpps://restcountries.eu is a RESTful API which provides information about countries such as name , capital city , population , currencies ,borders info , languages , flag , calling codes , etc.
With RESTCountries.NET , we can:
- Retrieve a list of countries.
- Get a list of country names in others languages such as German , Spanish , French , Italian, Portuguese, Dutch, Croatian , Japanese , Breton , and Persian language.
- Search by country name.
- Search by capital city.
- Search by ISO 4217 currency code.
- Search by continent: Africa, Americas, Asia, Europe, Oceania.
- Search by regional bloc.
- Apply filters to retrieve what we need.
- etc.
Setup
- The library is available on NuGet at https://www.nuget.org/packages/RESTCountries.NET/
- Install it into your .NET project(.NET Standard, .NET Core, Xamarin, etc).
Usage
- Add namespace
// Add namespace | |
using RESTCountries.Services; |
- Get all countries
// Get all countries | |
List<Country> countries = await RESTCountriesAPI.GetAllCountriesAsync(); |
Each method return an object of type Country or a List of Country. You can apply filters on the returned value to retrieve what you need.
- Get a list of country names
// Get a list of country names | |
var all = await RESTCountriesAPI.GetAllCountriesAsync(); | |
List<string> countries = all.Select(c => c.Name).ToList(); |
Country names are in English by default.
- Retrieve a list of country names in Spanish
// Get all countries in Spanish language | |
var countries = await RESTCountriesAPI.GetAllCountriesAsync(); | |
List<string> countriesInSpanish = countries.Select(c => c.Translations.Es).ToList(); |
Available languages are: de(German language), es(Spanish language), fr(French language), ja(Japanese language), it(Italian language), br(Breton language), pt(Portuguese language), nl(Dutch language), hr(Croatian language) and fa(Persian language).
- Search by country partial name or full name
// Search by country name | |
List<Country> result = await RESTCountriesAPI.GetCountriesByNameContainsAsync(string name); | |
// Search by country full name | |
Country country = await RESTCountriesAPI.GetCountryByFullNameAsync(string fullName); |
The first method could return a list of countries or a list of one element.
- Search by continent: Africa, Americas, Asia, Europe, Oceania
// Search by continent: Africa, Americas, Asia, Europe, Oceania | |
List<Country> result = await RESTCountriesAPI.GetCountriesByContinentAsync(string continent); |
Possibles values of “continent” are Africa, Americas, Asia, Europe and Oceania.
For more information, check out the full documentation at https://github.com/egbakou/RESTCountries.NET
Conclusion
In web applications(.NET or .NET Core) RESTCountries.NET allows us to populate country select tag options dynamically. Populate Xamarin Picker with a list of countries become easy.
Top comments (0)