Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
assignment1
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
Mathilde Hertaas
assignment1
Commits
30f29385
Commit
30f29385
authored
4 months ago
by
Mathilde Hertaas
Browse files
Options
Downloads
Plain Diff
Merge to main, conflicts resolved
parents
b237e7e1
3ea7d30f
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
main.go
+73
-14
73 additions, 14 deletions
main.go
with
73 additions
and
14 deletions
main.go
+
73
−
14
View file @
30f29385
...
...
@@ -6,6 +6,7 @@ import (
"net/http"
"time"
"strings"
"strconv"
)
var
serviceStartTime
time
.
Time
...
...
@@ -73,6 +74,9 @@ type CountryInfo struct {
Borders
[]
string
`json:"borders"`
Population
int
`json:"population"`
Flags
Flags
`json:"flags"`
}
type
CityInfo
struct
{
Cities
[]
string
`json:"cities"`
}
...
...
@@ -114,8 +118,43 @@ func getCountryDataByCode(countryCode string) (*CountryInfo, error) {
return
&
countries
[
0
],
nil
}
func
getCitiesByCountry
(
countryName
string
)
([]
string
,
error
)
{
url
:=
fmt
.
Sprintf
(
"https://countriesnow.space/api/v0.1/countries/cities/q?country=%s"
,
countryName
)
resp
,
err
:=
http
.
Get
(
url
)
if
err
!=
nil
{
return
nil
,
err
}
defer
resp
.
Body
.
Close
()
if
resp
.
StatusCode
!=
http
.
StatusOK
{
return
nil
,
fmt
.
Errorf
(
"error retrieving cities, status code: %d"
,
resp
.
StatusCode
)
}
var
responseBody
map
[
string
]
interface
{}
if
err
:=
json
.
NewDecoder
(
resp
.
Body
)
.
Decode
(
&
responseBody
);
err
!=
nil
{
return
nil
,
err
}
if
cities
,
ok
:=
responseBody
[
"data"
]
.
([]
interface
{});
ok
{
var
cityList
[]
string
for
_
,
city
:=
range
cities
{
if
cityStr
,
ok
:=
city
.
(
string
);
ok
{
cityList
=
append
(
cityList
,
cityStr
)
}
}
// Hvis ingen byer er funnet, returner en feilmelding
if
len
(
cityList
)
==
0
{
fmt
.
Println
(
"No cities found in the response."
)
}
return
cityList
,
nil
}
return
nil
,
fmt
.
Errorf
(
"no cities found for country %s"
,
countryName
)
}
// Handler som kombinerer data fra begge API-er
func
countryInfoHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
parts
:=
strings
.
Split
(
r
.
URL
.
Path
,
"/"
)
if
len
(
parts
)
<
5
{
...
...
@@ -124,15 +163,40 @@ func countryInfoHandler(w http.ResponseWriter, r *http.Request) {
}
countryCode
:=
parts
[
4
]
// Hent data fra
begge
API
-er
// Hent
land
data fra
REST Countries
API
country
,
err
:=
getCountryDataByCode
(
countryCode
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
// Hent antall byer som skal vises fra URL-parameteren (default til 10)
// F.eks. /countryinfo/v1/info/NO?cities=5
cityCount
:=
10
// Standardverdi
cityCountStr
:=
r
.
URL
.
Query
()
.
Get
(
"cities"
)
// Hent "cities" parameteren
if
cityCountStr
!=
""
{
// Prøv å konvertere parameteren til et heltall
if
count
,
err
:=
strconv
.
Atoi
(
cityCountStr
);
err
==
nil
&&
count
>
0
{
cityCount
=
count
}
}
// Hent byene fra countriesnow API
cities
,
err
:=
getCitiesByCountry
(
country
.
Name
.
Common
)
if
err
!=
nil
{
http
.
Error
(
w
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
// Hvis det er flere enn det brukeren ønsker, begrens listen
if
len
(
cities
)
>
cityCount
{
cities
=
cities
[
:
cityCount
]
}
// Feilsøking: Hvis ingen byer ble funnet
if
len
(
cities
)
==
0
{
fmt
.
Printf
(
"No cities found for %s
\n
"
,
country
.
Name
.
Common
)
}
// Bygg kombinert respons
response
:=
CombinedInfo
{
Name
:
country
.
Name
.
Common
,
...
...
@@ -142,19 +206,14 @@ func countryInfoHandler(w http.ResponseWriter, r *http.Request) {
Borders
:
country
.
Borders
,
Flags
:
country
.
Flags
.
PNG
,
Capital
:
""
,
Cities
:
""
,
Cities
:
strings
.
Join
(
cities
,
", "
),
// Legg til de 10 første byene som en kommaseparert liste
}
if
len
(
country
.
Capital
)
>
0
{
response
.
Capital
=
country
.
Capital
[
0
]
}
if
len
(
country
.
Cities
)
>
0
{
response
.
Cities
=
country
.
Cities
[
0
]
}
// Sett Content-Type til application/json
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
}
...
...
@@ -182,8 +241,8 @@ func homeHandler(w http.ResponseWriter, r *http.Request) {
"endpoints"
:
map
[
string
]
interface
{}{
"Get Country Info"
:
map
[
string
]
string
{
"Description"
:
"Get country details including cities, population, and more."
,
"URL Format"
:
"http://localhost:8080/countryinfo/v1/info/{country_code}"
,
"Example"
:
"http://localhost:8080/countryinfo/v1/info/NO"
,
"URL Format"
:
"http://localhost:8080/countryinfo/v1/info/{country_code}
{?limit=10}
"
,
"Example"
:
"http://localhost:8080/countryinfo/v1/info/NO
?cities=5
"
,
},
"Get status info"
:
map
[
string
]
string
{
"Description"
:
"Get information about the API statuses"
,
...
...
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