Ejemplo n.º 1
0
    def _get_swagger_spec(self, endpoint_name: str) -> Spec:
        """Get Swagger spec of specified service."""
        logic_module = self._get_logic_module(endpoint_name)
        schema_url = utils.get_swagger_url_by_logic_module(logic_module)

        if schema_url not in self._specs:
            try:
                # Use stored specification of the module
                spec_dict = logic_module.api_specification

                # Pull specification of the module from its service and store it
                if spec_dict is None:
                    response = utils.get_swagger_from_url(schema_url)
                    spec_dict = response.json()
                    logic_module.api_specification = spec_dict
                    logic_module.save()

            except URLError:
                raise URLError(f'Make sure that {schema_url} is accessible.')

            swagger_spec = Spec.from_dict(spec_dict,
                                          config=self.SWAGGER_CONFIG)
            self._specs[schema_url] = swagger_spec

        return self._specs[schema_url]
Ejemplo n.º 2
0
    def update_api_specification(self, request, *args, **kwargs):
        """
        Updates the API specification of given logic module
        """
        instance = self.get_object()
        schema_url = utils.get_swagger_url_by_logic_module(instance)

        response = utils.get_swagger_from_url(schema_url)
        spec_dict = response.json()
        data = {'api_specification': spec_dict}

        serializer = self.get_serializer(instance, data=data, partial=True)
        serializer.is_valid(raise_exception=True)
        self.perform_update(serializer)

        if getattr(instance, '_prefetched_objects_cache', None):
            # If 'prefetch_related' has been applied to a queryset, we need to
            # forcibly invalidate the prefetch cache on the instance.
            instance._prefetched_objects_cache = {}

        return Response(serializer.data)
Ejemplo n.º 3
0
    def get_aggregate_swagger(self) -> dict:
        """
        Get swagger files associated with the aggregates.

        :return: a dict of swagger spec
        """
        swagger_apis = {}
        if 'apis' in self.configuration:  # Check if apis is in the config file
            for endpoint_name, schema_url in self.configuration['apis'].items(
            ):
                if endpoint_name not in swagger_apis:
                    # Get the swagger.json
                    try:
                        # Use stored specification of the module
                        spec_dict = LogicModule.objects.values_list(
                            'api_specification', flat=True).filter(
                                endpoint_name=endpoint_name).first()

                        # Pull specification of the module from its service and store it
                        if spec_dict is None:
                            response = utils.get_swagger_from_url(schema_url)
                            spec_dict = response.json()
                            LogicModule.objects.filter(
                                endpoint_name=endpoint_name).update(
                                    api_specification=spec_dict)

                        swagger_apis[endpoint_name] = {
                            'spec': spec_dict,
                            'url': schema_url
                        }
                    except ConnectionError as error:
                        logger.warning(error)
                    except TimeoutError as error:
                        logger.warning(error)
                    except ValueError:
                        logger.info(
                            'Cannot remove {} from errors'.format(schema_url))
        return swagger_apis
Ejemplo n.º 4
0
 def test_unavailable_logic_module_connection_exception(
         self, mock_request_get):
     mock_request_get.side_effect = ConnectionError
     with pytest.raises(ConnectionError):
         assert get_swagger_from_url('http://microservice:5000')
Ejemplo n.º 5
0
 def test_unavailable_logic_module_timeout_exception(
         self, mock_request_get):
     mock_request_get.side_effect = TimeoutError
     with pytest.raises(TimeoutError):
         assert get_swagger_from_url('http://example.com')