The hawqal
package is a library that provides a list of countries, states, and cities in the world. It is available in Node.js, Go, and Python.
Functionality
- Get all countries
- Get states by country
- Get cities by country
- Get cities by state
To install Hawqal, run the following command:
go get github.com/CapregSoft/Hawqal-go
Get All Countries
Get All Cities
Get All States
Get State By Country
Get Cities By Country
Get Cities By State
package main
import (
"fmt"
hawqal "gihtub.com/CapregSoft/Hawqal-go"
"log"
)
To get a list of all countries, use the hawqal.GetCountriesData()
function whch returns countries & error.
countries, err := hawqal.GetCountriesData()
if err != nil {
log.Fatalf("Error %v", err)
}
fmt.Print("Country")
for _, country := range countries {
fmt.Printf("ID :: %v - Country :: %v\n", *country.CountryID, *country.CountryName)
}
Success Response
{
Countries : ['Afghanistan', 'Aland Islands', 'Albania', 'Algeria', . . . ]
}
To get a list of all cities, use the hawqal.GetCitiesData()
function whch returns cities & error.
cities, err := hawqal.GetCitiesData()
if err != nil {
log.Fatalf("Error %v", err)
}
fmt.Print("Cities")
for _, city := range cities {
fmt.Printf("City :: %v - Country :: %v \n", *city.CityName, *city.CountryName)
}
Success Response
{
Cities : ['Haripur','Abbotabad','WahCantt','Topi',........]
}
To get a list of all states, use the hawqal.GetStatesData()
function:
states, err := hawqal.GetStatesData()
if err != nil {
log.Fatalf("Error %v", err)
}
fmt.Print("States")
for _, state := range states {
fmt.Printf("State :: %v - Country :: %v \n", *state.StateName, *state.CountryName)
}
Success Response
{
States : ['Alabama', 'Alaska', 'American Samoa', 'Arizona', . . . ]
}
To get a list of states for a specific country, use the GetStatesByCountry
function:
statesByCountry, err := hawqal.GetStatesByCountry(countryName)
if err != nil {
log.Fatalf("Error %v", err)
}
fmt.Printf("states: %v ", statesByCountry)
Success Response
{
States : ['Alabama', 'Alaska', 'American Samoa', 'Arizona', . . . ]
}
To get a list of cities for a specific country, use the GetCitiesByCountryData
function:
citiesByCountry, err := hawqal.GetCitiesByCountryData(citiesCountryName)
if err != nil {
log.Fatalf("Error %v", err)
}
fmt.Print("Cities For Country ", citiesCountryName)
for _, city := range citiesByCountry {
fmt.Printf("City :: %v \n", *city.CityName)
}
Success Response
{
Cities : ['Haripur','Abbotabad','WahCantt','Topi',........]
}
To get a list of cities for a specific state, use the GetCitiesByState
function:
citiesByState, err := hawqal.GetCitiesByState(stateName)
if err != nil {
log.Fatalf("Error %v", err)
}
fmt.Print("Cities For State ", stateName)
for _, city := range citiesByState {
fmt.Printf("City :: %v \n", *city.CityName)
}
Success Response
{
Cities : ['Haripur','Abbotabad','WahCantt','Topi',........]
}
To install Hawqal, run the following command:
npm i @capregsoft/hawqal
Get Countries
Get Cities
Get States
Use Asyc
and await
to get meaningful response
To get a list of all countries, use the getCountries
function:
const worldData = require('@capregsoft/hawqal')
const getData = async () =>{
console.log(await worldData.getCountries());
}
getData();
Success Response
[
'Afghanistan',
'Aland Islands',
'Albania',
'Algeria',
'American Samoa',
'Andorra',
'Angola',
'Anguilla',...
]
To get a list of all states, use the getStates
function:
const worldData = require('@capregsoft/hawqal')
const getStates = async () =>{
console.log(await await index.getStates('pakistan'));
}
getStates();
Success Response
[
'Azad Kashmir',
'Balochistan',
'Federally Administered Tribal Areas',
'Gilgit-Baltistan',
'Islamabad Capital Territory',
'Khyber Pakhtunkhwa',
'Punjab',
'Sindh'
]
To get a list of cities for a specific state, cities or country, use the getCities
parameterized function:
const worldData = require('@capregsoft/hawqal')
const getCities = async () =>{
//get all cities
console.log(await await index.getCities());
//get state cities
console.log(await await index.getCities('','punjab'));
//get country cities
console.log(await await index.getCities('pakistan'));
//get country,state cities
console.log(await await index.getCities('pakistan','punjab'));
}
getCities();
Success Response
[
'Fatehgarh Sahib', 'Firozpur', 'Firozpur District',
'Fazilka', 'Gardhiwala', 'Garhshankar',
'Ghanaur', 'Giddarbaha', 'Gurdaspur',
'Guru Har Sahai', 'Hariana', 'Hoshiarpur',
'Hajipur', 'Jagraon', 'Jaito',
]
Error Response
[]
Db connection Error!!!
Python
To install the package in Python, run the following command:
pip install hawqal
Getting a list of Countries
Getting a list of Cities
Getting a list of States
Getting Cities By Country
Getting Cities By State
Getting States by Country
To get a list of all countries, you can use the getCountries()
function.
from hawqal.country import Country
Country.getCountries()
Success Response
['Afghanistan', 'Aland Islands', 'Albania', 'Algeria', . . . ]
To get a list of all Cities, you can use the getCities()
function.
from hawqal.cities import City
City.getCities("countries name", "state")
Success Response
['Haripur','Abbotabad','Topi',........]
To get a list of all States, you can use the getStates()
function.
from hawqal.states import StatesByCountry
StatesByCountry.getStates()
Success Response
['Alabama', 'Alaska', 'American Samoa', 'Arizona', . . . ]
To get a list of all Countries, you can use the CitiesByCountry()
function.
from hawqal.citiesbycountry import CitiesByCountry
CitiesByCountry.getCities("country name")
Success Response
['Haripur','Abbotabad','WahCantt','Topi',........]
To get a list of all Cities By State, you can use the CitiesByCountry()
function.
from hawqal.cities import City
City.getCities("", "state")
Success Response
['Haripur','Abbotabad','WahCantt','Topi',........]
To get a list of all states by country, you can use the CitiesByCountry()
function.
from hawqal.states import StatesByCountry
StatesByCountry.getStates("country name")
Success Response
['Alabama', 'Alaska', 'American Samoa', 'Arizona', . . . ]