gocorona

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 22, 2020 License: MIT Imports: 11 Imported by: 1

README

go-corona

itsksaurabh Go Report Card GoDoc Built with Mage MIT License

go-corona is a Golang client library for accessing global coronavirus (COVID-19, SARS-CoV-2) outbreak data.

API Documentation

It consumes data from Coronavirus Tracker API. You can read the API server documentation here.

Available data sources:

Installation

Make sure you have set the environment variable $GOPATH

export GOPATH="path/to/your/go/folder"

Obtain the latest version of the go-corona library with:

go get github.com/itsksaurabh/go-corona

Then, add the following to your Golang project:

import (
	"github.com/itsksaurabh/go-corona"
)

Usage

Package provides a client for accessing different endpoints of the API. 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:

Error Handling

All errors generated at runtime will be returned to the calling client method. Any API request for which the API returns an error encoded in a JSON response will be parsed and returned by the client method as a Golang error struct. Lastly, it is important to note that for HTTP requests, if the response code returned is not '200 OK', an error will be returned to the client method detailing the response code that was received.

Testing

In order to run the tests for this library, you will first need to install Mage - A Make/rake-like dev tool using Go. You can install the dependency with the following command:

Using GOPATH

go get -u -d github.com/magefile/mage
cd $GOPATH/src/github.com/magefile/mage
go run bootstrap.go

Using Go Modules

git clone https://github.com/magefile/mage
cd mage
go run bootstrap.go

The mage binary will be created in your $GOPATH/bin directory. You may also install a binary release from Mage's releases page.

Then run all tests by executing the following in your command line:

$ mage -v Test

Updating Test Data

You can update the test data inside ./testdata/ by enabling the following flag inside the file gocorona_test.go and then perform testing. By default the flag is set to false.

var (
	updateTestData = flag.Bool(
		"update",
		true,
		"if set then update testdata else use saved testdata for testing.",
	)
)

Contributing

I welcome pull requests, bug fixes and issue reports. Before proposing a change, please discuss your change by raising an issue.

Maintainer

Kumar Saurabh

License

MIT © Kumar Saurabh

Logo Attributions

The project Logo is made using the Go gopher designed by Renee French and the Gopher sticker made by Takuya Ueda. The Go Logo is copyrighted by the Go authors. All of the logo materials are licensed under the Creative Commons 3.0 Attributions license.

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

View Source
const (
	// DefaultBaseURL is the default server URL.
	DefaultBaseURL = "https://coronavirus-tracker-api.herokuapp.com/v2"
)

Variables

This section is empty.

Functions

func WithCtx

func WithCtx(ctx context.Context, req *http.Request) *http.Request

WithCtx applies 'ctx' to the the http.Request and returns *http.Request The provided ctx and req must be non-nil

Types

type CaseCountWithTimestamp

type CaseCountWithTimestamp struct {
	Timestamp time.Time
	CaseCount int
}

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) Do

func (c Client) Do(req *http.Request, target interface{}) error

Do sends the http.Request and unmarshalls the JSON response into 'target'

func (Client) GetAllLocationData

func (c Client) GetAllLocationData(ctx context.Context, timelines bool) (data Locations, err error)

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

type Coordinates struct {
	Latitude  string `json:"latitude"`
	Longitude string `json:"longitude"`
}

Coordinates hols coordinates of a location

type ErrAPI

type ErrAPI struct {
	// Response from the request which returned error.
	Response *http.Response
}

ErrAPI is returned by API calls when the response status code isn't 200.

func (ErrAPI) Error

func (err ErrAPI) Error() (errStr string)

Error implements the error interface.

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

type LatestWithTimeline struct {
	Latest   int      `json:"latest"`
	Timeline Timeline `json:"timeline"`
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL