Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

from collections import OrderedDict 

import sys 

 

PY3 = sys.version_info > (3,) 

 

 

def unpack(value): 

"""Return a three tuple of data, code, and headers""" 

if not isinstance(value, tuple): 

return value, 200, {} 

value_len = len(value) 

if value_len == 2: 

return value[0], value[1], {} 

elif value_len == 3: 

return value[0], value[1], value[2] 

else: 

return value, 200, {} 

 

 

def get_accept_mimetypes(request): 

accept_types = request.headers.get('accept', None) 

if accept_types is None: 

return {} 

split_types = str(accept_types).split(',') 

# keep the order they appear! 

return OrderedDict([((s, 1,), s,) for s in split_types]) 

 

 

def best_match_accept_mimetype(request, representations, default=None): 

if representations is None or len(representations) < 1: 

return default 

try: 

accept_mimetypes = get_accept_mimetypes(request) 

if accept_mimetypes is None or len(accept_mimetypes) < 1: 

return default 

# find exact matches, in the order they appear in the `Accept:` header 

for accept_type, qual in accept_mimetypes: 

if accept_type in representations: 

return accept_type 

# match special types, like "application/json;charset=utf8" where the first half matches. 

for accept_type, qual in accept_mimetypes: 

type_part = str(accept_type).split(';', 1)[0] 

if type_part in representations: 

return type_part 

# if _none_ of those don't match, then fallback to wildcard matching 

for accept_type, qual in accept_mimetypes: 

if accept_type == "*"\ 

or accept_type == "*/*"\ 

or accept_type == "*.*": 

return default 

except (AttributeError, KeyError): 

return default