# Path: cartographer/exceptions/request_exceptions.py
# class DataMissing(ParameterMissing):
#     parameter_name = 'data'
# 
# class BadPageCountParameter(ParameterInvalid):
#     def __init__(self, parameter_value):
#         super().__init__(parameter_name='page[count]',
#                          parameter_value=parameter_value)
#         self.error_description = "Page sizes must be integers."
# 
# class BadPageCursorParameter(ParameterInvalid):
#     def __init__(self, parameter_value):
#         super().__init__(parameter_name='page[cursor]',
#                          parameter_value=parameter_value)
#         self.error_description = "Provided cursor was not parsable."
# 
# class BadPageOffsetParameter(ParameterInvalid):
#     def __init__(self, parameter_value):
#         super().__init__(parameter_name='page[offset]',
#                          parameter_value=parameter_value)
#         self.error_description = "Page offsets must be integers."
# 
# Path: cartographer/requests/jsonapi_request_interface.py
# class JSONAPIRequestInterface(object):
#     _metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def get_json_api_version(self):
#         return
# 
#     @abstractmethod
#     def get_json(self, force=False):
#         return
# 
#     @abstractmethod
#     def get_includes(self):
#         return
# 
#     @abstractmethod
#     def get_requested_fields(self):
#         return
# 
# Path: cartographer/utils/version.py
# class JSONAPIVersion(Enum):
#     JSONAPI_RC2 = "1.0RC2"
#     JSONAPI_RC3 = "1.0RC3"
#     JSONAPI_1_0 = "1.0"
# 
# def get_default_version():
#     return _JSONAPI_DEFAULT_VERSION


import json
import re
from cartographer.exceptions.request_exceptions import DataMissing, BadPageCountParameter, BadPageCursorParameter, \
    BadPageOffsetParameter
from cartographer.requests.jsonapi_request_interface import JSONAPIRequestInterface
from cartographer.utils.version import JSONAPIVersion, get_default_version



class JSONAPIFlaskRequestMixin(JSONAPIRequestInterface):
    def get_jsonapi_json(self):
        force_if_correct_mime = (self.mimetype == 'application/vnd.api+json')
        return self.get_json(force=force_if_correct_mime)

    def get_jsonapi_data(self):
        sent_json = self.get_jsonapi_json()
        if sent_json and 'data' in sent_json:
            data = sent_json.get('data')
        elif self.form and self.form.get('data'):
            data_str = self.form.get('data')
            data = json.loads(data_str)
        else:
            raise DataMissing()
        # TODO: have get_data clients expect attributes to be nested
        if self.get_json_api_version() == JSONAPIVersion.JSONAPI_1_0:
            data.update(data.get('attributes', {}))
        return data

    def get_sort(self):
        return self.args.get('sort')

    def get_pagination(self, page_count_default=10, cursor_formatter=None):
        per_page = self.args.get('page[count]')
        if per_page is not None:
            if per_page != 'infinity':
                try:

# Path: cartographer/exceptions/request_exceptions.py
# class DataMissing(ParameterMissing):
#     parameter_name = 'data'
# 
# class BadPageCountParameter(ParameterInvalid):
#     def __init__(self, parameter_value):
#         super().__init__(parameter_name='page[count]',
#                          parameter_value=parameter_value)
#         self.error_description = "Page sizes must be integers."
# 
# class BadPageCursorParameter(ParameterInvalid):
#     def __init__(self, parameter_value):
#         super().__init__(parameter_name='page[cursor]',
#                          parameter_value=parameter_value)
#         self.error_description = "Provided cursor was not parsable."
# 
# class BadPageOffsetParameter(ParameterInvalid):
#     def __init__(self, parameter_value):
#         super().__init__(parameter_name='page[offset]',
#                          parameter_value=parameter_value)
#         self.error_description = "Page offsets must be integers."
# 
# Path: cartographer/requests/jsonapi_request_interface.py
# class JSONAPIRequestInterface(object):
#     _metaclass__ = ABCMeta
# 
#     @abstractmethod
#     def get_json_api_version(self):
#         return
# 
#     @abstractmethod
#     def get_json(self, force=False):
#         return
# 
#     @abstractmethod
#     def get_includes(self):
#         return
# 
#     @abstractmethod
#     def get_requested_fields(self):
#         return
# 
# Path: cartographer/utils/version.py
# class JSONAPIVersion(Enum):
#     JSONAPI_RC2 = "1.0RC2"
#     JSONAPI_RC3 = "1.0RC3"
#     JSONAPI_1_0 = "1.0"
# 
# def get_default_version():
#     return _JSONAPI_DEFAULT_VERSION


# Path: cartographer/requests/jsonapi_flask_request_mixin.py
import json
import re
from cartographer.exceptions.request_exceptions import DataMissing, BadPageCountParameter, BadPageCursorParameter, \
    BadPageOffsetParameter
from cartographer.requests.jsonapi_request_interface import JSONAPIRequestInterface
from cartographer.utils.version import JSONAPIVersion, get_default_version




class JSONAPIFlaskRequestMixin(JSONAPIRequestInterface):
    def get_jsonapi_json(self):
        force_if_correct_mime = (self.mimetype == 'application/vnd.api+json')
        return self.get_json(force=force_if_correct_mime)

    def get_jsonapi_data(self):
        sent_json = self.get_jsonapi_json()
        if sent_json and 'data' in sent_json:
            data = sent_json.get('data')
        elif self.form and self.form.get('data'):
            data_str = self.form.get('data')
            data = json.loads(data_str)
        else:
            raise DataMissing()
        # TODO: have get_data clients expect attributes to be nested
        if self.get_json_api_version() == JSONAPIVersion.JSONAPI_1_0:
            data.update(data.get('attributes', {}))
        return data

    def get_sort(self):
        return self.args.get('sort')

    def get_pagination(self, page_count_default=10, cursor_formatter=None):
        per_page = self.args.get('page[count]')
        if per_page is not None:
            if per_page != 'infinity':
                try:

                    per_page = int(per_page)