:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import Optional, cast
from products.warehouse_sources.sources.openweather._config import OpenWeatherSourceConfig
from products.warehouse_sources.sources.openweather.openweather import (
openweather_source,
validate_credentials as validate_openweather_credentials,
)
from products.warehouse_sources.sources.openweather.settings import (
API_VERSION_2_5,
API_VERSION_3_0,
API_VERSION_4_0,
endpoints_for_version,
)
from products.warehouse_sources.sources.sdk import (
CanonicalDescriptions,
DataWarehouseSourceCategory,
ExternalDataSourceType,
FieldType,
ReleaseStatus,
SimpleSource,
SourceConfig,
SourceFieldInputConfig,
SourceFieldInputConfigType,
SourceInputs,
SourceRegistry,
SourceResponse,
SourceSchema,
detect,
)
@SourceRegistry.register
class OpenWeatherSource(SimpleSource[OpenWeatherSourceConfig]):
codebase_detection = detect(env=("^(OPEN_WEATHER|OPENWEATHER)_",), pypi=("pyowm",))
supported_versions = (API_VERSION_2_5, API_VERSION_3_0, API_VERSION_4_0)
default_version = API_VERSION_4_0
api_docs_url = "https://openweathermap.org/api"
@property
def source_type(self) -> ExternalDataSourceType:
return ExternalDataSourceType.OPENWEATHER
@property
def get_source_config(self) -> SourceConfig:
return SourceConfig(
name=ExternalDataSourceType.OPENWEATHER,
category=DataWarehouseSourceCategory.ANALYTICS,
label="OpenWeather",
releaseStatus=ReleaseStatus.BETA,
caption="""Enter your OpenWeather API key and the locations you want to track to pull weather data into the PostHog Data warehouse.
Create an API key in your [OpenWeather account](https://home.openweathermap.org/api_keys). A newly created key can take a couple of hours to activate.
OpenWeather has no list endpoint — every request is for a single coordinate — so enter one location per line as `lat,lon` (an optional label is allowed: `lat,lon,label`). For example:
```
51.5074,-0.1278,London
40.7128,-74.0060,New York
```
Each sync polls every location once. To accumulate a history of point-in-time snapshots, pick the **append** sync method on the table.
""",
iconPath="/static/services/openweather.png",
docsUrl="https://openweathermap.org/api",
fields=cast(
list[FieldType],
[
SourceFieldInputConfig(
name="api_key",
label="API key",
type=SourceFieldInputConfigType.PASSWORD,
required=True,
placeholder="Your OpenWeather API key",
secret=True,
),
SourceFieldInputConfig(
name="locations",
sync_setting=True,
label="Locations",
type=SourceFieldInputConfigType.TEXTAREA,
required=True,
placeholder="51.5074,-0.1278,London\n40.7128,-74.0060,New York",
secret=False,
),
],
),
)
def get_canonical_descriptions(self) -> CanonicalDescriptions:
from products.warehouse_sources.sources.openweather.canonical_descriptions import (
CANONICAL_DESCRIPTIONS, # noqa: PLC0415
)
return CANONICAL_DESCRIPTIONS
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
# A missing/invalid key (or a key not subscribed to the requested product) surfaces as a
# 401 when `_fetch` calls `raise_for_status()`. Retrying can never satisfy it.
"401 Client Error: Unauthorized for url: https://api.openweathermap.org": "Your OpenWeather API key is invalid, not yet activated, or not subscribed to this product. Check the key in your OpenWeather account, then reconnect.",
"401 Client Error: Unauthorized for url: https://history.openweathermap.org": "Your OpenWeather API key is not subscribed to the History API. Disable the weather_history table or upgrade your OpenWeather plan.",
}
def get_schemas(
self,
config: OpenWeatherSourceConfig,
team_id: int,
with_counts: bool = False,
names: list[str] | None = None,
force_refresh: bool = False,
api_version: str | None = None,
) -> list[SourceSchema]:
endpoints = endpoints_for_version(self.resolve_api_version(api_version))
schemas = [
SourceSchema(
name=endpoint,
# Only the history endpoints filter server-side (`start`/`end`), so only they are
# true incremental. Append on the others re-polls the (cheap) snapshot each sync,
# accumulating a time series across runs.
supports_incremental=config.supports_incremental,
supports_append=config.supports_append,
incremental_fields=config.incremental_fields,
should_sync_default=config.should_sync_default,
description=config.description,
)
for endpoint, config in endpoints.items()
]
if names is not None:
names_set = set(names)
schemas = [s for s in schemas if s.name in names_set]
return schemas
def validate_credentials(
self,
config: OpenWeatherSourceConfig,
team_id: int,
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
return validate_openweather_credentials(config.api_key, config.locations, self.resolve_api_version(api_version))
def source_for_pipeline(self, config: OpenWeatherSourceConfig, inputs: SourceInputs) -> SourceResponse:
return openweather_source(
api_key=config.api_key,
endpoint=inputs.schema_name,
locations_raw=config.locations,
logger=inputs.logger,
api_version=self.resolve_api_version(inputs.api_version),
db_incremental_field_last_value=(
inputs.db_incremental_field_last_value if inputs.should_use_incremental_field else None
),
)