|
15 | 15 |
|
16 | 16 | from pydantic import BaseModel, ConfigDict, Field |
17 | 17 |
|
| 18 | +from airbyte.cloud._case_conversion import snake_to_camel_keys |
| 19 | + |
18 | 20 |
|
19 | 21 | class StreamDescriptor(BaseModel): |
20 | 22 | """Descriptor for a single stream within a connection state.""" |
@@ -107,6 +109,145 @@ def __str__(self) -> str: |
107 | 109 | ) |
108 | 110 |
|
109 | 111 |
|
| 112 | +def _normalize_state_to_protocol( |
| 113 | + raw_state: dict[str, Any], |
| 114 | +) -> list[dict[str, Any]]: |
| 115 | + """Convert a raw Config API state blob to Airbyte protocol state messages. |
| 116 | +
|
| 117 | + Transforms the camelCase API format into a list of `AirbyteStateMessage` dicts |
| 118 | + with snake_case keys, suitable for passing to a connector's `--state` flag. |
| 119 | +
|
| 120 | + Args: |
| 121 | + raw_state: Full state dict from the Config API, including `stateType`. |
| 122 | +
|
| 123 | + Returns: |
| 124 | + List of protocol-format state message dicts. Empty list if state is `not_set`. |
| 125 | + """ |
| 126 | + parsed = ConnectionStateResponse(**raw_state) |
| 127 | + |
| 128 | + if parsed.state_type == "not_set": |
| 129 | + return [] |
| 130 | + |
| 131 | + if parsed.state_type == "legacy": |
| 132 | + return [{"type": "LEGACY", "data": parsed.state or {}}] |
| 133 | + |
| 134 | + if parsed.state_type == "global" and parsed.global_state: |
| 135 | + return [ |
| 136 | + { |
| 137 | + "type": "GLOBAL", |
| 138 | + "global": parsed.global_state.model_dump(by_alias=False), |
| 139 | + } |
| 140 | + ] |
| 141 | + |
| 142 | + # state_type == "stream" |
| 143 | + if not parsed.stream_state: |
| 144 | + return [] |
| 145 | + |
| 146 | + return [ |
| 147 | + { |
| 148 | + "type": "STREAM", |
| 149 | + "stream": entry.model_dump(by_alias=False), |
| 150 | + } |
| 151 | + for entry in parsed.stream_state |
| 152 | + ] |
| 153 | + |
| 154 | + |
| 155 | +def _stream_entry_to_api(stream_entry: dict[str, Any]) -> dict[str, Any]: |
| 156 | + """Convert a single protocol-format stream entry to Config API format. |
| 157 | +
|
| 158 | + Converts `stream_descriptor` keys to camelCase (via generic helper) and |
| 159 | + preserves the opaque `stream_state` blob as-is. Strips `None` values from |
| 160 | + the descriptor (the API omits null fields like `namespace`). |
| 161 | + """ |
| 162 | + descriptor = stream_entry.get("stream_descriptor", {}) |
| 163 | + api_descriptor = {k: v for k, v in snake_to_camel_keys(descriptor).items() if v is not None} |
| 164 | + return { |
| 165 | + "streamDescriptor": api_descriptor, |
| 166 | + "streamState": stream_entry.get("stream_state"), |
| 167 | + } |
| 168 | + |
| 169 | + |
| 170 | +def _denormalize_protocol_state_to_api( |
| 171 | + protocol_messages: list[dict[str, Any]], |
| 172 | + connection_id: str, |
| 173 | +) -> dict[str, Any]: |
| 174 | + """Convert Airbyte protocol state messages back to Config API format. |
| 175 | +
|
| 176 | + Reverses `_normalize_state_to_protocol`, producing a dict suitable for |
| 177 | + `import_raw_state()` / the Config API `create_or_update_safe` endpoint. |
| 178 | + Uses generic snake_case → camelCase key conversion for stream entries; |
| 179 | + the opaque state blobs are preserved as-is. |
| 180 | +
|
| 181 | + Args: |
| 182 | + protocol_messages: List of protocol-format `AirbyteStateMessage` dicts. |
| 183 | + connection_id: Connection ID to embed in the result. |
| 184 | +
|
| 185 | + Returns: |
| 186 | + A Config API state dict with camelCase keys and `stateType`. |
| 187 | + """ |
| 188 | + if not protocol_messages: |
| 189 | + return { |
| 190 | + "stateType": "not_set", |
| 191 | + "connectionId": connection_id, |
| 192 | + } |
| 193 | + |
| 194 | + first = protocol_messages[0] |
| 195 | + msg_type = first.get("type", "").upper() |
| 196 | + |
| 197 | + if msg_type == "LEGACY": |
| 198 | + return { |
| 199 | + "stateType": "legacy", |
| 200 | + "connectionId": connection_id, |
| 201 | + "state": first.get("data", {}), |
| 202 | + } |
| 203 | + |
| 204 | + if msg_type == "GLOBAL": |
| 205 | + global_body = first.get("global", {}) |
| 206 | + return { |
| 207 | + "stateType": "global", |
| 208 | + "connectionId": connection_id, |
| 209 | + "globalState": { |
| 210 | + "sharedState": global_body.get("shared_state"), |
| 211 | + "streamStates": [ |
| 212 | + _stream_entry_to_api(s) for s in global_body.get("stream_states", []) |
| 213 | + ], |
| 214 | + }, |
| 215 | + } |
| 216 | + |
| 217 | + # STREAM type |
| 218 | + return { |
| 219 | + "stateType": "stream", |
| 220 | + "connectionId": connection_id, |
| 221 | + "streamState": [_stream_entry_to_api(s.get("stream", {})) for s in protocol_messages], |
| 222 | + } |
| 223 | + |
| 224 | + |
| 225 | +def _is_protocol_state_format( |
| 226 | + state_input: dict[str, Any] | list[dict[str, Any]], |
| 227 | +) -> bool: |
| 228 | + """Detect whether the input is in Airbyte protocol format. |
| 229 | +
|
| 230 | + Returns `True` for protocol format: |
| 231 | + - an empty list (representing no protocol state), or |
| 232 | + - a non-empty list where every item is a dict with a known `type`, or |
| 233 | + - a single dict with top-level snake_case `type` and no `stateType`. |
| 234 | +
|
| 235 | + Returns `False` for Config API format (for example, a dict with `stateType` |
| 236 | + or a raw `streamState` list from the Config API). |
| 237 | + """ |
| 238 | + protocol_message_types = {"STREAM", "GLOBAL", "LEGACY"} |
| 239 | + |
| 240 | + if isinstance(state_input, list): |
| 241 | + if not state_input: |
| 242 | + return True |
| 243 | + return all( |
| 244 | + isinstance(message, dict) and message.get("type") in protocol_message_types |
| 245 | + for message in state_input |
| 246 | + ) |
| 247 | + # A dict with snake_case 'type' at top-level is a single protocol message |
| 248 | + return "type" in state_input and "stateType" not in state_input |
| 249 | + |
| 250 | + |
110 | 251 | def _match_stream( |
111 | 252 | stream: StreamState, |
112 | 253 | stream_name: str, |
|
0 commit comments