def test_list_with_pagination(self):
     doc = Document(Mock())
     doc.make_request = Mock()
     doc.list('collections', 'collection_test', 20, 2)
     params = {'per_page': 20, 'page': 2}
     doc.make_request.assert_called_once_with(
         method='GET', uri='collections/collection_test', params=params)
    def test_get(self):
        doc = Document(Mock())
        doc.make_request = Mock()
        doc.get('collections', 'collection_test', 'key1')

        doc.make_request.assert_called_once_with(
            method='GET', uri='collections/collection_test/key1')
    def test_post(self):
        doc = Document(Mock())
        doc.make_request = Mock()
        doc.post('collections', 'collection_test', {'doc': 1})

        doc.make_request.assert_called_once_with(
            method='POST', uri='collections/collection_test/', data={'doc': 1})
    def test_delete(self):
        doc = Document(Mock())
        doc.make_request = Mock()
        doc.delete('collections', 'collection_test', 'key1')

        doc.make_request.assert_called_once_with(
            method='DELETE', uri='collections/collection_test/key1/')
예제 #5
0
 def generate_auth(self):
     LOGGER.info('New Auth')
     self.auth = auth.Auth(api_url=self.host,
                           username=GLOBOMAP_API_USERNAME,
                           password=GLOBOMAP_API_PASSWORD)
     self.doc = Document(auth=self.auth)
     self.query = Query(auth=self.auth)
    def test_patch(self):
        doc = Document(Mock())
        doc.make_request = Mock()
        doc.patch('collections', 'collection_test', 'key1', {'doc': 1})

        doc.make_request.assert_called_once_with(
            method='PATCH',
            uri='collections/collection_test/key1/',
            data={'doc': 1})
    def test_search_with_pagination(self):
        doc = Document(Mock())
        doc.make_request = Mock()
        query = [[{'field': 'name', 'operator': 'LIKE', 'value': 'test'}]]
        doc.search('collections', 'collection_test', query)

        params = {'query': json.dumps(query), 'per_page': 10, 'page': 1}
        doc.make_request.assert_called_once_with(
            method='GET', uri='collections/collection_test', params=params)
    def test_clear(self):
        doc = Document(Mock())
        doc.make_request = Mock()
        query = [[{'field': 'name', 'operator': 'LIKE', 'value': 'test'}]]
        doc.clear('collections', 'collection_test', query)

        doc.make_request.assert_called_once_with(
            method='POST',
            uri='collections/collection_test/clear/',
            data=query)
예제 #9
0
class GloboMapClient(object):
    def __init__(self, host):
        self.host = host
        self.generate_auth()

    def generate_auth(self):
        LOGGER.info('New Auth')
        self.auth = auth.Auth(api_url=self.host,
                              username=GLOBOMAP_API_USERNAME,
                              password=GLOBOMAP_API_PASSWORD)
        self.doc = Document(auth=self.auth)
        self.query = Query(auth=self.auth)

    def update_element_state(self,
                             action,
                             type,
                             collection,
                             element,
                             key,
                             retries=0):

        try:
            if action.upper() == 'CREATE':
                return self.create(type, collection, element)
            elif action.upper() == 'UPDATE':
                return self.update(type, collection, key, element)
            elif action.upper() == 'PATCH':
                return self.patch(type, collection, key, element)
            elif action.upper() == 'DELETE':
                return self.delete(type, collection, key)
            elif action.upper() == 'CLEAR':
                return self.clear(type, collection, element)

        except exceptions.ValidationError as err:
            if '1200' in err.message['errors'] and retries < RETRIES:
                LOGGER.warning('Retry action %s %s %s %s %s', action, type,
                               collection, element, key)
                retries += 1
                time.sleep(2)
                self.update_element_state(action, type, collection, element,
                                          key, retries)
            else:
                LOGGER.error('Bad request in send element %s %s %s %s %s %s',
                             action, type, collection, element, key,
                             err.message)
                raise GloboMapException(err.message, err.status_code)

        except exceptions.Unauthorized as err:
            if retries < RETRIES:
                LOGGER.warning('Retry action %s %s %s %s %s', action, type,
                               collection, element, key)
                retries += 1
                self.generate_auth()
                time.sleep(5 + (retries * 5))
                self.update_element_state(action, type, collection, element,
                                          key, retries)
            else:
                LOGGER.error('Error send element %s %s %s %s %s', action, type,
                             collection, element, key)
                raise GloboMapException(err.message, err.status_code)

        except exceptions.Forbidden as err:
            LOGGER.error('Forbbiden send element %s %s %s %s %s', action, type,
                         collection, element, key)
            raise GloboMapException(err.message, err.status_code)

        except exceptions.ApiError as err:
            if retries < RETRIES:
                LOGGER.warning('Retry send element %s %s %s %s %s', action,
                               type, collection, element, key)
                retries += 1
                time.sleep(5 + (retries * 5))
                self.update_element_state(action, type, collection, element,
                                          key, retries)
            else:
                LOGGER.error('Error send element %s %s %s %s %s', action, type,
                             collection, element, key)
                raise GloboMapException(err.message, err.status_code)

    def create(self, type, collection, payload):
        try:
            return self.doc.post(type, collection, payload)

        except exceptions.NotFound as err:
            raise GloboMapException(err.message, err.status_code)

        except exceptions.DocumentAlreadyExists:
            LOGGER.warning('Element already insered')

    def update(self, type, collection, key, payload):
        try:
            return self.doc.put(type, collection, key, payload)

        except exceptions.NotFound:
            return self.create(type, collection, payload)

    def patch(self, type, collection, key, payload):
        try:
            return self.doc.patch(type, collection, key, payload)

        except exceptions.NotFound:
            return self.create(type, collection, payload)

    def delete(self, type, collection, key):
        try:
            return self.doc.delete(type, collection, key)

        except exceptions.NotFound:
            LOGGER.warning('Element %s already deleted', key)

    def clear(self, type, collection, payload):
        return self.doc.clear(type, collection, payload)

    def run_query(self, query_id, variable):
        return self.query.execute(query_id, variable)