weather_demo_helpers

Crates.ioweather_demo_helpers
lib.rsweather_demo_helpers
version0.1.1
sourcesrc
created_at2022-12-08 18:09:02.282284
updated_at2022-12-08 18:35:53.891073
descriptionHelpers for the Fastly Compute@Edge synthetic content tutorial (Weather App)
homepagehttps://developer.fastly.com/solutions/tutorials/synthetic/
repository
max_upload_size
id732736
size8,893
Dora Militaru (doramatadora)

documentation

README

Helpers for Fastly's Compute@Edge Synthetic Content Tutorial

This crate exports a (de)serializable struct ForecastAPIResponse for WeatherAPI's Forecast endpoint response.

The ensuing data structure supports the creation of a Weather Widget during the Synthetic Content Tutorial using Fastly's Compute@Edge.

Use with Response.take_body_json<T: DeserializeOwned>

let url = format!(
    "http://api.weatherapi.com/v1/forecast.json?q={},{}&key={}",
    LATITUDE,
    LONGITUDE,
    MY_API_KEY
);

let mut resp = Request::new(Method::GET, url)
    .with_pass(true)
    .send(BACKEND_NAME)?;

// Parse the JSON response body into an ForecastAPIResponse
let api_response = resp.take_body_json::<ForecastAPIResponse>()?;

Resulting structure

ForecastAPIResponse {
    on: LocationData {
        date: DateElements {
            weekday: "Wednesday",
            weekday_short: "Wed",
            pretty_date: " 7 December 2022",
        },
    },
    current: CurrentWeatherData {
        temp_c: 2.0,
        temp_f: 35.6,
        wind_mph: 6.9,
        wind_kph: 11.2,
        precip_mm: 0.0,
        precip_in: 0.0,
        humidity: 80.0,
        condition: WeatherCondition {
            description: "Clear",
            icon: 1000,
        },
    },
    forecast: ForecastData {
        on: [
            WeatherReport {
                dt: DateElements {
                    weekday: "Wednesday",
                    weekday_short: "Wed",
                    pretty_date: " 7 December 2022",
                },
                day: DayWeatherData {
                    avgtemp_c: 3.5,
                    avgtemp_f: 38.3,
                    condition: WeatherCondition {
                        description: "Partly cloudy",
                        icon: 1003,
                    },
                },
            },
            WeatherReport {
                dt: DateElements {
                    weekday: "Thursday",
                    weekday_short: "Thu",
                    pretty_date: " 8 December 2022",
                },
                day: DayWeatherData {
                    avgtemp_c: 2.0,
                    avgtemp_f: 35.5,
                    condition: WeatherCondition {
                        description: "Sunny",
                        icon: 1000,
                    },
                },
            },
            WeatherReport {
                dt: DateElements {
                    weekday: "Friday",
                    weekday_short: "Fri",
                    pretty_date: " 9 December 2022",
                },
                day: DayWeatherData {
                    avgtemp_c: 1.3,
                    avgtemp_f: 34.4,
                    condition: WeatherCondition {
                        description: "Sunny",
                        icon: 1000,
                    },
                },
            },
        ],
    },
}
Commit count: 0

cargo fmt