Skip to content

Instantly share code, notes, and snippets.

@jnv
Last active July 27, 2023 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnv/7ff937322d05b5d3c70642fccaa272ec to your computer and use it in GitHub Desktop.
Save jnv/7ff937322d05b5d3c70642fccaa272ec to your computer and use it in GitHub Desktop.
{
"name": "openweathermap",
"defaultService": "default",
"services": [
{
"id": "default",
"baseUrl": "https://api.openweathermap.org"
}
],
"securitySchemes": [
{
"id": "apikey",
"type": "apiKey",
"in": "query",
"name": "appid"
}
],
"parameters": []
}
function WeatherForecastRetrieval({input, parameters, services}) {
const getWeatherResult = getWeather(input, services);
const convertTimestampsResult = convertTimestamps(input, services);
return convertTimestampsResult;
}
function getWeather(input, services) {
const url = `${services.default}/data/2.5/onecall`;
const options = {
method: 'GET',
query: {
lat: input.lat,
lon: input.lon,
exclude: input.exclude,
units: input.units,
lang: input.lang,
appid: '{API_KEY}',
},
headers: {
'Content-Type': 'application/json',
},
security: 'apikey',
};
const response = std.unstable.fetch(url, options).response();
const body = response.bodyAuto() ?? {};
if (response.status !== 200) {
const error = {
cod: response.status,
message: response.headers['error-message'] ? response.headers['error-message'][0] : 'Unknown error',
};
throw new std.unstable.MapError(error);
}
const result = {
lat: body.lat,
lon: body.lon,
timezone: body.timezone,
timezone_offset: body.timezone_offset,
current: body.current,
hourly: body.hourly,
daily: body.daily,
};
return result;
}
function convertTimestamps(input, services) {
const url = `${services.default}/data/2.5/onecall`;
const options = {
method: 'GET',
query: {
lat: input.lat,
lon: input.lon,
exclude: input.exclude,
units: input.units,
lang: input.lang,
},
security: 'apikey',
};
const response = std.unstable.fetch(url, options).response();
const body = response.bodyAuto() ?? {};
if (response.status !== 200) {
const error = {
cod: response.status,
message: response.headers['error-message'] ? response.headers['error-message'][0] : 'Unknown error',
};
throw new std.unstable.MapError(error);
}
const convertTimestampToISO = (timestamp) => new Date(timestamp * 1000).toISOString();
const result = {
lat: body.lat,
lon: body.lon,
timezone: body.timezone,
timezone_offset: body.timezone_offset,
current: {
...body.current,
dt: convertTimestampToISO(body.current.dt),
sunrise: convertTimestampToISO(body.current.sunrise),
sunset: convertTimestampToISO(body.current.sunset),
},
hourly: body.hourly.map((hour) => ({
...hour,
dt: convertTimestampToISO(hour.dt),
})),
daily: body.daily.map((day) => ({
...day,
dt: convertTimestampToISO(day.dt),
sunrise: convertTimestampToISO(day.sunrise),
sunset: convertTimestampToISO(day.sunset),
moonrise: convertTimestampToISO(day.moonrise),
moonset: convertTimestampToISO(day.moonset),
})),
};
return result;
}
name = "weather-and-forecasting/get-current-forecast"
version = "0.0.0"
"Retrieves current weather data with hourly and daily forecasts, converting UNIX timestamps to ISO dates."
usecase WeatherForecastRetrieval safe {
input {
"Latitude of the location."
lat! number!
"Longitude of the location."
lon! number!
"Exclude some parts of the weather data from the API response."
exclude string!
"Units of measurement."
units string!
"Language of the API response."
lang string!
}
result {
"Latitude"
lat! number!
"Longitude"
lon! number!
timezone! string!
timezone_offset! number!
current {
"""
Date Time in ISO format
"""
dt! string!
"Sunrise time in ISO format"
sunrise! string!
"Sunset time in ISO format"
sunset! string!
"""
Temperature
Units - default: kelvin, metric: Celsius, imperial: Fahrenheit.
"""
temp! number!
"""
Feels Like temperature
This temperature parameter accounts for the human perception of weather. Units - default: kelvin, metric: Celsius, imperial: Fahrenheit.
"""
feels_like! number!
"""
Atmospheric pressure on the sea level, hPa
"""
pressure! number!
"""
Humidity, %
"""
humidity! number!
"""
Atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form. Units – default: kelvin, metric: Celsius, imperial: Fahrenheit.
"""
dew_point! number!
"Current UV index"
uvi! number!
"Cloudiness, %"
clouds! number!
"Average visibility, metres. The maximum value of the visibility is 10km"
visibility! number!
"""
Wind speed
Units - default: metre/sec, metric: metre/sec, imperial: miles/hour.
"""
wind_speed! number!
"Wind direction, degrees (meteorological)"
wind_deg! number!
weather! [{
"Weather condition id"
id! number!
"Group of weather parameters (Rain, Snow, Extreme etc.)"
main! string!
"Weather condition within the group"
description! string!
"Weather icon id"
icon! string!
}!]!
}!
hourly [{
"""
Date Time in ISO format
"""
dt! string!
"""
Temperature
Units - default: kelvin, metric: Celsius, imperial: Fahrenheit.
"""
temp! number!
"""
Feels Like temperature
This temperature parameter accounts for the human perception of weather. Units - default: kelvin, metric: Celsius, imperial: Fahrenheit.
"""
feels_like! number!
"""
Atmospheric pressure on the sea level, hPa
"""
pressure! number!
"""
Humidity, %
"""
humidity! number!
"""
Atmospheric temperature (varying according to pressure and humidity) below which water droplets begin to condense and dew can form. Units – default: kelvin, metric: Celsius, imperial: Fahrenheit.
"""
dew_point! number!
"Current UV index"
uvi! number!
"Cloudiness, %"
clouds! number!
"Average visibility, metres. The maximum value of the visibility is 10km"
visibility! number!
"""
Wind speed
Units - default: metre/sec, metric: metre/sec, imperial: miles/hour.
"""
wind_speed! number!
"Wind direction, degrees (meteorological)"
wind_deg! number!
weather! [{
"Weather condition id"
id! number!
"Group of weather parameters (Rain, Snow, Extreme etc.)"
main! string!
"Weather condition within the group"
description! string!
"Weather icon id"
icon! string!
}!]!
}!]!
daily! [{
"""
Date Time in ISO format
"""
dt! string!
"Sunrise time in ISO format"
sunrise! string!
"Sunset time in ISO format"
sunset! string!
"Moonrise time in ISO format"
moonrise! string!
"Moonset time in ISO format"
moonset! string!
"""
Moon phase
0 and 1 are 'new moon', 0.25 is 'first quarter moon', 0.5 is 'full moon' and 0.75 is 'last quarter moon'. The periods in between are called 'waxing crescent', 'waxing gibous', 'waning gibous', and 'waning crescent', respectively.
"""
moon_phase! number!
"Human-readable description of the weather conditions for the day"
summary! string!
temp! {
"Day temperature"
day! number!
"Min daily temperature"
min! number!
"Max daily temperature"
max! number!
"Night temperature"
night! number!
"Evening temperature"
eve! number!
"Morning temperature"
morn! number!
}!
feels_like! {
day! number!
night! number!
eve! number!
morn! number!
}!
pressure! number!
humidity! number!
dew_point! number!
wind_speed! number!
wind_deg! number!
weather! [{
id! number!
main! string!
description! string!
icon! string!
}!]!
clouds! number!
"""
Probability of precipitation
The values of the parameter vary between 0 and 1, where 0 is equal to 0%, 1 is equal to 100%
"""
pop! number!
uvi! number!
}!]!
}!
error {
"Error code."
cod! number!
"Error message."
message! string!
}!
example InputExample {
input {
lat = 51.5074,
lon = -0.1278,
exclude = 'minutely,alerts',
units = 'metric',
lang = 'en',
}
result {
lat = 0,
lon = 0,
timezone = 'placeholder',
current = {
dt = 'placeholder',
sunrise = 'placeholder',
sunset = 'placeholder',
temp = 0,
feels_like = 0,
dew_point = 0,
uvi = 0,
wind_speed = 0,
weather = [
{
main = 'placeholder',
description = 'placeholder',
icon = 'placeholder',
}
],
},
hourly = [
{
dt = 'placeholder',
temp = 0,
feels_like = 0,
dew_point = 0,
uvi = 0,
wind_speed = 0,
weather = [
{
main = 'placeholder',
description = 'placeholder',
icon = 'placeholder',
}
],
}
],
daily = [
{
dt = 'placeholder',
sunrise = 'placeholder',
sunset = 'placeholder',
temp = {
day = 0,
min = 0,
max = 0,
night = 0,
eve = 0,
morn = 0,
},
feels_like = {
day = 0,
night = 0,
eve = 0,
morn = 0,
},
dew_point = 0,
wind_speed = 0,
weather = [
{
main = 'placeholder',
description = 'placeholder',
icon = 'placeholder',
}
],
pop = 0,
uvi = 0,
}
],
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment