View Source Apple.WeatherKit (apple_weather_kit v0.5.0)

A client for Apple's WeatherKit REST API.

Quick Start

Before using Apple.WeatherKit, you need a basic understanding of the WeatherKit REST API. Checkout WeatherKit REST API.

Then, you can start using this package:

# 1. build the config
config =
  Apple.WeatherKit.Config.new!(
    team_id: "XXXXXXXXXX",
    service_id: "com.example.weatherkit-client",
    key_id: "YYYYYYYYYY",
    private_key: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"
  )

# 2. make the request
Apple.WeatherKit.current_weather(config, 27.637, 120.699)

# It returns:
#
# {:ok,
#  %{
#    "current_weather" => %{
#      "as_of" => "2024-02-25T09:24:38Z",
#      "cloud_cover" => 0.39,
#      "cloud_cover_high_alt_pct" => 0.0,
#      "cloud_cover_low_alt_pct" => 0.6,
#      # ...
#    }
#  }
# }

You may notice that Apple.WeatherKit convert the keys in response to snake-cased keys, which is used in Elixir community conventionally.

All possible values of conditionCode?

WeatherKit REST API doesn't provide a list of all possible conditionCode, but WeatherKit Swift API does.

Please check out Apple.WeatherKit.Condition for more information.

References

Summary

Types

The ISO Alpha-2 country code for the requested location, like "US".

Available data sets.

The data sets to include in the response for batch request.

The name of language, like "en", "zh-CN", "zh-HK".

The latitude of the requested location.

The longitude of the requested location.

Functions

Obtains attribution information.

Obtains the list of data sets available for the requested location.

Obtains the current weather for the requested location.

Obtains the daily forecast for the requested location.

Obtains the hourly forecast for the requested location.

Obtains the next hour forecast for the requested location.

Obtains weather alerts for the requested location.

Obtains data sets for the requested location in batch.

Types

@type country_code() :: String.t()

The ISO Alpha-2 country code for the requested location, like "US".

@type data_set() :: String.t()

Available data sets.

It can be:

  • "currentWeather"
  • "forecastDaily"
  • "forecastHourly"
  • "forecastNextHour"
  • "weatherAlerts"
@type data_sets() :: [data_set(), ...]

The data sets to include in the response for batch request.

@type language() :: String.t()

The name of language, like "en", "zh-CN", "zh-HK".

@type latitude() :: number()

The latitude of the requested location.

The value should be between -90 and 90.

@type longitude() :: number()

The longitude of the requested location.

The value should be between -180 and 180.

@type result() :: {:ok, map()} | {:error, Exception.t()}

Functions

Link to this function

attribution(config, language)

View Source
@spec attribution(Apple.WeatherKit.Config.t(), language()) :: result()

Obtains attribution information.

Link to this function

available_data_sets(config, latitude, longitude)

View Source
@spec available_data_sets(Apple.WeatherKit.Config.t(), latitude(), longitude()) ::
  result()

Obtains the list of data sets available for the requested location.

When country_code argument is provided, the data sets of air quality and weather alerts will be taken into consideration, or they are will be always ignored from the list.

Link to this function

available_data_sets(config, latitude, longitude, country_code)

View Source
@spec available_data_sets(
  Apple.WeatherKit.Config.t(),
  latitude(),
  longitude(),
  country_code()
) ::
  result()
Link to this function

current_weather(config, latitude, longitude, opts \\ [])

View Source
@spec current_weather(Apple.WeatherKit.Config.t(), latitude(), longitude(), keyword()) ::
  result()

Obtains the current weather for the requested location.

Options

  • language - the name of language. Default to "en".
  • current_as_of - the UTC datetime string to obtain current conditions. Default to UTC datatime string of now, like "2024-02-15T19:23:45Z".
Link to this function

forecast_daily(config, latitude, longitude, opts \\ [])

