Ejemplo n.º 1
0
    def add_template(self, endpoint_template):
        """
        Add a new template for the external API.

        :param six.text_type endpoint_template: a :obj:`IEndpointTemplate` to add to the
            :obj:`IExternalAPIMock` instance
        :raises: ValueError if the endpoint template has already been added
        :raises: TypeError if the endpoint template does not implement
            the expected interface (IEndpointTempalte)
        """
        if IEndpointTemplate.providedBy(endpoint_template):
            key = endpoint_template.id_key

            if key in self.endpoint_templates:
                raise EndpointTemplateAlreadyExists(
                    key + " already exists. Please call update.")

            if endpoint_template.type_key != self.type_key:
                raise InvalidEndpointTemplateServiceType(
                    "template does not match the service type.")

            self.endpoint_templates[key] = endpoint_template
        else:
            raise InvalidEndpointTemplateInterface(
                endpoint_template.__class__.__module__ + "/" +
                endpoint_template.__class__.__name__ +
                " does not implement IEndpointTemplate"
            )
Ejemplo n.º 2
0
    def update_template(self, endpoint_template):
        """
        Update an existing template for the external API.

        :param six.text_type endpoint_template: a :obj:`IEndpointTemplate` to add to the
            :obj:`IExternalAPIMock` instance
        :raises: IndexError if the endpoint template does not already exist
        :raises: TypeError if the endpoint template does not implement
            the expected interface (IEndpointTempalte)
        """
        if IEndpointTemplate.providedBy(endpoint_template):
            key = endpoint_template.id_key

            if key not in self.endpoint_templates:
                raise EndpointTemplateDoesNotExist(
                    "Endpoint template does not exist. Unable to update. The "
                    "template must first be added before it can be updated"
                )

            if endpoint_template.type_key != self.type_key:
                raise InvalidEndpointTemplateServiceType(
                    "template does not match the service type.")

            if self.endpoint_templates[key].id_key != endpoint_template.id_key:
                raise InvalidEndpointTemplateId(
                    "template id must match the id of the template it is "
                    "updating"
                )

            self.endpoint_templates[key] = endpoint_template

        else:
            raise InvalidEndpointTemplateInterface(
                endpoint_template.__class__.__module__ + "/" +
                endpoint_template.__class__.__name__ +
                " does not implement IEndpointTemplate"
            )