Documentation ¶
Overview ¶
Package gocorona is a Golang client library for accessing global coronavirus (COVID-19, SARS-CoV-2) outbreak data. It consumes data from Coronavirus Tracker API.
You can read the API server documentation at https://github.com/ExpDev07/coronavirus-tracker-api.
Usage:
create a new instance of Client, then use the various methods on the client to access different parts of the API. For demonstration:
package main import ( "context" "fmt" "log" "github.com/itsksaurabh/go-corona" ) func main() { // client for accessing different endpoints of the API client := gocorona.Client{} ctx := context.Background() // GetLatestData returns total amonut confirmed cases, deaths, and recoveries. data, err := client.GetLatestData(ctx) if err != nil { log.Fatal("request failed:", err) } fmt.Println(data) }
Notes: * Using the [https://godoc.org/context](https://godoc.org/context) package for passing context. * Look at tests(*_test.go) files for more sample usage.
Index ¶
- Constants
- func WithCtx(ctx context.Context, req *http.Request) *http.Request
- type CaseCountWithTimestamp
- type Client
- func (c Client) Do(req *http.Request, target interface{}) error
- func (c Client) GetAllLocationData(ctx context.Context, timelines bool) (data Locations, err error)
- func (c Client) GetDataByCountryCode(ctx context.Context, countryCode string, timelines bool) (data Locations, err error)
- func (c Client) GetDataByLocationID(ctx context.Context, id int, timelines bool) (data LocationData, err error)
- func (c Client) GetLatestData(ctx context.Context) (data LatestData, err error)
- type Coordinates
- type ErrAPI
- type Latest
- type LatestData
- type LatestWithTimeline
- type Location
- type LocationData
- type Locations
- type Timeline
- type Timelines
Constants ¶
const (
// DefaultBaseURL is the default server URL.
DefaultBaseURL = "https://coronavirus-tracker-api.herokuapp.com/v2"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CaseCountWithTimestamp ¶
CaseCountWithTimestamp holds timestamp with CaseCount
type Client ¶
type Client struct { // HTTPClient is a reusable http client instance. HTTP *http.Client // BaseURL is the REST endpoints URL of the api server BaseURL *url.URL }
Client for accessing different endpoints of the API
func (Client) GetAllLocationData ¶
GetAllLocationData returns all cases from all locations. You can Exclude/Include timelines. Timelines are excluded by default.
func (Client) GetDataByCountryCode ¶
func (c Client) GetDataByCountryCode(ctx context.Context, countryCode string, timelines bool) (data Locations, err error)
GetDataByCountryCode returns all cases from different locations of a country by it's Country Code. Check alpha-2 country codes here: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
func (Client) GetDataByLocationID ¶
func (c Client) GetDataByLocationID(ctx context.Context, id int, timelines bool) (data LocationData, err error)
GetDataByLocationID returns data of a specific location by it's ID. You can Exclude/Include timelines. Timelines are excluded by default. You can Exclude/Include timelines. Timelines are excluded by default.
func (Client) GetLatestData ¶
func (c Client) GetLatestData(ctx context.Context) (data LatestData, err error)
GetLatestData returns total amonut confirmed cases, deaths, and recoveries.
type Coordinates ¶
Coordinates hols coordinates of a location
type Latest ¶
type Latest struct { Confirmed int `json:"confirmed"` Deaths int `json:"deaths"` Recovered int `json:"recovered"` }
Latest holds fields related to latest data
type LatestData ¶
type LatestData struct {
Data Latest `json:"latest"`
}
LatestData holds response from the endpoint /v2/latest
type LatestWithTimeline ¶
LatestWithTimeline struct holds latest count with timelines
func (*LatestWithTimeline) UnmarshalJSON ¶
func (t *LatestWithTimeline) UnmarshalJSON(data []byte) error
UnmarshalJSON implements the json.Unmarshaler interface. coverts json keys from string to unix timestamp and it's values as case count.
type Location ¶
type Location struct { Coordinates Coordinates `json:"coordinates"` Country string `json:"country"` CountryCode string `json:"country_code"` ID int `json:"id"` Latest Latest `json:"latest"` Province string `json:"province"` Timelines Timelines `json:"timelines,omitempty"` }
Location holds data of a location
type LocationData ¶
type LocationData struct {
Location Location `json:"location"`
}
LocationData holds data of a particular location
type Locations ¶
type Locations struct {
Locations []Location `json:"locations"`
}
Locations holds response from endpoint /v2/locations
type Timeline ¶
type Timeline struct {
Data []CaseCountWithTimestamp
}
Timeline holds list of Case Counts with timestamp
type Timelines ¶
type Timelines struct { Confirmed LatestWithTimeline `json:"confirmed"` Deaths LatestWithTimeline `json:"deaths"` Recovered LatestWithTimeline `json:"recovered"` }
Timelines holds latest data with timelines