From 5c19f17c13f7e6c97dfd9b15699ee42b4e42240f Mon Sep 17 00:00:00 2001 From: Alejandro Matos Date: Fri, 10 Mar 2023 13:22:34 -0500 Subject: [PATCH] Closes #186 #189: * fixing Austrian language field * adding 'independent' endpoint to get only independent countries --- README.md | 233 ++---------------- .../controller/ControllerV3Helper.java | 3 +- .../controller/CountryControllerV31.java | 18 ++ .../service/v3/v31/CountryServiceV31.java | 9 + src/main/resources/countriesV3.1.json | 2 +- 5 files changed, 55 insertions(+), 210 deletions(-) diff --git a/README.md b/README.md index 13ca066..11320bc 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ This project is inspired on restcountries.eu by Fayder Florez. Although the original project has now moved to a subscription base API, this project is still Open Source and Free to use. -## Important Information About Version 2 -The structure has been reverted to its original form from the [Original Project] to maintain compatibility. +## Important Information +* The structure of V2 has been reverted to its original form from the [Original Project] to maintain compatibility. +* Only the latest version will receive updates and improvements. # REST Countries You can access API through https://restcountries.com/v3.1/all @@ -23,17 +24,6 @@ You can check the [FIELDS.md](FIELDS.md) file to get a description for each fiel # API Endpoints -Currently there are 3 versions: -- Version 2 is the original version from restcountries.eu by Fayder Florez -- Version 3 is the implementation from [this project](https://github.com/mledoze/countries) -- Version 3.1 adds named values to the `flags` object like this: - -```json -"flags": { - "svg": "https://restcountries.com/data/per.svg", - "png": "https://restcountries.com/data/png/per.png" -} -``` ## Using this Project - [Famosos](https://famosos.com) @@ -42,17 +32,24 @@ Currently there are 3 versions: - [Asendia](https://tracking.asendia.com) - [Picker](https://mwb.pickerexpress.com/#/login) -#Endpoints +# Endpoints Below are described the REST endpoints available that you can use to search for countries +## Latest added enpoint + +### Independent +Now you can get all independent (or not independent) countries by calling this endpoint: +``` html +https://restcountries.com/v3.1/independent?status=true +``` +If you don't specify the status, true will be taken as default. You can mix it with the `fields` filter like this: + +``` html +https://restcountries.com/v3.1/independent?status=true&fields=languages,capital +``` + ## All -### V2 -``` html -https://restcountries.com/v2/all -``` - -### V3.1 ``` html https://restcountries.com/v3.1/all ``` @@ -60,23 +57,7 @@ https://restcountries.com/v3.1/all ## Name -### V2 -Search by country name. It can be the native name or partial name - -``` javascript -https://restcountries.com/v2/name/{name} -``` - -``` html -https://restcountries.com/v2/name/eesti -``` - -``` html -https://restcountries.com/v2/name/united -``` - -### V3.1 -Search by country name. It can be the common or official value +**Search** by country name. If you want to get an exact match, use the next endpoint. It can be the common or official value ``` html https://restcountries.com/v3.1/name/{name} ``` @@ -90,18 +71,7 @@ https://restcountries.com/v3.1/name/deutschland ``` ## Full Name -### V2 -Search by country's full name -``` html -https://restcountries.com/v2/name/{name}?fullText=true -``` - -``` html -https://restcountries.com/v2/name/aruba?fullText=true -``` - -### V3.1 Search by country's full name. It can be the common or official value ``` html https://restcountries.com/v3.1/name/{name}?fullText=true @@ -111,21 +81,7 @@ https://restcountries.com/v3.1/name/{name}?fullText=true https://restcountries.com/v3.1/name/aruba?fullText=true ``` ## Code -### V2 -Search by ISO 3166-1 2-letter or 3-letter country code -``` html -https://restcountries.com/v2/alpha/{code} -``` - -``` html -https://restcountries.com/v2/alpha/co -``` - -``` html -https://restcountries.com/v2/alpha/col -``` -### V3.1 Search by cca2, ccn3, cca3 or cioc country code (yes, any!) ``` html @@ -144,17 +100,7 @@ https://restcountries.com/v3.1/alpha/col https://restcountries.com/v3.1/alpha/170 ``` ## List of codes -### V2 -Search by list of ISO 3166-1 2-letter or 3-letter country codes -``` html -https://restcountries.com/v2/alpha?codes={code},{code},{code} -``` - -``` html -https://restcountries.com/v2/alpha?codes=col,no,ee,pe -``` -### V3.1 Search by cca2, ccn3, cca3 or cioc country code (yes, any!) ``` html https://restcountries.com/v3.1/alpha?codes={code},{code},{code} @@ -164,16 +110,7 @@ https://restcountries.com/v3.1/alpha?codes={code},{code},{code} https://restcountries.com/v3.1/alpha?codes=170,no,est,pe ``` ## Currency -### V2 -Search by ISO 4217 currency code -``` html -https://restcountries.com/v2/currency/{currency} -``` -``` html -https://restcountries.com/v2/currency/cop -``` -### V3.1 Search by currency code or name ``` html https://restcountries.com/v3.1/currency/{currency} @@ -183,7 +120,6 @@ https://restcountries.com/v3.1/currency/cop ``` ### Demonym -### V3.1 Now you can search by how a citizen is called. ``` html https://restcountries.com/v3.1/demonym/{demonym} @@ -193,17 +129,6 @@ https://restcountries.com/v3.1/demonym/peruvian ``` ## Language -### V2 -Search by ISO 639-1 language code - -``` javascript -https://restcountries.com/v2/lang/{et} -``` -``` html -https://restcountries.com/v2/lang/es -``` - -### V3.1 Search by language code or name ``` html https://restcountries.com/v3.1/lang/{currency} @@ -216,18 +141,8 @@ https://restcountries.com/v3.1/lang/spanish ``` ## Capital city - -_This is the same in both versions_ - Search by capital city -``` javascript -https://restcountries.com/v2/capital/{capital} -``` -``` html -https://restcountries.com/v2/capital/tallinn -``` - ``` javascript https://restcountries.com/v3.1/capital/{capital} ``` @@ -236,65 +151,28 @@ https://restcountries.com/v3.1/capital/tallinn ``` ## Calling code -### V2 -Search by calling code - -``` html -https://restcountries.com/v2/callingcode/{callingcode} -``` -``` html -https://restcountries.com/v2/callingcode/372 -``` -### V3.1 In version 3, calling codes are in the _idd_ object. There is no implementation to search by calling codes in V3. ## Region -### V2 - V3 & V3.1 Search by region (replace X with the version you want to use) ``` html -https://restcountries.com/vX/region/{region} +https://restcountries.com/v3.1/region/{region} ``` ``` html -https://restcountries.com/vX/region/europe +https://restcountries.com/v3.1/region/europe ``` ## Subregions -### V2 - V3 & V3.1 You can search by subregions (replace X with the version you want to use) ``` html -https://restcountries.com/vX/subregion/{subregion} +https://restcountries.com/v3.1/subregion/{subregion} ``` ``` html -https://restcountries.com/vX/subregion/Northern Europe +https://restcountries.com/v3.1/subregion/Northern Europe ``` -## Regional Bloc (*Version 2 only*) - -Search by regional bloc: - -- EU (European Union) -- EFTA (European Free Trade Association) -- CARICOM (Caribbean Community) -- PA (Pacific Alliance) -- AU (African Union) -- USAN (Union of South American Nations) -- EEU (Eurasian Economic Union) -- AL (Arab League) -- ASEAN (Association of Southeast Asian Nations) -- CAIS (Central American Integration System) -- CEFTA (Central European Free Trade Agreement) -- NAFTA (North American Free Trade Agreement) -- SAARC (South Asian Association for Regional Cooperation) - -``` html -https://restcountries.com/v2/regionalbloc/{regionalbloc} -``` -``` html -https://restcountries.com/v2/regionalbloc/eu -``` - -## Translation (*V3.x only*) +## Translation You can search by any translation name ``` html https://restcountries.com/v3.1/translation/{translation} @@ -309,76 +187,15 @@ https://restcountries.com/v3.1/translation/alemania https://restcountries.com/v3.1/translation/Saksamaa ``` -## Response Example - -``` html -https://restcountries.com/v2/alpha/col -``` - -``` json -[[{ - "name": "Colombia", - "topLevelDomain": [".co"], - "alpha2Code": "CO", - "alpha3Code": "COL", - "callingCodes": ["57"], - "capital": "Bogotá", - "altSpellings": ["CO", "Republic of Colombia", "República de Colombia"], - "region": "Americas", - "subregion": "South America", - "population": 48759958, - "latlng": [4.0, -72.0], - "demonym": "Colombian", - "area": 1141748.0, - "gini": 55.9, - "timezones": ["UTC-05:00"], - "borders": ["BRA", "ECU", "PAN", "PER", "VEN"], - "nativeName": "Colombia", - "numericCode": "170", - "currencies": [{ - "code": "COP", - "name": "Colombian peso", - "symbol": "$" - }], - "languages": [{ - "iso639_1": "es", - "iso639_2": "spa", - "name": "Spanish", - "nativeName": "Español" - }], - "translations": { - "de": "Kolumbien", - "es": "Colombia", - "fr": "Colombie", - "ja": "コロンビア", - "it": "Colombia", - "br": "Colômbia", - "pt": "Colômbia" - }, - "flag": "https://restcountries.com/data/col.svg", - "regionalBlocs": [{ - "acronym": "PA", - "name": "Pacific Alliance", - "otherAcronyms": [], - "otherNames": ["Alianza del Pacífico"] - }, { - "acronym": "USAN", - "name": "Union of South American Nations", - "otherAcronyms": ["UNASUR", "UNASUL", "UZAN"], - "otherNames": ["Unión de Naciones Suramericanas", "União de Nações Sul-Americanas", "Unie van Zuid-Amerikaanse Naties", "South American Union"] - }] -}] -``` - ## Filter Response You can filter the output of your request to include only the specified fields. ``` javascript -https://restcountries.com/v2/{service}?fields={field},{field},{field} +https://restcountries.com/v3.1/{service}?fields={field},{field},{field} ``` ``` html -https://restcountries.com/v2/all?fields=name,capital,currencies +https://restcountries.com/v3.1/all?fields=name,capital,currencies ``` ## Similar projects diff --git a/src/main/java/dev/amatos/restcountries/controller/ControllerV3Helper.java b/src/main/java/dev/amatos/restcountries/controller/ControllerV3Helper.java index be4ca7f..d0d82a6 100644 --- a/src/main/java/dev/amatos/restcountries/controller/ControllerV3Helper.java +++ b/src/main/java/dev/amatos/restcountries/controller/ControllerV3Helper.java @@ -119,6 +119,7 @@ public class ControllerV3Helper { "startOfWeek", "capitalInfo", "postalCode", - "capital" + "capital", + "altSpellings" }; } diff --git a/src/main/java/dev/amatos/restcountries/controller/CountryControllerV31.java b/src/main/java/dev/amatos/restcountries/controller/CountryControllerV31.java index fdfdee5..6a6da33 100644 --- a/src/main/java/dev/amatos/restcountries/controller/CountryControllerV31.java +++ b/src/main/java/dev/amatos/restcountries/controller/CountryControllerV31.java @@ -183,4 +183,22 @@ public class CountryControllerV31 extends ControllerV3Helper { return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR); } } + + @Get("independent") + @Schema(name = "RestCountries") + public Object getIndependentCountries(@QueryParam("status") Optional status, @QueryParam("fields") Optional fields) { + try { + var result = true; + if(status.isPresent()) { + result = Boolean.TRUE.equals(status.get()); + } + var countries = CountryServiceV31.getInstance().getIndependent(result); + if (!countries.isEmpty()) { + return ControllerHelper.ok(checkFieldsAndParseCountries(fields, countries)); + } + return ControllerHelper.notFound(); + } catch (Exception e) { + return HttpResponse.serverError(Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/src/main/java/dev/amatos/restcountries/service/v3/v31/CountryServiceV31.java b/src/main/java/dev/amatos/restcountries/service/v3/v31/CountryServiceV31.java index 1f1eb4d..d88586f 100644 --- a/src/main/java/dev/amatos/restcountries/service/v3/v31/CountryServiceV31.java +++ b/src/main/java/dev/amatos/restcountries/service/v3/v31/CountryServiceV31.java @@ -7,7 +7,9 @@ import dev.amatos.restcountries.domain.v3.v31.Country; import dev.amatos.restcountries.service.v3.CountryServiceBaseV3; import java.text.Normalizer; import java.util.HashSet; +import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; public class CountryServiceV31 extends CountryServiceBaseV3 { @@ -78,6 +80,13 @@ public class CountryServiceV31 extends CountryServiceBaseV3 { return result; } + public Set getIndependent(boolean status) { + return countries.stream().filter(country -> { + var independent = Boolean.TRUE.equals(country.getIndependent()); + return independent == status; + }).collect(Collectors.toSet()); + } + @Override protected String normalize(String string) { return Normalizer.normalize(string, Normalizer.Form.NFD) diff --git a/src/main/resources/countriesV3.1.json b/src/main/resources/countriesV3.1.json index d7f6a91..1213962 100644 --- a/src/main/resources/countriesV3.1.json +++ b/src/main/resources/countriesV3.1.json @@ -3140,7 +3140,7 @@ "Europe" ], "languages": { - "bar": "Austro-Bavarian German" + "de": "German" }, "translations": { "ara": {