Returning the result of request

Viewed 40

I am developing a request method by using tornado in the back-end. It should perform a get-request and make a callback, however I cannot seem to return the response results.

For example, If I return response.body this prints out the body parameter inserted into the HTTPRequest method, rather than giving the response back.

Here is my script:


class getToken(tornado.web.RequestHandler):

        def __init__(self,url: str, params: str, headers: dict):
            #super(tornado.web.RequestHandler, self).__init__(*args, **kwargs)
            self._client = tornado.httpclient.AsyncHTTPClient()
            self._url = url
            self._body = params
            self._headers = headers


        def response(response):
            return response.request

        @tornado.gen.coroutine
        def _request(self, callback, request):
            try:
                response = yield self._client.fetch(request)
            except tornado.httpclient.HTTPError as e:
                response = e.response
            raise tornado.gen.Return(
                callback(
                    self.response(
                        response
                        )
                    )
                )
        def get_request(self, callback):
            response =  tornado.httpclient.HTTPRequest(self._url,
                                                        method = 'GET',
                                                        headers = self._headers,
                                                        body = data)
            print(response.body)
            return self._request(
                            callback, 
                            response)

I run this script with the following:

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
}
url = 'https://query1.finance.yahoo.com/v8/finance/chart/BOIL.L'

params = 'region=GB&lang=en-GB&includePrePost=false&interval=2m&useYfid=true&range=1d&corsDomain=uk.finance.yahoo.com&.tsrc=finance'

r = getToken(url = url, params=params, headers=headers)

print(r.get_request(Future.add_done_callback))

result:

b'region=GB&lang=en-GB&includePrePost=false&interval=2m&useYfid=true&range=1d&corsDomain=uk.finance.yahoo.com&.tsrc=finance'

<Future pending cb=[coroutine.<locals>.wrapper.<locals>.<lambda>() at /Users/usr/yahoo/venv/lib/python3.8/site-packages/tornado/gen.py:251]>

The response should return a dictionary format, although I am slightly concerned why the future is pending.

1 Answers

I managed to figure this one out - there was a slight error in the code.

We should have:

@staticmethod
def response(response):
    return response.body

for response

Then having on_request replaced with async-await methods which according to tornado should be used.

        async def on_request(self, callback, request):
            try:
                response = await self._client.fetch(request)
            except tornado.httpclient.HTTPError as e:
                response = e.response
            return callback(
                    self.response(
                        response
                        )
                    )

and finally adding a run method on the loop produces the results I have after.

class getToken(tornado.web.RequestHandler):

        def __init__(self,url: str, params: str, headers: dict):
            #super(tornado.web.RequestHandler, self).__init__(*args, **kwargs)
            self._client = tornado.httpclient.AsyncHTTPClient()
            self._url = url
            self._body = params
            self._headers = headers

        @staticmethod
        def response(response):
            return response.body
            
        async def on_request(self, callback, request):
            try:
                response = await self._client.fetch(request)
            except tornado.httpclient.HTTPError as e:
                response = e.response
            return callback(
                    self.response(
                        response
                        )
                    )
                )
        def get_request(self, callback):
            response = tornado.httpclient.HTTPRequest(
                                                    self._url, 
                                                    headers = self._headers,
                                                    method='GET')

            return self._request(
                        callback, 
                        response)
        #@tornado.gen.coroutine
        def post_request(self, callback):
            if isinstance(self._body, str):
                response =  tornado.httpclient.HTTPRequest(self._url,
                                                        method = 'POST',
                                                        headers = self._headers,
                                                        body = self._body
                                                        )

                return self._request(
                            callback, 
                            response)
            else:
                
                data = f'refresh_token={self._body["refresh_token"]}&client_id={self._body["client_id"]}&client_secret={self._body["client_secret"]}&grant_type=refresh_token'
                response =  tornado.httpclient.HTTPRequest(self._url,
                                                        method = 'POST',
                                                        headers = self._headers,
                                                        body = data)
                print(response.body)
                return self._request(
                            callback, 
                            response)

headers = {
    'Origin': 'https://uk.finance.yahoo.com',
    'Accept': '*/*',
    'Referer': 'https://uk.finance.yahoo.com/quote/%5EFTMC?p=%5EFTMC',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15',
}

params = 'region=GB&lang=en-GB&includePrePost=false&interval=2m&useYfid=true&range=1d&corsDomain=uk.finance.yahoo.com&.tsrc=finance'

url = 'https://query1.finance.yahoo.com/v8/finance/chart/%5EFTMC'
async def main(request):
    return await request.get_request(print)

if __name__ == '__main__':
    asyncio.run(main(getToken(url, params, headers)))

Results:

b'{"chart":{"result":[{"meta":{"currency":"GBP","symbol":"^FTMC","exchangeName":"FGI","instrumentType":"INDEX","firstTradeDate":504864000,"regularMarketTime":1663947330,"gmtoffset":3600,"timezone":"BST","exchangeTimezoneName":"Europe/London","regularMarketPrice":17972.69,"chartPreviousClose":18331.69,"previousClose":18331.69,"scale":3,"priceHint":2,"currentTradingPeriod":{"pre":{"timezone":"BST","start":1663913700,"end":1663916400,"gmtoffset":3600},"regular":{"timezone":"BST","start":1663916400,"end":1663947000,"gmtoffset":3600},"post":{"timezone":"BST","start":1663947000,"end":1663949700,"gmtoffset":3600}}

...

...
Related