UNPKG

geography-apis-sdk

Version:

SDK for making requests to Geography APIs

233 lines (214 loc) 10.7 kB
const Country = require('./model/Country.js'); const RestClient = require('./client/RestClient.js'); /** * SDK for making request to Geography API's. */ class GeographyAPISDK { /** * Create a new instance of GeographyAPISDK. * @param {string} apiKey - The API key for authentication. * @param {string} apiHost - The API host. * @param {string} baseURL - The base URL for the API. */ constructor(apiKey, apiHost, baseURL) { this.apiKey = apiKey; this.apiHost = apiHost; this.restClient = new RestClient(baseURL, apiKey, apiHost); } /** * Get all countries. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of all Country objects and headers. * @throws {Error} - If an error occurs during the API call or no countries are found. */ async getAllCountries(options = {}) { try { const endpoint = '/apis/geography/v1/country'; return await this.restClient.call(endpoint, '', options, Country); } catch (error) { throw new Error(`Failed to get all countries : ${error.message}`); } } /** * Search for countries by name. * @param {string} name - The name of the country to search for. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects and headers. * @throws {Error} - If an error occurs during the search. */ async searchCountriesByName(name, options = {}) { try { const endpoint = '/apis/geography/v1/country/name/'; return await this.restClient.call(endpoint, name, options, Country); } catch (error) { throw new Error(`Failed to search countries by name : ${error.message}`); } } /** * Search countries by capital. * @param {string} capital - The capital to search for. * @param {Object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A Promise that resolves to an array of matching countries and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesByCapital(capital, options = {}) { try { const endpoint = '/apis/geography/v1/country/capital/'; return await this.restClient.call(endpoint, capital, options, Country); } catch (error) { throw new Error(`Failed to search countries by capital: ${error.message}`); } } /** * Search for neighboring countries by country code. * @param {string} countryCode - The country code for which to search neighboring countries. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of neighboring Country objects and headers. * @throws {Error} - If an error occurs during the API call. */ async searchNeighboringCountries(countryCode, options = {}) { try { const endpoint = '/apis/geography/v1/country/neighbour/'; return await this.restClient.call(endpoint, countryCode, options, Country); } catch (error) { throw new Error(`Failed to search neighboring countries for given country code: ${error.message}`); } } /** * Search for countries by population threshold. * @param {number} population - The population threshold to search for countries. Use minPopulation and maxPopulation query parameters to input population threshold. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the population criteria and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesByPopulation(options = {}) { try { const endpoint = '/apis/geography/v1/country/population'; return await this.restClient.call(endpoint, '', options, Country); } catch (error) { throw new Error(`Failed to search countries for given population threshold: ${error.message}`); } } /** * Search for countries by language. * @param {string} languageCode - The language code to search for countries (e.g., "en" for English). * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the language criteria and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesByLanguage(languageCode, options = {}) { try { const endpoint = '/apis/geography/v1/country/language/'; return await this.restClient.call(endpoint, languageCode, options, Country); } catch (error) { throw new Error(`Failed to search countries by language : ${error.message}`); } } /** * Search for countries by currency. * @param {string} currencyCode - The currency code to search for countries (e.g., "USD" for US Dollar). * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the currency criteria and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesByCurrency(currencyCode, options = {}) { try { const endpoint = '/apis/geography/v1/country/currency/'; return await this.restClient.call(endpoint, currencyCode, options, Country); } catch (error) { throw new Error(`Failed to search countries by Currency : ${error.message}`); } } /** * Search for countries by continent. * @param {string} continent - The continent to search for countries (e.g., "Asia", "Europe"). * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the continent criteria and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesByContinent(continent, options = {}) { try { const endpoint = '/apis/geography/v1/country/continent/'; return await this.restClient.call(endpoint, continent, options, Country); } catch (error) { throw new Error(`Failed to search countries by continent : ${error.message}`); } } /** * Search for countries by region. * @param {string} region - The region to search for countries (e.g., "Eastern Asia", "Western Europe"). * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the region criteria and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesByRegion(region, options = {}) { try { const endpoint = '/apis/geography/v1/country/region/'; return await this.restClient.call(endpoint, region, options, Country); } catch (error) { throw new Error(`Failed to search countries by region : ${error.message}`); } } /** * Search for countries by subregion. * @param {string} subregion - The subregion to search for countries (e.g., "Northern Europe", "Central Asia"). * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the subregion criteria and headers. * @throws {Error} - If an error occurs during the API call. */ async searchCountriesBySubregion(subregion, options = {}) { try { const endpoint = '/apis/geography/v1/country/subregion/'; return await this.restClient.call(endpoint, subregion, options, Country); } catch (error) { throw new Error(`Failed to search countries by sub-region : ${error.message}`); } } /** * Search for countries by timezone. * @param {string} timezone - The timezone to search for. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country[]>} - A promise that resolves to an array of Country objects matching the timezone criteria and headers. * @throws {Error} - If an error occurs during the API call or no countries are found. */ async searchCountriesByTimezone(timezone, options = {}) { try { const endpoint = '/apis/geography/v1/country/timezone/'; return await this.restClient.call(endpoint, timezone, options, Country); } catch (error) { throw new Error(`Failed to search countries by timezone : ${error.message}`); } } /** * Get a country by FIFA code. * @param {string} fifaCode - The FIFA code of the country to search for. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country>} - A promise that resolves to a Country object matching the FIFA code criteria and headers. * @throws {Error} - If an error occurs during the API call or no country is found. */ async searchCountryByFifaCode(fifaCode, options = {}) { try { const endpoint = '/apis/geography/v1/country/fifacode/'; return await this.restClient.call(endpoint, fifaCode, options, Country); } catch (error) { throw new Error(`Failed to search countries by fifaCode : ${error.message}`); } } /** * Get a country by ISO code. * @param {string} countryCode - The ISO code of the country to retrieve e.g., USA, US. * @param {object} options - Additional options for the API call (e.g., headers, params). * @returns {Promise<Country>} - A promise that resolves to a Country object representing the requested country and headers. * @throws {Error} - If an error occurs during the API call or the country is not found. */ async getCountryByIsoCode(countryCode, options = {}) { try { const endpoint = `/apis/geography/v1/country/`; return await this.restClient.call(endpoint, countryCode, options, Country); } catch (error) { throw new Error(`Failed to search countries by countryCode : ${error.message}`); } } } // Export the SDK for usage in other modules module.exports = GeographyAPISDK;