Пример #1
0
class EndpointsService(BaseService):

    def __init__(self):
        self.rest_client = RestClientEngine()

    def get_endpoints(self, api_name):
        result = self.rest_client.executeRequest('getEndpoints', api_name=api_name)
        if result['headers']['status'] == '200':
            logger.debug(''.join(['Endpoints of capability ', api_name, ' retrieve successfully.']),)
            return json.loads(result['body'])
        else:
            logger.debug(''.join(['Failure retrieving endpoints from ', api_name, '.']),)
            return result

    def addEndpoint(self, data):
        api_name = data.pop('api_name', None)
        if len(data['ob']) == 0:
            data.pop('ob', None)
        body = json.dumps(data)
        result = self.rest_client.executeRequest('addEndpoint', api_name=api_name, body=body)
        if result['headers']['status'] == '201':
            logger.debug(''.join(['Added endpoint to ', api_name, ' successfully.']),)
            return json.loads(result['body'])
        else:
            logger.debug(''.join(['Failure adding endpoint to ', api_name, '.']),)
            return result

    def modifyEndpoint(self, data):
        api_name = data.pop('api_name', None)
        id_end = data.pop('id_end', None)
        if len(data['ob']) == 0:
            data.pop('ob', None)
        body = json.dumps(data)
        result = self.rest_client.executeRequest('modifyEndpoint', api_name=api_name, id_end=id_end, body=body)
        if result['headers']['status'] == '200':
            logger.debug(''.join(['Endpoint ', id_end, ' updated successfully.']),)
            return json.loads(result['body'])
        else:
            logger.debug(''.join(['Failure updating endpoint ', id_end, '.']),)
            return result

    def removeEndpoint(self, data):
        api_name = data.pop('api_name', None)
        id_end = data.pop('id_end', None)
        result = self.rest_client.executeRequest('removeEndpoint', api_name=api_name, id_end=id_end)
        if result['headers']['status'] == '204':
            logger.debug(''.join(['Endpoint ', id_end, ' removed successfully.']),)
            return {'result': 'Endpoint removed'}
        else:
            logger.debug(''.join(['Failure removing endpoint ', id_end, '.']),)
            return result
Пример #2
0
class CapabilitiesService(BaseService):

    def __init__(self):
        self.rest_client = RestClientEngine()

    def create_capability(self, capability_element):
        result = self.rest_client.executeRequest('createCapability', body=json.dumps(capability_element))
        if result['headers']['status'] == '201':
            logger.debug(' '.join(['Capability', capability_element['api_name'], 'created.']),)
            return {'result': 'Capability Created'}
        else:
            logger.debug(' '.join(['Capability', capability_element['api_name'], 'creation failed.']),)
            return result

    def remove_capability(self, capability):
        result = self.rest_client.executeRequest('removeCapability', api_name=capability)
        if result['headers']['status'] == '204':
            logger.debug(' '.join(['Capability', capability, 'removed.']),)
            return {'result': 'Capability Removed'}
        else:
            logger.debug(' '.join(['Capability', capability, 'deletion failed.']),)
            return result

    def modify_capability(self, capability_element):
        api_name = capability_element.pop('api_name', None)

        result = self.rest_client.executeRequest('modifyCapability', api_name=api_name,
                                                 body=json.dumps(capability_element))
        if result['headers']['status'] == '200':
            logger.debug(' '.join(['Capability', api_name, 'updated.']),)
            return {'result': 'Capability Updated'}
        else:
            logger.debug(' '.join(['Capability', api_name, 'updated failed.']),)
            return result

    def get_capabilities(self):
        result = self.rest_client.executeRequest('getCapabilities')
        if result['headers']['status'] == '200':
            logger.debug('Capabilities retrieve successfully.')
            return json.loads(result['body'])
        else:
            logger.debug('Failure retrieving capabilities.')
            return result
 def test_rest_client_engine_should_be_a_singleton(self):
     rest_client = RestClientEngine(self.conn)
     self.assertEquals(id(self.rest_client_engine), id(rest_client))
 def setUp(self):
     self.rest_client_engine = RestClientEngine(self.conn)
class RestClientEngineTest(unittest.TestCase):

    GENERAL_PARAMETERS = {
        'headers': {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'User-Agent': 'SD-Web'
        },
        'base_url': 'http://*****:*****@patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample1_should_perform_get_request(self):
        self.rest_client_engine.executeRequest('sampleEndpoint1',
                                               api_name='sample')
        self.conn.request_get.assert_called_once_with('apis/sample/endpoints',
                                                      headers={
                                                          'Content-Type':
                                                          'application/json',
                                                          'Accept':
                                                          'application/json',
                                                          'User-Agent':
                                                          'SD-Web'
                                                      },
                                                      args=None)

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample1_without_path_variable_should_raise_exception(
            self):
        self.assertRaises(NotFoundException,
                          self.rest_client_engine.executeRequest,
                          'sampleEndpoint1')

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample1_should_perform_post_request(self):
        self.rest_client_engine.executeRequest('sampleEndpoint2')
        self.conn.request_post.assert_called_once_with('apis',
                                                       body=None,
                                                       headers={
                                                           'Custom-Header':
                                                           'My Custom Header',
                                                           'Content-Type':
                                                           'application/json',
                                                           'Accept':
                                                           'application/json',
                                                           'User-Agent':
                                                           'SD-Web'
                                                       })

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample5_unknown_method_should_raise_exception(self):
        self.assertRaises(NotFoundException,
                          self.rest_client_engine.executeRequest,
                          'sampleEndpoint5')

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_unknown_request_should_raise_exception(self):
        self.assertRaises(NotFoundException,
                          self.rest_client_engine.executeRequest,
                          'unknownRequest')

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample3_should_perform_put_request(self):
        self.rest_client_engine.executeRequest('sampleEndpoint3',
                                               api_name='api',
                                               id='123')
        self.conn.request_put.assert_called_once_with('apis/api/123',
                                                      body=None,
                                                      headers={
                                                          'Custom-Header':
                                                          'My Custom Header',
                                                          'Content-Type':
                                                          'application/json',
                                                          'Accept':
                                                          'application/json',
                                                          'User-Agent':
                                                          'SD-Web'
                                                      })

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample4_should_perform_delete_request(self):
        self.rest_client_engine.executeRequest('sampleEndpoint4')
        self.conn.request_delete.assert_called_once_with(
            'apis',
            headers={
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'User-Agent': 'SD-Web'
            })

    @patch('commons.rest_client.rest_client_engine.GENERAL_PARAMETERS',
           GENERAL_PARAMETERS)
    @patch('commons.rest_client.rest_client_engine.REQUESTS', REQUESTS)
    def test_rest_client_sample4_whit_custom_headers_should_perform_delete_request(
            self):
        self.rest_client_engine.executeRequest('sampleEndpoint4',
                                               headers={'MyHeader': 'MyValue'})
        self.conn.request_delete.assert_called_once_with(
            'apis',
            headers={
                'MyHeader': 'MyValue',
                'Content-Type': 'application/json',
                'Accept': 'application/json',
                'User-Agent': 'SD-Web'
            })

    def tearDown(self):
        self.conn.reset_mock()
Пример #6
0
 def __init__(self):
     self.rest_client = RestClientEngine()