diff --git a/README.md b/README.md index 677c3ee132a54300c689a098a0228687ebc09b35..af0c7080db6f3f0867d16a7f66778a5328dff180 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,13 @@ http://localhost:8080/countryinfo/v1/population/{country_code}{?startyear-endYea ``` * startYear-endYear er en valfri parameter som gir begreninger på spesifikke år du får populasjonsdata fra (Default er alle tilgjengelige år) * Metode: GET -* Eksempel: http://localhost:8080/population/NO?limit=2010-2015 +* Eksempel: http://localhost:8080/countryinfo/v1/population/NO?limit=2010-2015 ### status Viser status for de eksterne API-ene, versjon og applikasjonens oppetid. ```sh -http://localhost:8080/countryinfo/v1/status +http://localhost:8080/status ``` diff --git a/handelers/constants.go b/handelers/constants.go index 85cf81170caacb5e3f14dd80c3a11e1e685f4518..196e273ffcc39fb61d1125af9d5967dfb5bd4169 100644 --- a/handelers/constants.go +++ b/handelers/constants.go @@ -3,7 +3,7 @@ package handelers //URL const HOMEPAGE_DEFAULT = "http://localhost:8080" const INFO_DEFAULT = "/countryinfo/v1/info/" -const POPULATION_DEFAULT = "/population/" +const POPULATION_DEFAULT = "/countryinfo/v1/population/" const STATUS_DEFAULT = "/status/" const SLASH_DEFAULT = "/" @@ -27,7 +27,7 @@ const STATUS_DESCRIPTION = "Retrieve information about the API status." const STATUS_URL = "http://localhost:8080/status/" const POPULATION_DESCRIPTION = "It requires the ISO 3166-2 country code and a optional limit of the years in the repsonse" const POPULATION_URL = "http://localhost:8080/countryinfo/v1/population/{ISO_3166-2_country_code}{?startYear-endYear}" -const POPULATION_URL_EXAMPLE = "http://localhost:8080/countryinfo/v1/population/NO?2010-2015}" +const POPULATION_URL_EXAMPLE = "http://localhost:8080/countryinfo/v1/population/NO?2010-2015" //status const CNA = "countriesnowapi" diff --git a/handelers/infohandler.go b/handelers/infohandler.go index 7bf229d1ed4385e0c08af0221b053e2b130f5456..68d290702a1959013873d49f476628562a958228 100644 --- a/handelers/infohandler.go +++ b/handelers/infohandler.go @@ -66,7 +66,7 @@ func CountryInfoHandler(w http.ResponseWriter, r *http.Request) { Population: country.Population, Languages: country.Languages, Borders: country.Borders, - Flags: country.Flags.PNG, + Flags: country.Flags, Capital: "", Cities: strings.Join(cities, ", "), } diff --git a/handelers/populasjonhandler.go b/handelers/populasjonhandler.go index afc7abda6e44d6589314169a47d6e37a8ce2a327..581725ebe0134f7cd1721bbc45afb0532f03a74b 100644 --- a/handelers/populasjonhandler.go +++ b/handelers/populasjonhandler.go @@ -9,7 +9,7 @@ import ( "strings" ) -// ISO 3166-1 alpha-2 til alpha-3 landkoder +// ISO 3166-1 alpha-2 to alpha-3 countrycodes var isoAlpha2ToAlpha3 = make(map[string]string) // Henter befolkningsdata fra eksternt API @@ -66,12 +66,12 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula // Filtrer basert på år // en tom liste fyllt med PopulationCount, altså fylles med spesifikke år og antall - var filteredValues []PopulationCount + var filteredValues []PopulationYears var total int count := 0 // itererer over alle verdiene som følger med et land sin populasjonsdata - // over alle årstall-verdi strukturene + // over alle årstall-verdi strukturene for _, stk := range countryData.PopulationCounts { if (startYear == 0 || stk.Year >= startYear) && (endYear == 0 || stk.Year <= endYear) { filteredValues = append(filteredValues, stk) @@ -94,35 +94,31 @@ func getPopulationForCountry(alpha2Code string, startYear, endYear int) (*Popula } func PopulationHandler(w http.ResponseWriter, r *http.Request) { - // r.URL.Path er den fulle URL som ble sendt til serveren altså /population/NO - // deretter fjerner den /population/ så vi sitter igjen med NO - path := strings.TrimPrefix(r.URL.Path, "/population/") - // NO + path := strings.TrimPrefix(r.URL.Path, "/countryinfo/v1/population/") + // NO countryCode := strings.ToUpper(path) // ISO 3166-2-kode - query := r.URL.RawQuery - limit := strings.Replace(query, "limit=","",1) - - - startYear, endYear := 0, 0 - - if len(limit)>0 { - yearRange := strings.Split(limit, "-") - if len(yearRange) == 2 { - startYear, _ = strconv.Atoi(yearRange[0]) - endYear, _ = strconv.Atoi(yearRange[1]) - } else { - fmt.Println("Year range format is incorrect") - } - } - - result, err := getPopulationForCountry(countryCode, startYear, endYear) - if err != nil { - http.Error(w, err.Error(), http.StatusNotFound) - return - } - - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(result) -} \ No newline at end of file + limit := strings.Replace(query, "limit=", "", 1) + + startYear, endYear := 0, 0 + + if len(limit) > 0 { + yearRange := strings.Split(limit, "-") + if len(yearRange) == 2 { + startYear, _ = strconv.Atoi(yearRange[0]) + endYear, _ = strconv.Atoi(yearRange[1]) + } else { + fmt.Println("Year range format is incorrect") + } + } + + result, err := getPopulationForCountry(countryCode, startYear, endYear) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(result) +} diff --git a/handelers/strukter.go b/handelers/strukter.go index c963f925f467e8b59a117bbd1c532e850d378c4c..e052a009a37db336e984ac20da16998ace76c8cf 100644 --- a/handelers/strukter.go +++ b/handelers/strukter.go @@ -1,74 +1,59 @@ package handelers -type Flags struct{ - PNG string `json:"png"` -} - -// Struktur for data fra REST Countries API type CountryInfo struct { Name struct { - Common string `json:"common"` - Official string `json:"official"` - } `json:"name"` - Capital []string `json:"capital"` - Languages map[string]string `json:"languages"` - Continents []string `json:"continents"` - Borders []string `json:"borders"` - Population int `json:"population"` - Flags Flags `json:"flags"` + Common string `json:"common"` + Official string `json:"official"` + } `json:"name"` + Capital []string `json:"capital"` + Languages map[string]string `json:"languages"` + Continents []string `json:"continent"` + Borders []string `json:"borders"` + Population int `json:"population"` + Flags string `json:"flag"` } -//type CityInfo struct { -// Cities []string `json:"cities"` -//} - -// Struktur for kombinert respons i info type CombinedInfo struct { Name string `json:"name"` - Continents []string `json:"continenents"` + Continents []string `json:"continent"` Population int `json:"population"` Languages map[string]string `json:"languages"` Borders []string `json:"borders"` - Flags string `json:"flags"` + Flags string `json:"flag"` Capital string `json:"capital"` - Cities string `json:"cities"` + Cities string `json:"cities"` } -// responsstruktur for befolkningsdata type PopulationResponse struct { - Error bool `json:"error"` - Msg string `json:"msg"` + Error bool `json:"error"` + Msg string `json:"msg"` Data []Country `json:"data"` } -// land og dens befolkningsdata type Country struct { Name string `json:"country"` - Code string `json:"iso3"` // Trebokstavskode (ISO 3166-1 alpha-3) - PopulationCounts []PopulationCount `json:"populationCounts"` + Code string `json:"iso3"` //(ISO 3166-1 alpha-3) + PopulationCounts []PopulationYears `json:"populationCounts"` } - -// befolkningsdate for et bestemt år (brukes i den endelige repsonsen under values) -type PopulationCount struct { +type PopulationYears struct { Year int `json:"year"` Value int `json:"value"` } -// Responsstruktur for vårt API(det som vises på skjerm) type PopulationAPIResponse struct { - Name string `json:"land"` - Gjennomsnitt int `json:"gjennomsnittlig populasjon"` - Values []PopulationCount `json:"årlige populasjoner"` + Name string `json:"country"` + Gjennomsnitt int `json:"average population"` + Values []PopulationYears `json:"yearly population"` } type Response struct { - WelcomeMessage string `json:"welcome"` + WelcomeMessage string `json:"welcome"` Endpoints map[string]Endpoint `json:"endpoints"` } -type Endpoint struct{ +type Endpoint struct { Description string `json:"Description"` URL string `json:"URL"` - Example string `json:"Example,omitempty"` -} \ No newline at end of file + Example string `json:"Example,omitempty"` +}