Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from yeboahnanaosei/master
Browse files Browse the repository at this point in the history
Return errors instead of log.Fataln. Add documentation to all exported functions
  • Loading branch information
nf1s authored Apr 17, 2020
2 parents 44f2f42 + 298dff9 commit 7defed3
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 54 deletions.
29 changes: 20 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,27 @@ Go Package to get information regarding the novel corona virus provided by Johns
Full Documentation can be found [here](https://ahmednafies.github.io/go-covid/)

## How to install

go get -u github.com/ahmednafies/go-covid/covid

## How to use

### Get All Data

data := covid.GetData()

data, err := covid.GetData()
if err != nil {
// Handle error here
}
fmt.Println(data)


### Get Status By Country Name

data := covid.GetCountryByName("italy")
data, err := covid.GetCountryByName("italy")
if err != nil {
// Handle error here
}

fmt.Println(data.Attrs.Id)
fmt.Println(data.Attrs.Country)
fmt.Println(data.Attrs.LastUpdate)
Expand All @@ -43,7 +51,10 @@ Full Documentation can be found [here](https://ahmednafies.github.io/go-covid/)

### List Countries

covid.ListCountries()
list, err := covid.ListCountries()
if err != nil {
// Handle error here
}

#### Result

Expand All @@ -61,20 +72,20 @@ Full Documentation can be found [here](https://ahmednafies.github.io/go-covid/)

### Get Status by Country ID

data := covid.GetCountryById(113)
data, err := covid.GetCountryById(113)

### Get Total Active Cases

active := covid.GetTotalActive()
active, err := covid.GetTotalActive()

### Get Total Confirmed Cases

confirmed := covid.GetTotalConfirmed()
confirmed, err := covid.GetTotalConfirmed()

### Get Total Recovered Cases

recovered := covid.GetTotalRecovered()
recovered, err := covid.GetTotalRecovered()

### Get Total Deaths

deaths := covid.GetTotalDeaths()
deaths, err := covid.GetTotalDeaths()
121 changes: 76 additions & 45 deletions covid/covid.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -98,71 +97,82 @@ func getTotalUrl(field string) string {
return _url.String()
}

func GetData() []Case {
// GetData returns the entire data of every country
func GetData() ([]Case, error) {
resp, err := http.Get(getAllUrl())
if err != nil {
log.Fatalln(err)
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return nil, err
}
defer resp.Body.Close()

var res Response
json.Unmarshal(body, &res)
err = json.Unmarshal(body, &res)
if err != nil {
log.Fatalln(err)
return nil, err
}

return res.Values
return res.Values, nil
}

func GetCountryById(id int) Case {
// GetCountryById returns the statistics/data of
// a particular country specified by id
func GetCountryById(id int) (Case, error) {
resp, err := http.Get(getCountryUrl(id))
if err != nil {
log.Fatalln(err)
return Case{}, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return Case{}, err
}
defer resp.Body.Close()

var res Response
json.Unmarshal(body, &res)
err = json.Unmarshal(body, &res)
if err != nil {
log.Fatalln(err)
return Case{}, err
}

return res.Values[0]
return res.Values[0], nil
}

func ListCountries() []Country {
// ListCountries returns a "list" or slice of countries, that shows the id and name
// of a country as understood by this api. This method is handy when you want
// the ID or name of a country to use with methods GetCountryById and GetCountryByName
// respectively
func ListCountries() ([]Country, error) {
resp, err := http.Get(getAllUrl())
if err != nil {
log.Fatalln(err)
return nil, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return nil, err
}
defer resp.Body.Close()

var res CountryResponse
json.Unmarshal(body, &res)
err = json.Unmarshal(body, &res)
if err != nil {
log.Fatalln(err)
return nil, err
}

return res.Values
return res.Values, nil
}

func getCountryId(name string) (int, error) {
countries := ListCountries()
countries, err := ListCountries()
if err != nil {
return -1, err
}

for _, country := range countries {
if strings.ToLower(country.Attrs.Name) == strings.ToLower(name) {
return country.Attrs.Id, nil
Expand All @@ -171,67 +181,88 @@ func getCountryId(name string) (int, error) {
return -1, errors.New("Wrong country name")
}

func GetCountryByName(name string) Case {
// GetCountryByName returns the statistics/data of
// a particular country as supplied by name
func GetCountryByName(name string) (Case, error) {
id, err := getCountryId(name)
if err != nil {
log.Fatalln(err)
return Case{}, err
}

resp, err := http.Get(getCountryUrl(id))
if err != nil {
log.Fatalln(err)
return Case{}, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return Case{}, err
}
defer resp.Body.Close()

var res Response
json.Unmarshal(body, &res)
err = json.Unmarshal(body, &res)
if err != nil {
log.Fatalln(err)
return Case{}, err
}

return res.Values[0]
return res.Values[0], nil
}

func getTotal(field string) Stats {
func getTotal(field string) (Stats, error) {
resp, err := http.Get(getTotalUrl(field))
if err != nil {
log.Fatalln(err)
return Stats{}, err
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
return Stats{}, err
}
defer resp.Body.Close()

var res StatsResponse
json.Unmarshal(body, &res)
err = json.Unmarshal(body, &res)
if err != nil {
log.Fatalln(err)
return Stats{}, err
}

return res.Values[0]
return res.Values[0], nil
}

func GetTotalActive() int {
data := getTotal("Active")
return data.Attrs.Value
// GetTotalActive returns the total number of active cases globally
func GetTotalActive() (int, error) {
data, err := getTotal("Active")
if err != nil {
return -1, err
}
return data.Attrs.Value, nil
}

func GetTotalConfirmed() int {
data := getTotal("Confirmed")
return data.Attrs.Value

// GetTotalConfirmed returns the total number of confirmed cases globally
func GetTotalConfirmed() (int, error) {
data, err := getTotal("Confirmed")
if err != nil {
return -1, err
}
return data.Attrs.Value, err
}
func GetTotalRecovered() int {
data := getTotal("Recovered")
return data.Attrs.Value

// GetTotalRecovered returns the total number of recovered cases globally
func GetTotalRecovered() (int, error) {
data, err := getTotal("Recovered")
if err != nil {
return -1, err
}
return data.Attrs.Value, nil
}
func GetTotalDeaths() int {
data := getTotal("Deaths")
return data.Attrs.Value

// GetTotalDeaths returns the total number of deaths recorded globally
func GetTotalDeaths() (int, error) {
data, err := getTotal("Deaths")
if err != nil {
return -1, err
}
return data.Attrs.Value, nil
}

0 comments on commit 7defed3

Please sign in to comment.