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

[low-code] convert request.body to a dict when converting to AirbyteLogMessage #20557

Merged
merged 13 commits into from
Jan 3, 2023

Conversation

girarda
Copy link
Contributor

@girarda girarda commented Dec 16, 2022

What

How

  • If the request body is set as body_json, the string is a json object encoded as a string
  • If the request body is set as body_data, the string is of the form "k1=v1&k2=v"
  • Either, the SimpleRetriever should convert the string to a dict so it can be interpreted by the connector builder

Recommended reading order

  1. airbyte-cdk/python/airbyte_cdk/sources/declarative/retrievers/simple_retriever.py
  2. airbyte-cdk/python/unit_tests/sources/declarative/retrievers/test_simple_retriever.py
  3. airbyte-connector-builder-server/unit_tests/connector_builder/impl/test_default_api.py

🚨 User Impact 🚨

Are there any breaking changes? What is the end result perceived by the user? If yes, please merge this PR with the 🚨🚨 emoji so changelog authors can further highlight this if needed.

Pre-merge Checklist

Expand the relevant checklist and delete the others.

New Connector

Community member or Airbyter

  • Community member? Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
    • docs/integrations/README.md
    • airbyte-integrations/builds.md
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub by running the /publish command described here
  • After the connector is published, connector added to connector index as described here
  • Seed specs have been re-generated by building the platform and committing the changes to the seed spec files, as described here
Updating a connector

Community member or Airbyter

  • Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • Changelog updated in docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub and connector version bumped by running the /publish command described here
Connector Generator
  • Issue acceptance criteria met
  • PR name follows PR naming conventions
  • If adding a new generator, add it to the list of scaffold modules being tested
  • The generator test modules (all connectors with -scaffold in their name) have been updated with the latest scaffold by running ./gradlew :airbyte-integrations:connector-templates:generator:testScaffoldTemplates then checking in your changes
  • Documentation which references the generator is updated as needed

Tests

Unit

Put your unit tests output here.

Integration

Put your integration tests output here.

Acceptance

Put your acceptance tests output here.

@octavia-squidington-iv octavia-squidington-iv added the CDK Connector Development Kit label Dec 16, 2022
@@ -408,20 +408,34 @@ def state(self, value: StreamState):
def parse_records_and_emit_request_and_responses(self, request, response, stream_slice, stream_state) -> Iterable[StreamData]:
# Only emit requests and responses when running in debug mode
if self.logger.isEnabledFor(logging.DEBUG):
yield self._create_trace_message_from_request(request)
yield self._create_trace_message_from_response(response)
yield prepared_request_to_airbyte_message(request)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

rename

"url": request.url,
"http_method": request.method,
"headers": dict(request.headers),
"body": _body_binary_string_to_dict(request.body),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

instead of assigning the body directly, delegate to_body_binary_string_to_dict to convert it to an Optional[Mapping]

@@ -155,6 +155,7 @@ def test_read_stream():
request = {
"url": "https://demonslayers.com/api/v1/hashiras?era=taisho",
"headers": {"Content-Type": "application/json"},
"http_method": "GET",
Copy link
Contributor Author

@girarda girarda Dec 16, 2022

Choose a reason for hiding this comment

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

fixes the tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we'll be able to merge this PR when this is merged in so CI fails on broken tests #20217

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for pointing that out - was looking into it for a PR I'm working on right now 😅

@@ -409,6 +422,11 @@ def test_read_stream_returns_error_if_stream_does_not_exist():
HttpRequest(url="https://nichirin.com/v1/swords", headers={"field": "name"}, body={"key": "value"}, http_method="PUT"),
id="test_create_request_with_no_parameters",
),
pytest.param(
'request:{"url": "https://nichirin.com/v1/swords", "http_method": "POST", "headers": {"field": "name"}, "body":null}',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

add a test where "body": null for peace of mind

@girarda girarda temporarily deployed to more-secrets December 16, 2022 01:01 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 16, 2022 01:01 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 16, 2022 01:14 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 16, 2022 01:15 — with GitHub Actions Inactive
return None


def response_to_airbyte_message(response: requests.Response) -> AirbyteMessage:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to a function for consistency and ease of testing

Copy link
Contributor

Choose a reason for hiding this comment

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

Those methods aren't used anywhere else except from the tests though. Should we indicate them as private using the _ prefix?

Saying that makes me think it is weird that parse_records_and_emit_request_and_responses is public as well since it's only used internally by the class. My main concern is that we have someone external using it which would bring a dependency to manage when we want to change this. This happened for #20019 and prevented us to do a breaking change we wanted to do

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call. prepended both with _

@girarda girarda temporarily deployed to more-secrets December 16, 2022 01:35 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 16, 2022 01:36 — with GitHub Actions Inactive
@girarda girarda changed the title Alex/request body [low-code] convert request.body to a dict when converting to AirbyteLogMessage Dec 16, 2022
@girarda girarda marked this pull request as ready for review December 16, 2022 03:00
@girarda girarda requested a review from a team as a code owner December 16, 2022 03:00
return AirbyteMessage(type=MessageType.LOG, log=AirbyteLogMessage(level=Level.INFO, message=log_message))

def prepared_request_to_airbyte_message(request: requests.PreparedRequest) -> AirbyteMessage:
# FIXME: this should return some sort of trace message
Copy link
Contributor Author

Choose a reason for hiding this comment

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

need to keep them FIXMEs for now 😞

return AirbyteMessage(type=MessageType.LOG, log=AirbyteLogMessage(level=Level.INFO, message=log_message))


def _body_binary_string_to_dict(body_str) -> Optional[Mapping[str, str]]:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have typing for this parameter?

return None


def response_to_airbyte_message(response: requests.Response) -> AirbyteMessage:
Copy link
Contributor

Choose a reason for hiding this comment

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

Those methods aren't used anywhere else except from the tests though. Should we indicate them as private using the _ prefix?

Saying that makes me think it is weird that parse_records_and_emit_request_and_responses is public as well since it's only used internally by the class. My main concern is that we have someone external using it which would bring a dependency to manage when we want to change this. This happened for #20019 and prevented us to do a breaking change we wanted to do

Copy link
Contributor

@clnoll clnoll left a comment

Choose a reason for hiding this comment

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

LGTM (pending resolution of Maxime's comments) @girarda!

@girarda girarda temporarily deployed to more-secrets December 20, 2022 01:36 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 20, 2022 01:36 — with GitHub Actions Inactive
raise HTTPException(status_code=400, detail=f"Could not perform read with with error: {error.args[0]}")
raise HTTPException(
status_code=400,
detail=f"Could not perform read with with error: {error.args[0]}\n{''.join(traceback.TracebackException.from_exception(error).format())}",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@flash1293 FYI you can rebase from this branch if you need the stacktraces. will merge in after the holidays

@girarda girarda temporarily deployed to more-secrets December 21, 2022 23:02 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 21, 2022 23:03 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 22, 2022 07:10 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets December 22, 2022 07:11 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets January 1, 2023 22:05 — with GitHub Actions Inactive
@girarda girarda temporarily deployed to more-secrets January 1, 2023 22:06 — with GitHub Actions Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CDK Connector Development Kit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

connector builder server does not handle request body
4 participants