Skip to content

examples

Mondonno edited this page May 17, 2021 · 2 revisions

Airly.Net Examples

Hello there mate! If you are here maybe you want to see some examples of usage Airly.Net :) or you ust do not know why you are here but no matter, get started.

After start

You maybe sure you have properly using's on top of your program file:

using AirlyNet;
using AirlyNet.Models; // required for using data models

Also you need to define apiKey variable (recommended to have it in env/or to have special config file where are they stored)

required on top of file/method or as the class static variable

readonly string apiKey = "your api key"; 

// or

private static string apiKey = "your api key";

also create after using example the airly client with the previoriusly saved api key :D

Airly airly = new Airly(apiKey);

if in the example is used await keyword and it is used in the void or void child (like task) use async keyword the provided location objects documentation can be found here

Remember if you do not know the csharp basics do not use our library.

Fetch Airly CAQI of the current measurement nearest specified location

In the airly app maybe you are seeing the value of the air healthy, it is named caqi more specified it is airly caqi wich is prepered to the polish standards.

Airly airly = new Airly(apiKey);

Location location = new Location(0, 0);

Measurement measurement = airly.Measurements.Nearest(location);
Index caqiEntrie = measurement.Current.Indexes.Find((e) => e.Name == "AIRLY_CAQI");

double? caqi = caqiEntrie !== null ? caqiEntrie.Value : null;
// remember that caqi can be null because of sensor defect

Fetch Airly CAQI of the current installation nearest specified location

This example will be simmilar as the example up but with some modifications. Remember that this examples takes 2 request to API (installation + measurement request)

Location location = new Location(0, 0);

Installation installation = (await airly.Installations.Nearest(location))[0];
Measurement measurement = await airly.Measurements.Installation(installation);

Index caqiEntrie = measurement.Current.Indexes.Find((e) => e.Name == "AIRLY_CAQI");
double? caqi = caqiEntrie.Value;

How this works? As i said, simmilar to the example up but with some modifications:

1. Added the installation
2. Used the Measurements#Installation() method to get the measurement of the installation
3. Repeated the 2 last lines of the example up

Fetch the Installation info with the redirect (fetching by id)

As we all know you can fetch the installation information by using Installations#Nearest or Installations#Area.
But we are providing some thing wich can help you to interact with api and it is NOT the API feature but only our thing to improve developers to interact with the Airly API.

What it does?
In the offical airly docs page you can find the 301 Permently replaced error wich indicates that the installation get permently replaced to the new location. We are introducing the thing called redirect, wich if the error is thrown making the another request to the correct url.

What if the another location is replaced?
You may ask yourselfes what happen when the another location is replaced. The chance when it will happen is 0,000001% but it is so we explain it here. If the another location is replaced to prevent the infinity request loop and the ratelimit reach we are throwing the error to user, and the user now can do what he/she want.

Warning! If the installlation IS replaced you need to remember that the 2 requests will be sent
Remember! You need the airly installation id to use Info() method

int installationId = 0;
Installation installation;

try {
    installation = airly.Installations.Info(installationId, redirect: true);
}catch(Exception ex) {
    // do something with the error
}

// do something with the installation

Meta endpoints operations

There are 2 supported and one unsupported Meta endpoint