Skip to content
Snippets Groups Projects
Commit e8ecb5c0 authored by Abdulhadi Al-Sayed's avatar Abdulhadi Al-Sayed
Browse files

Refined exchangeborder

parent e09987ec
Branches
No related tags found
No related merge requests found
......@@ -84,15 +84,18 @@ func GetNeighbour(countryName string, limit int) (string, error) {
country, err := gocountries.CountriesByName(countryName)
// Extract first country
c := country[0]
// Extract currency code
neighbourAlpha := make([]string, limit)
neighbourAlpha = c.Borders[:]
// Extract border alpha codes
neighbourAlpha := c.Borders[:]
// To avoid indexing out of neighbourAlpha's range
if limit > len(neighbourAlpha) {limit = len(neighbourAlpha)}
// parse neighbour alpha codes and append to API call URL
for i, a := range neighbourAlpha {
if (i != len(neighbourAlpha) - 1) { // If not last element in array
borderURL += a + ";"
} else {
borderURL += a // Avoid appending with ';' at the end
for i:= 0; i < limit; i++ {
if i >= limit {
// Nothing happens if index exceeds limit
} else if i == limit - 1 { // If last element in array
borderURL += neighbourAlpha[i] // Avoid appending with ';' at the end
} else if i < limit { // If not last element in array
borderURL += neighbourAlpha[i] + ";"
}
}
// Using http API for restcountriesAPI because gocountries pckg does not support searching by country code
......@@ -111,7 +114,7 @@ func GetNeighbour(countryName string, limit int) (string, error) {
// Make string value of neighbour country currencies for return
currencyCodes := ""
for i, a := range countries {
if (i != len(countries) - 1) { // If not last element in array
if i != len(countries) - 1 { // If not last element in array
currencyCodes += a.Currencies[0].Code + ","
} else {
currencyCodes += a.Currencies[0].Code // Avoid appending with ',' at the end
......
......@@ -30,22 +30,25 @@ func GetExchangeData(startDate, endDate, currencyCode, currencyBase string) (map
if err != nil { // Error handling data
return nil, err
}
return Decode(resData)
return Decode(resData, "date")
} else { // Request for history
// Insert parameters into HISTORYURL for request
resData, err := http.Get(fmt.Sprintf(HISTORYURL, startDate, endDate, currencyCode, currencyBase))
if err != nil { // Error handling data
return nil, err
}
return Decode(resData)
return Decode(resData, "")
}
}
// TODO make this function return a struct instead of a map, no random placement
// of JSON arrays and keys like map handles it
// Decode returns a decoded map from a decoded JSON
func Decode(data *http.Response) (map[string]interface{}, error) {
/*
Decode returns a decoded map from a decoded JSON
* Optional removal of a key in decoded map
*/
func Decode(data *http.Response, filter string) (map[string]interface{}, error) {
var result = make(map[string]interface{}) // Body object
defer data.Body.Close() // Closing body after finishing read
......@@ -58,5 +61,14 @@ func Decode(data *http.Response) (map[string]interface{}, error) {
if err != nil { // Error handling decoding
return nil, err
}
// Optional filtering of certain key in map
if filter != "" {
// Check for filter word existence
_, ok := result[filter];
if ok {
delete(result, filter);
}
}
// Return map with requested data
return result, err
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment