import sys

# Part of our Lambda deployment process installs requirements into ./vendor, so add it to the path
sys.path.append('./vendor')

import requests  # NOQA

BASE_URL = "https://api-extensions.southwest.com/v1/mobile"
USER_AGENT = "Southwest/3.3.7 (iPhone; iOS 9.3; Scale/2.00)"
API_KEY = "l7xx8d8bfce4ee874269bedc02832674129b"


class SouthwestAPIError(Exception):
    pass


def _make_request(path, data, content_type, method='post', check_status_code=True):
    url = "%s%s" % (BASE_URL, path)
    headers = {
        "User-Agent": USER_AGENT,
        "Content-Type": content_type,
        "X-Api-Key": API_KEY,
        "Accept-Language": "en-US;q=1"
    }

    assert method.lower() in ("post", "get")

    request_fn = getattr(requests, method.lower())
    response = request_fn(url, json=data, headers=headers, verify=False)

    if check_status_code and not response.ok:
        try:
            msg = response.json()['message']
        except:
            msg = response.reason

        raise SouthwestAPIError("status_code=%s msg=\"%s\"" % (response.status_code, msg))

    return response

# TODO(dw): Create a handler wrapper to accept event/context?


def get_reservation(event, context):
    first_name = event['first_name']
    last_name = event['last_name']
    confirmation_number = event['confirmation_number']


def check_in(event, context):
    content_type = "application/vnd.swacorp.com.mobile.boarding-passes-v1.0+json"
    first_name = event['first_name']
    last_name = event['last_name']
    confirmation_number = event['confirmation_number']
    data = {
        'names': [{
            'firstName': first_name,
            'lastName': last_name
        }]
    }

    response = _make_request(
        "/reservations/record-locator/%s/boarding-passes" % confirmation_number,
        data,
        content_type
    )

    check_in_docs = response.json()['passengerCheckInDocuments'][0]['checkinDocuments']

    message = "confirmation_number=%s documents=%s emailed=false" % (confirmation_number, len(check_in_docs))
    return dict(message=message, event=event)

@@ -1104,235 +1104,1751 @@
 se%0A%0A
-# TODO(dw): Create a handler wrapper to accept event/context?%0A%0A%0Adef get_reservation(event, context):%0A    first_name = event%5B'first_name'%5D%0A    last_name = event%5B'last_name'%5D%0A    confirmation_number = event%5B'confirmation_number'%5D
+%0Adef _pull_reservation(confirmation_number, first, last):%0A    %22%22%22Given a confirmation_number, first and last name, retrieve iternerary for trip.%22%22%22%0A    url = '%7B%7D/reservations/record-locator/%7B%7D'.format(BASE_URL, confirmation_number)%0A    headers = %7B%0A        'Accept-Language': 'en-US;q=1',%0A        'Content-Type': 'application/vnd.swacorp.com.mobile.boarding-passes-v1.0+json',%0A        'User-Agent': USER_AGENT,%0A        'X-Api-Key': API_KEY%0A    %7D%0A    # Pass params so they are encoded%0A    payload = %7B%0A        'action': 'VIEW',%0A        'first-name': first,%0A        'last-name': last%0A    %7D%0A%0A    response = requests.get(url, headers=headers, params=payload)%0A    # If request fails try to determine why and raise detailed error%0A    if not response.ok:%0A        try:%0A            msg = response.json()%5B'message'%5D%0A        except:%0A            msg = response.reason%0A%0A        raise ReservationError(%22status_code=%25s msg=%5C%22%25s%5C%22%22 %25 (response.status_code, msg))%0A    # The originationDestinationId is the%0A    origin_data = response.json().get('itinerary', %7B%7D).get('originationDestinations', %5B%7B'n': 0%7D%5D)%5B0%5D.get('originationDestinationId', None)%0A%0A    return origin_data%0A%0A# TODO(dw): Create a handler wrapper to accept event/context?%0A%0A%0Adef get_reservation(event, context):%0A    %22%22%22Find detailed origin information from reservation via confirmation number, first and last name.%22%22%22%0A    first_name = event%5B'first_name'%5D%0A    last_name = event%5B'last_name'%5D%0A    confirmation_number = event%5B'confirmation_number'%5D%0A%0A    # Call out to get origin_data from reservation%0A    origin_data = _pull_reservation(confirmation_number, first_name, last_name)%0A%0A    return dict(confirmation_number=confirmation_number, first_name=first_name, last_name=last_name, origin_data=origin_data)
 %0A%0A%0Ad

Add reservation lookup call