Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 3662: HTTP request source support 'csv type' response #3663

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,42 @@ def discover(self, logger: AirbyteLogger, config: json) -> AirbyteCatalog:
}

# json body will be returned as the "data" stream". we can't know its schema ahead of time, so we assume it's object (i.e. valid json).
return AirbyteCatalog(streams=[AirbyteStream(name=SourceHttpRequest.STREAM_NAME, json_schema=json_schema)])
return AirbyteCatalog(
streams=[
AirbyteStream(
name=SourceHttpRequest.STREAM_NAME, json_schema=json_schema
)
]
)

def read(
self, logger: AirbyteLogger, config: json, catalog: ConfiguredAirbyteCatalog, state: Dict[str, any]
self,
logger: AirbyteLogger,
config: json,
catalog: ConfiguredAirbyteCatalog,
state: Dict[str, any],
) -> Generator[AirbyteMessage, None, None]:
r = self._make_request(config)
if r.status_code != 200:
raise Exception(f"Request failed. {r.text}")

# need to eagerly fetch the json.
data = r.json()

if isinstance(data, list) and isinstance(data[0], list):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should also verify len(data) > 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @sherifnada, I'm actually going to close this PR
I agree with @marcosmarxm that the responses from the US Census API are so unconventional that they would be better supported to a separate "Source"

# found list of list
data = [{"data": records} for records in data]

if not isinstance(data, list):
data = [data]

for record in data:
yield AirbyteMessage(
type=Type.RECORD,
record=AirbyteRecordMessage(
stream=SourceHttpRequest.STREAM_NAME, data=record, emitted_at=int(datetime.now().timestamp()) * 1000
stream=SourceHttpRequest.STREAM_NAME,
data=record,
emitted_at=int(datetime.now().timestamp()) * 1000,
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@

import unittest

from unittest.mock import patch

from source_http_request import SourceHttpRequest


class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code

def json(self):
return self.json_data


class TestSourceHttpRequest(unittest.TestCase):
def test_parse_config(self):
config = {
Expand All @@ -46,3 +57,35 @@ def test_parse_config(self):
"body": {"something": "good"},
}
self.assertEqual(expected, actual)

def test_json_array_response(self):
with patch.object(
SourceHttpRequest,
attribute="_make_request",
return_value=MockResponse(
json_data=[
["foo", "bar"],
["test", 10],
["test2", 15],
],
status_code=200,
),
):
expected = [
{"data": ["foo", "bar"]},
{"data": ["test", "10"]},
{"data": ["test2", "15"]},
]
source = SourceHttpRequest()
results = [
r.record.data
for r in list(
source.read(
logger=None,
state=None,
catalog=None,
config={},
)
)
]
self.assertEqual(expected, results)