View Source
@spec forecast_daily(Apple.WeatherKit.Config.t(), latitude(), longitude(), keyword()) ::
  result()

Obtains the daily forecast for the requested location.

Options

  • language - the name of language. Default to "en".
  • timezone - the name of timezone which is use for rolling up weather forecasts into daily forecasts.Default to "Etc/UTC".
  • daily_start - The UTC datetime string whose day will be used to start the daily forecast. Default to UTC datetime string of now, like "2024-02-15T19:23:45Z".
  • daily_end - The UTC datetime string whose day will be used to end the daily forecast. Default to UTC datetime string of now plus 10 days, like "2024-02-25T19:23:45Z".
Link to this function

forecast_hourly(config, latitude, longitude, opts \\ [])

View Source
@spec forecast_hourly(Apple.WeatherKit.Config.t(), latitude(), longitude(), keyword()) ::
  result()

Obtains the hourly forecast for the requested location.

Options

  • language - the name of language. Default to "en".
  • hourly_start - The UTC datetime string whose hour will be used to start the hourly forcast.Default to UTC datetime string of now, like "2024-02-15T19:23:45Z".
  • hourly_end - The UTC datetime string whose hour will be used to end the hourly forcast.Default to UTC datetime string of 24 hours or the length of the daily forecast, whichever is longer, like "2024-02-16T19:23:45Z".
Link to this function

forecast_next_hour(config, latitude, longitude, opts \\ [])

View Source
@spec forecast_next_hour(
  Apple.WeatherKit.Config.t(),
  latitude(),
  longitude(),
  keyword()
) :: result()

Obtains the next hour forecast for the requested location.

Options

  • language - the name of language. Default to "en".
  • current_as_of - the UTC datetime string to obtain current conditions. Default to UTC datatime string of now, like "2024-02-15T19:23:45Z".
Link to this macro

is_latitude(value)

View Source (macro)
Link to this macro

is_longitude(value)

View Source (macro)
Link to this function

weather_alerts(config, latitude, longitude, country_code, opts \\ [])

View Source
@spec weather_alerts(
  Apple.WeatherKit.Config.t(),
  latitude(),
  longitude(),
  country_code(),
  keyword()
) ::
  result()

Obtains weather alerts for the requested location.

Options

  • language - the name of language. Default to "en".
  • timezone - the name of timezone which is use for rolling up weather forecasts into daily forecasts.Default to "Etc/UTC".
Link to this function

weather_batch(config, latitude, longitude, data_sets, opts \\ [])

View Source
@spec weather_batch(
  Apple.WeatherKit.Config.t(),
  latitude(),
  longitude(),
  data_sets(),
  keyword()
) ::
  result()

Obtains data sets for the requested location in batch.

Options

  • language - the name of language. Default to "en".
  • timezone - the name of timezone which is use for rolling up weather forecasts into daily forecasts.Default to "Etc/UTC".
  • current_as_of - the UTC datetime string to obtain current conditions. Default to UTC datatime string of now, like "2024-02-15T19:23:45Z".
  • daily_start - The UTC datetime string whose day will be used to start the daily forecast. Default to UTC datetime string of now, like "2024-02-15T19:23:45Z".
  • daily_end - The UTC datetime string whose day will be used to end the daily forecast. Default to UTC datetime string of now plus 10 days, like "2024-02-25T19:23:45Z".
  • hourly_start - The UTC datetime string whose hour will be used to start the hourly forcast.Default to UTC datetime string of now, like "2024-02-15T19:23:45Z".
  • hourly_end - The UTC datetime string whose hour will be used to end the hourly forcast.Default to UTC datetime string of 24 hours or the length of the daily forecast, whichever is longer, like "2024-02-16T19:23:45Z".
  • country_code - The ISO Alpha-2 country code for the requested location, like "US".

Examples

Apple.WeatherKit.weather_batch(config, 27.637, 120.699, ["currentWeather", "forecastDaily", "forecastHourly"])