1
0
mirror of synced 2026-01-02 03:02:26 -05:00
Files
airbyte/airbyte-cdk/python/airbyte_cdk/sources/declarative/decoders/json_decoder.py
Denys Davydov 8145be55a3 Low-code CDK: safe get response.json (#18931)
* low-code cdk: safe get response.json

* flake fix
2022-11-04 11:44:51 +02:00

26 lines
692 B
Python

#
# Copyright (c) 2022 Airbyte, Inc., all rights reserved.
#
from dataclasses import InitVar, dataclass
from typing import Any, List, Mapping, Union
import requests
from airbyte_cdk.sources.declarative.decoders.decoder import Decoder
from dataclasses_jsonschema import JsonSchemaMixin
@dataclass
class JsonDecoder(Decoder, JsonSchemaMixin):
"""
Decoder strategy that returns the json-encoded content of a response, if any.
"""
options: InitVar[Mapping[str, Any]]
def decode(self, response: requests.Response) -> Union[Mapping[str, Any], List]:
try:
return response.json()
except requests.exceptions.JSONDecodeError:
return {}