Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
CountryAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Magnarson Nypan
CountryAPI
Commits
752265ec
Commit
752265ec
authored
6 months ago
by
Andreas Magnarson Nypan
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
8691e33e
No related branches found
No related tags found
No related merge requests found
Pipeline
#30879
canceled
6 months ago
Stage: build
Stage: test
Stage: dast
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/api_fetch.go
+183
-0
183 additions, 0 deletions
utils/api_fetch.go
with
183 additions
and
0 deletions
utils/api_fetch.go
0 → 100644
+
183
−
0
View file @
752265ec
package
utils
import
(
"CountryAPI/models"
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"strings"
)
// fetching the country data using the alpha code to find the my country
func
FetchCountryData
(
countryCode
string
)
(
models
.
CountryInfoStruct
,
error
)
{
url
:=
"http://129.241.150.113:8080/v3.1/alpha/"
+
countryCode
r
,
err
:=
http
.
NewRequest
(
http
.
MethodGet
,
url
,
nil
)
if
err
!=
nil
{
fmt
.
Errorf
(
"Error in creating request:"
,
err
.
Error
())
}
client
:=
&
http
.
Client
{}
res
,
err
:=
client
.
Do
(
r
)
if
err
!=
nil
{
fmt
.
Errorf
(
"Error in response:"
,
err
.
Error
())
}
decoder
:=
json
.
NewDecoder
(
res
.
Body
)
var
countryAPI
[]
map
[
string
]
any
if
err
:=
decoder
.
Decode
(
&
countryAPI
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
country
:=
countryAPI
[
0
]
//countryname
name
:=
""
if
nameData
,
ok
:=
country
[
"name"
]
.
(
map
[
string
]
any
);
ok
{
name
,
_
=
nameData
[
"common"
]
.
(
string
)
}
//continent
var
continents
[]
string
// for all the arrays with strings i do this to extract my strings
if
continentData
,
ok
:=
country
[
"continents"
]
.
([]
any
);
ok
{
for
_
,
v
:=
range
continentData
{
if
con
,
ok
:=
v
.
(
string
);
ok
{
continents
=
append
(
continents
,
con
)
}
}
}
// population
population
:=
int
(
country
[
"population"
]
.
(
float64
))
var
languages
[]
string
if
langList
,
ok
:=
country
[
"languages"
]
.
(
map
[
string
]
any
);
ok
{
for
_
,
v
:=
range
langList
{
if
lan
,
ok
:=
v
.
(
string
);
ok
{
languages
=
append
(
languages
,
lan
)
}
}
}
//borders
var
borders
[]
string
if
borderData
,
ok
:=
country
[
"borders"
]
.
([]
any
);
ok
{
for
_
,
v
:=
range
borderData
{
if
bor
,
ok
:=
v
.
(
string
);
ok
{
borders
=
append
(
borders
,
bor
)
}
}
}
//flag
var
flag
string
if
flagData
,
ok
:=
country
[
"flags"
]
.
(
map
[
string
]
any
);
ok
{
flag
,
_
=
flagData
[
"png"
]
.
(
string
)
}
//capital
var
capital
string
if
capitalData
,
ok
:=
country
[
"capital"
]
.
([]
any
);
ok
&&
len
(
capitalData
)
>
0
{
if
cap
,
ok
:=
capitalData
[
0
]
.
(
string
);
ok
{
capital
=
cap
}
}
return
models
.
CountryInfoStruct
{
Name
:
name
,
Continents
:
continents
,
Population
:
population
,
Languages
:
languages
,
Borders
:
borders
,
Flag
:
flag
,
Capital
:
capital
,
},
nil
}
// fetching cities
func
FetchCities
(
countryName
string
)
([]
string
,
error
)
{
url
:=
"http://129.241.150.113:3500/api/v0.1/countries/cities"
rb
:=
`{"country":"`
+
countryName
+
`"}`
r
,
err
:=
http
.
NewRequest
(
http
.
MethodPost
,
url
,
strings
.
NewReader
(
rb
))
if
err
!=
nil
{
fmt
.
Errorf
(
"Error in creating request:"
,
err
.
Error
())
}
r
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
client
:=
&
http
.
Client
{}
res
,
err
:=
client
.
Do
(
r
)
if
err
!=
nil
{
fmt
.
Errorf
(
"Error in creating response:"
,
err
.
Error
())
}
var
citiesData
struct
{
Data
[]
string
`json:"data"`
}
if
err
:=
json
.
NewDecoder
(
res
.
Body
)
.
Decode
(
&
citiesData
);
err
!=
nil
{
log
.
Fatal
(
err
)
}
return
citiesData
.
Data
,
nil
}
// fetching population data
func
FetchPopulationData
(
countryCode
string
,
startYear
,
endYear
int
)
(
map
[
string
]
any
,
error
)
{
//Using fetchcountrydata to find the country name cus cant use iso2 directrly to look at population stats
countryData
,
err
:=
FetchCountryData
(
countryCode
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error fetching country name:"
,
err
)
}
countryName
:=
countryData
.
Name
url
:=
"http://129.241.150.113:3500/api/v0.1/countries/population"
rb
:=
`{"country":"`
+
countryName
+
`"}`
r
,
err
:=
http
.
NewRequest
(
http
.
MethodPost
,
url
,
strings
.
NewReader
(
rb
))
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error creating request:"
,
err
)
}
r
.
Header
.
Set
(
"Content-Type"
,
"application/json"
)
client
:=
&
http
.
Client
{}
res
,
err
:=
client
.
Do
(
r
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error creating response:"
,
err
)
}
//i did the whole because of alot error checking previously
var
popData
struct
{
Error
bool
`json:"error"`
Msg
string
`json:"msg"`
Data
struct
{
Country
string
`json:"country"`
Code
string
`json:"code"`
Iso3
string
`json:"iso3"`
PopulationCounts
[]
struct
{
Year
int
`json:"year"`
Value
int
`json:"value"`
}
`json:"populationCounts"`
}
`json:"data"`
}
if
err
:=
json
.
NewDecoder
(
res
.
Body
)
.
Decode
(
&
popData
);
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"error decoding JSON: %w"
,
err
)
}
var
filteredValues
[]
map
[
string
]
int
var
totalPopulation
,
count
int
for
_
,
record
:=
range
popData
.
Data
.
PopulationCounts
{
if
(
startYear
==
0
||
record
.
Year
>=
startYear
)
&&
(
endYear
==
0
||
record
.
Year
<=
endYear
)
{
filteredValues
=
append
(
filteredValues
,
map
[
string
]
int
{
"year"
:
record
.
Year
,
"value"
:
record
.
Value
,
})
totalPopulation
+=
record
.
Value
count
++
}
}
meanPopulation
:=
0
if
count
>
0
{
meanPopulation
=
int
(
math
.
Round
(
float64
(
totalPopulation
)
/
float64
(
count
)))
// findding the means
}
return
map
[
string
]
interface
{}{
"mean"
:
meanPopulation
,
"values"
:
filteredValues
,
},
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment