Skyscanner / skyscanner-python-sdk

Skyscanner Python SDK
Other
122 stars 33 forks source link

Flights.get_results blows up if create_session gets "error" answer from SkyScanner server #24

Open ditiem opened 7 years ago

ditiem commented 7 years ago

The method create_session in the Flights class does not always return a "url" (see POINT 2 below, where a callback is supposed to be called to return the Location header for a given response object), making the call to "poll_session" (see POINT 1 below) blow up. Notice create_session returns a "request library" response object if something went wrong.

PROBLEM:

Notice poll_session calls make_request straight away passing poll_url without any check:

def poll_session(self, poll_url, initial_delay=2, delay=1, tries=20,
                 errors=GRACEFUL, **params):
    ...
    for n in range(tries):
        poll_response = **self.make_request(
            poll_url,**
            headers=self._headers(),
            errors=errors, **params
        )

and in make_request there is a line that assumes the first argument, service_url, is an string:

    if 'apikey' not in service_url.lower(): <--- Boom! Object Response does not have any lower method

TRACE:

class Transport(object):
...
    def get_result(self, errors=GRACEFUL, **params):
    ...
        return self.poll_session(
            self.create_session(**params), <--- POINT 1

class Flights(Transport):
    ...
    def create_session(self, **params):
        ...
        return self.make_request(self.PRICING_SESSION_URL,
                                 method='post',
                                 headers=self._session_headers(),
                                 allow_redirects=False,
                                 callback=lambda resp: resp.headers['location'],  <---- POINT 2
                                 data=params)

POINT 2 is not executed, and instead Flights.create_session returns a response object, which becomes the poll_url argument of poll_session.