def retrieve_state(self, activity, agent, state_id, registration=None):
        """Retrieve state from LRS with the provided parameters

        :param activity: Activity object of desired state
        :type activity: :class:`tincan.activity.Activity`
        :param agent: Agent object of desired state
        :type agent: :class:`tincan.agent.Agent`
        :param state_id: UUID of desired state
        :type state_id: str | unicode
        :param registration: registration UUID of desired state
        :type registration: str | unicode
        :return: LRS Response object with retrieved state document as content
        :rtype: :class:`tincan.lrs_response.LRSResponse`
        """
        if not isinstance(activity, Activity):
            activity = Activity(activity)

        if not isinstance(agent, Agent):
            agent = Agent(agent)

        request = HTTPRequest(
            method="GET",
            resource="activities/state",
            ignore404=True
        )

        request.query_params = {
            "activityId": activity.id,
            "agent": agent.to_json(self.version),
            "stateId": state_id
        }

        if registration is not None:
            request.query_params["registration"] = registration

        lrs_response = self._send_request(request)

        if lrs_response.success:
            doc = StateDocument(
                id=state_id,
                content=lrs_response.data,
                activity=activity,
                agent=agent
            )
            if registration is not None:
                doc.registration = registration

            headers = lrs_response.response.getheaders()
            if "lastModified" in headers and headers["lastModified"] is not None:
                doc.timestamp = headers["lastModified"]
            if "contentType" in headers and headers["contentType"] is not None:
                doc.content_type = headers["contentType"]
            if "etag" in headers and headers["etag"] is not None:
                doc.etag = headers["etag"]

            lrs_response.content = doc

        return lrs_response
 def test_delete_state(self):
     doc = StateDocument(activity=self.activity,
                         agent=self.agent,
                         id="test")
     response = self.lrs.delete_state(doc)
     self.assertIsInstance(response, LRSResponse)
     self.assertTrue(response.success)
Beispiel #3
0
    def retrieve_state(self, activity, agent, state_id, registration=None):
        """Retrieve state from LRS with the provided parameters

        :param activity: Activity object of desired state
        :type activity: :class:`tincan.activity.Activity`
        :param agent: Agent object of desired state
        :type agent: :class:`tincan.agent.Agent`
        :param state_id: UUID of desired state
        :type state_id: str | unicode
        :param registration: registration UUID of desired state
        :type registration: str | unicode
        :return: LRS Response object with retrieved state document as content
        :rtype: :class:`tincan.lrs_response.LRSResponse`
        """
        if not isinstance(activity, Activity):
            activity = Activity(activity)

        if not isinstance(agent, Agent):
            agent = Agent(agent)

        request = HTTPRequest(
            method="GET",
            resource="activities/state",
            ignore404=True
        )

        request.query_params = {
            "activityId": activity.id,
            "agent": agent.to_json(self.version),
            "stateId": state_id
        }

        if registration is not None:
            request.query_params["registration"] = registration

        lrs_response = self._send_request(request)

        if lrs_response.success:
            doc = StateDocument(
                id=state_id,
                content=lrs_response.data,
                activity=activity,
                agent=agent
            )
            if registration is not None:
                doc.registration = registration

            headers = lrs_response.response.getheaders()
            if "lastModified" in headers and headers["lastModified"] is not None:
                doc.timestamp = headers["lastModified"]
            if "contentType" in headers and headers["contentType"] is not None:
                doc.content_type = headers["contentType"]
            if "etag" in headers and headers["etag"] is not None:
                doc.etag = headers["etag"]

            lrs_response.content = doc

        return lrs_response
 def test_save_state(self):
     doc = StateDocument(activity=self.activity,
                         agent=self.agent,
                         id="test",
                         content=bytearray("Test value", encoding="utf-8"))
     saveResp = self.lrs.save_state(doc)
     self.assertIsInstance(saveResp, LRSResponse)
     self.assertTrue(saveResp.success)
     response = self.lrs.retrieve_state(activity=self.activity,
                                        agent=self.agent,
                                        state_id="test")
     self.assertIsInstance(response, LRSResponse)
     self.assertTrue(response.success)
     self._vars_verifier(response.content, doc)