Exemple #1
0
def parse_boolean(body, code, headers):
    # Mixpanel's track endpoint responds with '1' for True and '0' for False
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)

    if body == b'1':
        return True
    elif body == b'0':
        return False

    raise http.HTTPError(body, code, headers)
Exemple #2
0
def parse_export(body, code, headers):
    # the export endpoint returns lines of JSON values
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)

    # JSON mandates the use of UTF-8, so assume each line is decodable
    return [json.loads(line.decode('utf-8')) for line in body.splitlines()]
Exemple #3
0
def parse_count(body, code, headers):
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)

    # default to 1 if the header is not present - usually it means that the
    # endpoint returns a single resource and not a collection
    return int(headers.get('x-records', 1))
Exemple #4
0
def parse_boolean(body, code, headers):
    # The boolean value endpoints respond with 204 if the response is true and
    # 404 if it is not.
    if code == 204:
        return True
    if code == 404:
        return False
    raise http.HTTPError(body, code, headers)
Exemple #5
0
def parse_xml(body, code, headers):
    """
    Return a dictionary.

    Transformation is done assuming:
    1.  If a tag indicates its value type, we'll try to cast it.
    2.  Siblings with the same tag name, become a list.
    3.  Attributes become a dict key starting with '@'.

    <duck>
        <name>Donald</name>
        <birth_date type="datetime">1934-06-04T00:00:00</birth_date>
        <first_film/>
        <last_film></last_film>
        <species href="http://en.wikipedia.org/wiki/Pekin_duck"/>
        <created_by href="http://en.wikipedia.org/wiki/Walt_Disney">
            <name>Walt Disney</name>
            <cryopreserved type="boolean">true</cryopreserved>
        </created_by>
        <family>
            <children type="array"></children>
            <uncles type="array">
                <uncle><name>Scrooge McDuck</name></uncle>
                <uncle><name>Ludwig Von Drake</name></uncle>
            </uncles>
            <nephew><name>Huey</name></nephew>
            <nephew><name>Dewey</name></nephew>
            <nephew><name>Louie</name></nephew>
        </family>
    </duck>

    {'duck': {'birth_date': '1934-06-04T00:00:00',
              'created_by': {'@href': 'http://en.wikipedia.org/wiki/Walt_Disney',
                             'cryopreserved': True,
                             'name': 'Walt Disney'},
              'family': {'nephew': [{'name': 'Huey'},
                                    {'name': 'Dewey'},
                                    {'name': 'Louie'}],
                         'children': [],
                         'uncles': {'uncle': [{'name': 'Scrooge McDuck'},
                                              {'name': 'Ludwig Von Drake'}]}},
              'first_film': None,
              'last_film': None,
              'name': 'Donald',
              'species': {'@href': 'http://en.wikipedia.org/wiki/Pekin_duck'}}
    }
    """
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)

    root_elem = etree.fromstring(body)
    return {root_elem.tag: value_for_element(root_elem)}
Exemple #6
0
def parse_passthrough(body, code, headers):
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)

    return body
Exemple #7
0
def parse_empty(body, code, headers):
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)
Exemple #8
0
def parse_json(body, code, headers):
    if not 200 <= code < 300:
        raise http.HTTPError(body, code, headers)

    # JSON mandates the use of UTF-8, so assume the body is decodable
    return json.loads(body.decode('utf-8'))
Exemple #9
0
def parse_redirect(body, code, headers):
    if code != 302:
        raise http.HTTPError(body, code, headers)

    return headers.get('location')