import os
import unittest
import time
import jwt
from elasticsearch import Elasticsearch, NotFoundError
from os_package_registry import PackageRegistry
from ..config import LOCAL_ELASTICSEARCH

try:
    from unittest.mock import Mock, patch
except ImportError:
    from mock import Mock, patch
from importlib import import_module

module = import_module('conductor.blueprints.package.controllers')
dpp_module = import_module('datapackage.helpers')

PACKAGES_INDEX_NAME = os.environ.get('OS_ES_PACKAGES_INDEX_NAME',
                                     'test_packages')
USERS_INDEX_NAME = os.environ.get('OS_ES_USERS_INDEX_NAME', 'test_users')


class Response:
    def __init__(self, status_code, _json):
        self.status_code = status_code
        self._json = _json

    def json(self):
        return self._json

    def raise_for_status(self):
        if self.status_code != 200:
            raise AssertionError('HTTP {}'.format(self.status_code))


datapackage = {
    'name': 'my-dataset',
    'resources': [
        {
            'name': 'my-resource',
            'path': 'data.csv',
            'schema': {
                'fields': [
                    {'name': 'year',
                     'type': 'integer',
                     'columnType': 'date:fiscal-year'}
                ]
            }
        }
    ]
}

_cache = {}
callback = 'http://conductor/callback'
token = None


def cache_get(key):