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

correctly handle async POST requests in swaggerpy + py3 #238

Merged
merged 1 commit into from
Aug 9, 2016
Merged

correctly handle async POST requests in swaggerpy + py3 #238

merged 1 commit into from
Aug 9, 2016

Conversation

mxmul
Copy link

@mxmul mxmul commented Aug 9, 2016

AsynchronousHTTPClient.start_request fails with a TypeError when the request has any form data in python3, because we assume that the body of the request is a byte string:

self = <class 'swaggerpy.async_http_client.AsynchronousHttpClient'>(), request_params = {'data': {'name': 'Matt'}, 'headers': {}, 'method': b'POST', 'url': 'http://localhost:49111/post'}

    def start_request(self, request_params):
        """Sets up the request params as per Twisted Agent needs.
            Sets up crochet and triggers the API request in background

            :param request_params: request parameters for API call
            :type request_params: dict

            :return: crochet EventualResult
            """

        prepared_request = requests.PreparedRequest()
        prepared_request.prepare(
            headers=request_params.get('headers'),
            data=request_params.get('data'),
            params=request_params.get('params'),
            files=request_params.get('files'),
            url=request_params.get('url'),
            method=request_params.get('method')
        )

        request_for_crochet = {
            'method': prepared_request.method or 'GET',
            'bodyProducer': FileBodyProducer(BytesIO(prepared_request.body))
>           if prepared_request.body else None,
            'headers': listify_headers(prepared_request.headers),
            'uri': prepared_request.url,
        }
E       TypeError: 'str' does not support the buffer interface

This branch encodes the request body to a byte string if it's unicode.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.09%) to 93.404% when pulling da968ed on mxmul:mmulder_swaggerpy_py3_async_post into f4567a4 on Yelp:swaggerpy.

1 similar comment
@coveralls
Copy link

coveralls commented Aug 9, 2016

Coverage Status

Coverage decreased (-0.09%) to 93.404% when pulling da968ed on mxmul:mmulder_swaggerpy_py3_async_post into f4567a4 on Yelp:swaggerpy.

@prat0318
Copy link
Contributor

prat0318 commented Aug 9, 2016

👍 lgtm

we shouldn't assume that the body of a requests.PreparedRequest consists
of bytes
@coveralls
Copy link

coveralls commented Aug 9, 2016

Coverage Status

Coverage increased (+0.1%) to 93.602% when pulling a6f4c64 on mxmul:mmulder_swaggerpy_py3_async_post into f4567a4 on Yelp:swaggerpy.

@prat0318 prat0318 merged commit 8c3e3ad into Yelp:swaggerpy Aug 9, 2016
@lucagiovagnoli
Copy link
Contributor

If possible, I would suggest not using the AsynchronousHttpClient which comes with swaggerpy because I found it to be unstable on production. Also, swaggerpy in general as it is not actively maintained as bravado (and handles old swagger 1.x specs).

@mxmul mxmul deleted the mmulder_swaggerpy_py3_async_post branch March 18, 2017 21:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants