-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathjson_decoder.py
93 lines (71 loc) · 2.81 KB
/
json_decoder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import logging
from dataclasses import InitVar, dataclass
from gzip import decompress
from typing import Any, Generator, Mapping, MutableMapping
import requests
from airbyte_cdk.sources.declarative.decoders.decoder import Decoder
from orjson import orjson
logger = logging.getLogger("airbyte")
@dataclass
class JsonDecoder(Decoder):
"""
Decoder strategy that returns the json-encoded content of a response, if any.
"""
parameters: InitVar[Mapping[str, Any]]
def is_stream_response(self) -> bool:
return False
def decode(self, response: requests.Response) -> Generator[MutableMapping[str, Any], None, None]:
"""
Given the response is an empty string or an emtpy list, the function will return a generator with an empty mapping.
"""
try:
body_json = response.json()
yield from self.parse_body_json(body_json)
except requests.exceptions.JSONDecodeError:
logger.warning(
f"Response cannot be parsed into json: {response.status_code=}, {response.text=}"
)
yield {}
@staticmethod
def parse_body_json(
body_json: Mapping[str, Any] | list,
) -> Generator[MutableMapping[str, Any], None, None]:
if not isinstance(body_json, list):
body_json = [body_json]
if len(body_json) == 0:
yield {}
else:
yield from body_json
@dataclass
class IterableDecoder(Decoder):
"""
Decoder strategy that returns the string content of the response, if any.
"""
parameters: InitVar[Mapping[str, Any]]
def is_stream_response(self) -> bool:
return True
def decode(self, response: requests.Response) -> Generator[MutableMapping[str, Any], None, None]:
for line in response.iter_lines():
yield {"record": line.decode()}
@dataclass
class JsonlDecoder(Decoder):
"""
Decoder strategy that returns the json-encoded content of the response, if any.
"""
parameters: InitVar[Mapping[str, Any]]
def is_stream_response(self) -> bool:
return True
def decode(self, response: requests.Response) -> Generator[MutableMapping[str, Any], None, None]:
# TODO???: set delimiter? usually it is `\n` but maybe it would be useful to set optional?
# https://github.com/airbytehq/airbyte-internal-issues/issues/8436
for record in response.iter_lines():
yield orjson.loads(record)
@dataclass
class GzipJsonDecoder(JsonDecoder):
encoding: str = "utf-8"
def decode(self, response: requests.Response) -> Generator[MutableMapping[str, Any], None, None]:
raw_string = decompress(response.content).decode(encoding=self.encoding)
yield from self.parse_body_json(orjson.loads(raw_string))