UKHSA-Internal / coronavirus-dashboard-api-net-sdk

Coronavirus (COVID-19) in the UK - API Service SDK for .Net
https://coronavirus.data.gov.uk/
MIT License
12 stars 4 forks source link
coronavirus covid19 covid19-data covid19-service covid19-uk govuk-service

Coronavirus (COVID-19) in the UK - API Service

Software Development Kit (SDK) for .NET

This is a .NET SDK for the COVID-19 API, as published by Public Health England on Coronavirus (COVID-19) in the UK.

The API supplies the latest data for the COVID-19 outbreak in the United Kingdom.

The endpoint for the data provided using this SDK is:

https://api.coronavirus.data.gov.uk/v1/data

The SDK is also available for Python, R, JavaScript and Elixir.

Pagination

Using this SDK will bypass the pagination process. You will always download the entire dataset unless the latest_by argument is defined.

Installation

.NET Core is required to use this SDK.

To install visit here and download the correct installation for your OS:

Example

We would like to extract the number of new cases England using the API.

We start off by adding the project via NuGet:

dotnet add package UKCovid19

In our application or library we can then instantiate the API by providing filters and a structure: NOTE: The structure key/value object must use one of the valid metrics for the value however the key can match to any property you have on a POCO.

public class CovidData
{
    public DateTime MyDate { get; set; }

    public int NewCases { get; set; }
}

var cov19api = new Cov19Api(new UkCovid19Props
{
    FiltersType = new Dictionary<string, string> { { "areaType", "nation" }, { "areaName", "England" } },
    StructureType = new Dictionary<string, string> { { "MyDate", "date" }, { "newCases", "newCasesByPublishDate" } },
    LatestBy = "newCasesByPublishDate"
});

var data = await cov19api.Get<CovidData>();

foreach (var covidData in data.Data)
{
    Console.WriteLine($"Date:{covidData.MyDate} No. of New Cases:{covidData.NewCases}");
}
LatestBy metric

To get the latest data by a specific metric, you can supply the LatestBy argument to the API:

var cov19api = new Cov19Api(new UkCovid19Props
{
    FiltersType = new Dictionary<string, string> { { "areaType", "nation" }, { "areaName", "England" } },
    StructureType = new Dictionary<string, string> { { "MyDate", "date" }, { "newCases", "newCasesByPublishDate" } },
    LatestBy = "newCasesByPublishDate"
});
Date:08/17/2020 00:00:00 No. of New Cases:634
Xml

You may also use cov19api.GetXml() to return the data into a .NET XDocument object.

Latest Update

To see the timestamp for the last update, run:

var dateTimeOffset = await cov19api.LastUpdate();
Console.WriteLine(dateTimeOffset.ToString("O"));
2020-07-28T15:34:31.000Z
HTTP HEAD

To get an underlying picture of the API you can use the cov19api.Head() method which will return all the headers from the underlying API which will return IEnumerable<KeyValuePair<string, IEnumerable<string>>>.

OpenAPI

If you prefer OpenAPI, you can use cov19api.Options() which will return a OpenApiDocument object that can be inspected.


Developed by Jonathan Channon and maintained by Public Health England.

Copyright (c) 2020, Public Health England.