Exemplo n.º 1
0
    def get(self, request):
        """Return http response with all Instances

        Args:

            request: HTTP request

        Returns:

            - code: 200
              content: List of Instances
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            instance_object_list = instance_api.get_all()

            # Serialize object
            return_value = InstanceSerializerModel(instance_object_list,
                                                   many=True)

            # Return response
            return Response(return_value.data)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 2
0
    def get(self, request, pk):
        """Get Instance

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: Instance
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            instance_object = self.get_object(pk)
            # Serialize object
            return_value = InstanceSerializerModel(instance_object)
            # Return response
            return Response(return_value.data)
        except Http404:
            content = {"message": "Instance not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 3
0
    def patch(self, request, pk):
        """Refresh token of an Instance

        Parameters:

            {
                "client_id": "my_client_id",
                "client_secret": "my_client_secret",
                "timeout": "1"
            }

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: Updated Instance
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            instance_object = self.get_object(pk)

            # refresh the token
            instance_object = instance_api.refresh_instance_token(
                instance_object,
                request.data["client_id"],
                request.data["client_secret"],
                request.data["timeout"],
            )
            # Serialize object
            return_value = InstanceSerializerModel(instance_object)
            # Return response
            return Response(return_value.data, status=status.HTTP_200_OK)
        except Http404:
            content = {"message": "Instance not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 4
0
    def patch(self, request, pk):
        """Update the Instance

        Parameters:

            {
                "name": "name"
            }

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: Updated Instance
            - code: 400
              content: Validation error
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            instance_object = self.get_object(pk)
            # Build serializer
            instance_serializer = InstanceSerializerModel(
                instance=instance_object, data=request.data, partial=True)
            # Validation
            instance_serializer.is_valid(True)
            # Save data
            instance_serializer.save()
            # Return response
            return Response(instance_serializer.data,
                            status=status.HTTP_200_OK)
        except Http404:
            content = {"message": "Instance not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except ValidationError as validation_exception:
            content = {"message": validation_exception.detail}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)