Skip to content
Snippets Groups Projects
Commit 2b7cb383 authored by Marius Nersveen's avatar Marius Nersveen
Browse files

/energy/v1/renewables/current/ now accepts the optional parameter "{country}?neighbours=true"

parent 2f09c9f3
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@ import (
"Assignment02/handlers"
"Assignment02/utils"
"encoding/csv"
"encoding/json"
"fmt"
"log"
"net/http"
......@@ -14,6 +15,42 @@ func main() {
serverState := utils.ServerState{}
NewRequest, err := http.NewRequest(http.MethodGet, utils.REST_COUNTRIES_URL, nil)
if err != nil {
fmt.Errorf("Error in creating request:", err.Error())
}
// Setting content type -> effect depends on the service provider
NewRequest.Header.Add("content-type", "application/json")
client := &http.Client{}
defer client.CloseIdleConnections()
res, err := client.Do(NewRequest)
if err != nil {
fmt.Errorf("Error in response:", err.Error())
}
// Instantiate decoder
decoder := json.NewDecoder(res.Body)
// Prepare empty struct to populate
RESTCountries := []utils.RESTCountries{}
// Decode uni instance --> Alternative: "err := json.NewDecoder(r.Body).Decode(&uni)"
err = decoder.Decode(&RESTCountries)
if err != nil {
// Note: more often than not is this error due to client-side input, rather than server-side issues
fmt.Println("Error during decoding: ", err.Error())
return
}
for _, line := range RESTCountries {
utils.RestCountriesDataset = append(utils.RestCountriesDataset, line)
}
// Flat printing
//fmt.Println(utils.RestCountriesDataset)
// Open the CSV file
fd, err := os.Open(utils.CSVFilePath)
if err != nil {
......
......@@ -11,6 +11,27 @@ import (
)
func createRenewableEnergyDatasetTask1(data [][]string, country string) []utils.RenewableEnergy {
optionalParameter := strings.Split(country, "?")
var neighbours bool = false
if len(optionalParameter) > 1 {
country = optionalParameter[0]
if optionalParameter[1] == "neighbours=true" {
neighbours = true
}
}
// Prepare empty array to populate
var borders []string
// Populate the array
for _, line := range utils.RestCountriesDataset {
if strings.EqualFold(line.Name.Common, country) {
borders = line.Borders
}
}
// convert csv lines to array of structs
var RenewableEnergyDataset []utils.RenewableEnergy
for i, line := range data {
......@@ -25,6 +46,14 @@ func createRenewableEnergyDatasetTask1(data [][]string, country string) []utils.
}
rec.Entity = field
} else if j == 1 {
if neighbours == true {
for _, line := range borders {
if country == "" || strings.EqualFold(field, line) || flag == true {
flag = true
}
}
}
if country == "" || strings.EqualFold(field, country) || flag == true {
flag = true
}
......@@ -50,8 +79,7 @@ func createRenewableEnergyDatasetTask1(data [][]string, country string) []utils.
func HandleGetRequestForCurrentPercentage(w http.ResponseWriter, r *http.Request) {
// Get the country name from the request URL
URLParts := strings.Split(r.URL.String(), "/")
countryName := URLParts[5]
countryName := strings.Split(r.URL.String(), "/")[5]
fmt.Print("Displaying every item ")
if countryName != "" {
......
......@@ -6,4 +6,5 @@ const CURRENT_PATH = "/energy/v1/renewables/current/"
const HISTORY_PATH = "/energy/v1/renewables/history/"
const NOTIFICATIONS_PATH = "/energy/v1/notifications/"
const STATUS_PATH = "/energy/v1/status/"
const REST_COUNTRIES_URL = "http://129.241.150.113:8080/v3.1/all"
const CSV_FILE_PATH = "utils/renewable-share-energy.csv"
......@@ -48,3 +48,17 @@ type RenewableEnergy struct {
Year int `json:"Year"`
Renewables float64 `json:"Renewables (% equivalent primary energy)"`
}
type RESTCountries struct {
Name struct {
Common string `json:"common"`
} `json:"name"`
Borders []string `json:"borders"`
CCA3 string `json:"cca3"`
}
type RESTCountriesNEW struct {
Name string `json:"name"`
Borders []string `json:"borders"`
CCA3 string `json:"cca3"`
}
......@@ -19,3 +19,5 @@ var CSVFilePath = getCSVFilepath()
// "Storage" for the renewable energy data. It is set in the main function
// var RenewableEnergyDataset []RenewableEnergy
var RenewableEnergyDataset [][]string
var RestCountriesDataset []RESTCountries
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment