Example #1
0
 def save_stream(self, **kwargs):
     amp_api = ApiService(kwargs['api_host'], kwargs['api_id'],
                          kwargs['api_key'])
     storage = AmpStorageWrapper(self.__metadata(kwargs.get('name')))
     stream = storage.find_stream()
     if stream is None:
         logger.info('Controller - Creating the stream at API')
         response = amp_api.create(kwargs)
         logger.info('Controller - Created the stream at API: {}'.format(
             kwargs.get('name')))
         storage.save_stream_with_data(
             StreamDictManager(response['data']).merged_with(kwargs))
     else:
         dict_manager = StreamDictManager(stream)
         diff = dict_manager.diff_fields_from(kwargs)
         if len(diff) > 0:
             logger.info(
                 'Controller - Updating the stream at API {}'.format(
                     stream.get('name')))
             amp_api.update(stream['id'], diff)
             storage.save_stream_with_data(dict_manager.merged_with(kwargs))
             logger.info('Controller - Updated the stream at API')
     output = jsonresponse.JsonResponse()
     output.success = True
     return self.render_json(output)
 def setUp(self):
     self.options = {
         'host': 'host',
         'api_id': 'api_id',
         'api_key': 'api_key'
     }
     self.apiService = ApiService(**self.options)
Example #3
0
 def delete_event_stream(self, **kwargs):
     amp_api = ApiService(kwargs['api_host'], kwargs['api_id'],
                          kwargs['api_key'])
     self.__try_destroy_stream(amp_api, kwargs['id'])
     output = jsonresponse.JsonResponse()
     output.success = True
     return self.render_json(output)
 def delete_stream(self, **kwargs):
     amp_api = ApiService(kwargs['api_host'], kwargs['api_id'], kwargs['api_key'])
     storage = AmpStorageWrapper(self.__metadata(kwargs.get('name')))
     stream = storage.find_stream()
     if stream is not None:
         logger.info('Controller - Deleting the stream at API: {}'.format(stream.get('name')))
         self.__try_destroy_stream(amp_api, stream['id'])
         storage.delete_stream()
     output = jsonresponse.JsonResponse()
     output.success = True
     return self.render_json(output)
Example #5
0
 def event_streams_list(self, **kwargs):
     amp_api = ApiService(kwargs['api_host'], kwargs['api_id'],
                          kwargs['api_key'])
     response = amp_api.index()
     return self.render_json(response['data'])
Example #6
0
 def groups_list(self, **kwargs):
     amp_api = ApiService(kwargs['api_host'], kwargs['api_id'],
                          kwargs['api_key'])
     response = amp_api.groups()
     return self.render_json(response['data'])
class TestApiService(unittest.TestCase):
    DEFAULT_RESPONSE = '{"success": true}'
    JSON_RESPONSE = json.loads(DEFAULT_RESPONSE)
    ERROR_RESPONSE = '{"error": "Test error"}'

    def setUp(self):
        self.options = {
            'host': 'host',
            'api_id': 'api_id',
            'api_key': 'api_key'
        }
        self.apiService = ApiService(**self.options)

    def test_instantiation(self):
        self.assertEqual(self.apiService.host, self.options['host'])
        self.assertEqual(self.apiService.api_id, self.options['api_id'])
        self.assertEqual(self.apiService.api_key, self.options['api_key'])

    def test_index(self):
        with requests_mock.mock() as m:
            m.get('https://host/v1/event_streams/', text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.index(), self.JSON_RESPONSE)

    def test_show(self):
        with requests_mock.mock() as m:
            m.get('https://host/v1/event_streams/1',
                  text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.show(1), self.JSON_RESPONSE)

    def test_create(self):
        with requests_mock.mock() as m:
            m.post('https://host/v1/event_streams/',
                   text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.create({}), self.JSON_RESPONSE)

    def test_update(self):
        with requests_mock.mock() as m:
            m.patch('https://host/v1/event_streams/1',
                    text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.update(1, {}), self.JSON_RESPONSE)

    def test_destroy(self):
        with requests_mock.mock() as m:
            m.delete('https://host/v1/event_streams/1',
                     text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.destroy(1), self.JSON_RESPONSE)

    def test_event_types(self):
        with requests_mock.mock() as m:
            m.get('https://host/v1/event_types/', text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.event_types(), self.JSON_RESPONSE)

    def test_groups(self):
        with requests_mock.mock() as m:
            m.get('https://host/v1/groups/', text=self.DEFAULT_RESPONSE)
            self.assertEqual(self.apiService.groups(), self.JSON_RESPONSE)

    def test_construct_url(self):
        self.assertEqual(self.apiService._ApiService__construct_url(),
                         'https://host/v1/event_streams/')
        self.assertEqual(self.apiService._ApiService__construct_url(1),
                         'https://host/v1/event_streams/1')
        self.assertEqual(
            self.apiService._ApiService__construct_url('', '/v1/event_types/'),
            'https://host/v1/event_types/')
        self.assertEqual(
            self.apiService._ApiService__construct_url('', '/v1/groups/'),
            'https://host/v1/groups/')

    def test_error_raised(self):
        # raise error if status code is 400
        with requests_mock.mock() as m:
            m.get('https://host/v1/event_streams/',
                  status_code=400,
                  text=self.ERROR_RESPONSE)
            with self.assertRaises(ApiError) as api_error_context:
                self.apiService.index()
            self.assertEqual(api_error_context.exception.message,
                             self.ERROR_RESPONSE)
            self.assertEqual(api_error_context.exception.status, 400)