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

How to access the response headers? #171

Open
luzfcb opened this issue Jun 16, 2021 · 4 comments
Open

How to access the response headers? #171

luzfcb opened this issue Jun 16, 2021 · 4 comments

Comments

@luzfcb
Copy link

luzfcb commented Jun 16, 2021

Hi, I'm maintaining an existing application from a customer and there is an API client implementation of an external service that is implemented using tapioca-wrapper (v1.4.2). The external service now includes some headers in the response of each request and I need to perform different actions depending on the value that specific headers have.

Is there an official way to access the raw response object (not only the response body) from the TapiocaClient or TapiocaClientExecutor?

From the example below, how to access the response HEADERS from the tapioca_client instance, tapioca_client_executor instance, or from the result object of the get() method call?

from tapioca import TapiocaAdapter, generate_wrapper_from_adapter, JSONAdapterMixin
from requests.auth import HTTPBasicAuth

from .mapping import RESOURCE_MAPPING


class FooBarClientAdapter(JSONAdapterMixin, TapiocaAdapter):
    api_root = 'https://api.foobar.com/'
    resource_mapping = RESOURCE_MAPPING


FooBarClientClass = generate_wrapper_from_adapter(FooBarClientAdapter)

tapioca_client = FooBarClientClass()

url = "https://api.foobar.com/resource/123/?x=1&y=2"

tapioca_client_executor = tapioca_client._wrap_in_tapioca_executor(url)

page = 1

data = tapioca_client_executor.get(params={'page': page})

# The .response raises an exception
# tapioca_client_executor.response
@fjsj
Copy link
Member

fjsj commented Jun 17, 2021

Have you tried accessing the response from the executor? We have a property there:

def response(self):
if self._response is None:
raise Exception("This instance has no response object")
return self._response

So data().response is probably the right answer here.

To get the headers: data().response.headers.
Just tested with tapioca-github on gists endpoint and it worked fine.

@luzfcb
Copy link
Author

luzfcb commented Jun 17, 2021

@fjsj Thanks a lot. This is a good candidate for a new Cookbook section in the documentation. The current documentation is neither nearly clear nor straightforward on that particular subject.

@luzfcb
Copy link
Author

luzfcb commented Jun 17, 2021

Anyone who has the same doubts, this is a 100% working example.
Note: I used https://designer.mocky.io/design to create a mock response that includes a header.

from tapioca import TapiocaAdapter, generate_wrapper_from_adapter, JSONAdapterMixin


RESOURCE_MAPPING = {}

class FooBarClientAdapter(JSONAdapterMixin, TapiocaAdapter):
    api_root = 'https://run.mocky.io'
    resource_mapping = RESOURCE_MAPPING


FooBarClientClass = generate_wrapper_from_adapter(FooBarClientAdapter)

tapioca_client = FooBarClientClass()

url = "https://run.mocky.io/v3/927b0e28-7ec2-44fc-ab3a-d4bd386ad130"

tapioca_client_executor = tapioca_client._wrap_in_tapioca_executor(url)

new_tapioca_client_class = tapioca_client_executor.get(params={'page': 1})
new_tapioca_client_executor = new_tapioca_client_class()
print(new_tapioca_client_executor.response.headers)

@luzfcb luzfcb closed this as completed Jun 17, 2021
@fjsj
Copy link
Member

fjsj commented Jun 17, 2021

This is a good candidate for a new Cookbook section in the documentation. The current documentation is neither nearly clear nor straightforward on that particular subject.

Agreed @luzfcb , I'll leave the issue open due to that. Please feel free to open a PR for this additional documentation.

@fjsj fjsj reopened this Jun 17, 2021
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

No branches or pull requests

2 participants