Ejemplo n.º 1
0
 def object(self, value):
     if value is not None:
         if not isinstance(value, Agent) and \
                 not isinstance(value, Group) and \
                 not isinstance(value, SubStatement) and \
                 not isinstance(value, StatementRef) and \
                 not isinstance(value, Activity):
             if isinstance(value, dict):
                 if 'object_type' in value or 'objectType' in value:
                     if 'objectType' in value:
                         value['object_type'] = value['objectType']
                         value.pop('objectType')
                     if value['object_type'] == 'Agent':
                         value = Agent(value)
                     elif value['object_type'] == 'SubStatement':
                         value = SubStatement(value)
                     elif value['object_type'] == 'StatementRef':
                         value = StatementRef(value)
                     elif value['object_type'] == 'Activity':
                         value = Activity(value)
                     elif value['object_type'] == 'Group':
                         value = Group(value)
                     else:
                         value = Activity(value)
                 else:
                     value = Activity(value)
     self._object = value
Ejemplo n.º 2
0
    def retrieve_activity_profile_ids(self, activity, since=None):
        """Retrieve activity profile id(s) with the specified parameters

        :param activity: Activity object of desired activity profiles
        :type activity: :class:`tincan.activity.Activity`
        :param since: Retrieve activity profile id's since this time
        :type since: str | unicode
        :return: LRS Response object with list of retrieved activity profile id's as content
        :rtype: :class:`tincan.lrs_response.LRSResponse`
        """
        if not isinstance(activity, Activity):
            activity = Activity(activity)

        request = HTTPRequest(method="GET", resource="activities/profile")
        request.query_params["activityId"] = activity.id

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

        lrs_response = self._send_request(request)

        if lrs_response.success:
            lrs_response.content = json.loads(lrs_response.data)

        return lrs_response
Ejemplo n.º 3
0
 def retrieve_activity_profile(self, activity, profile_id):
     """Retrieve activity profile with the specified parameters
     :param activity: Activity object of the desired activity profile
     :type activity: :class:`tincan.activity.Activity`
     :param profile_id: UUID of the desired profile
     :type profile_id: str | unicode
     :return: LRS Response object with an activity profile doc as content
     :rtype: :class:`tincan.lrs_response.LRSResponse`
     """
     if not isinstance(activity, Activity):
         activity = Activity(activity)
     request = HTTPRequest(method="GET",
                           resource="activities/profile",
                           ignore404=True)
     request.query_params = {
         "profileId": profile_id,
         "activityId": activity.id
     }
     lrs_response = self._send_request(request)
     if lrs_response.success:
         doc = ActivityProfileDocument(id=profile_id,
                                       content=lrs_response.data,
                                       activity=activity)
         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
Ejemplo n.º 4
0
 def retrieve_state_ids(self,
                        activity,
                        agent,
                        registration=None,
                        since=None):
     """Retrieve state id's from the LRS with the provided parameters
     :param activity: Activity object of desired states
     :type activity: :class:`tincan.activity.Activity`
     :param agent: Agent object of desired states
     :type agent: :class:`tincan.agent.Agent`
     :param registration: Registration UUID of desired states
     :type registration: str | unicode
     :param since: Retrieve state id's since this time
     :type since: str | unicode
     :return: LRS Response object with the retrieved state id's 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")
     request.query_params = {
         "activityId": activity.id,
         "agent": agent.to_json(self.version)
     }
     if registration is not None:
         request.query_params["registration"] = registration
     if since is not None:
         request.query_params["since"] = since
     lrs_response = self._send_request(request)
     if lrs_response.success:
         lrs_response.content = json.loads(lrs_response.data)
     return lrs_response
Ejemplo n.º 5
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
Ejemplo n.º 6
0
 def activity(self, value):
     if not isinstance(value, Activity) and value is not None:
         try:
             value = Activity(value)
         except:
             raise TypeError(
                 f"Property 'activity' in 'tincan.{self.__class__.__name__}' must be set with a type "
                 f"that can be constructed into an tincan.Activity object.")
     self._activity = value
Ejemplo n.º 7
0
    def _activity_or_list(value):
        """Tries to convert value to :class:`tincan.ActivityList`

        :setter type: :class:`tincan.ActivityList`
        :rtype: :class:`tincan.ActivityList`
        """
        result = value
        if value is not None and not isinstance(value, ActivityList):
            try:
                result = ActivityList([Activity(value)])
            except (TypeError, AttributeError):
                result = ActivityList(value)
        return result
Ejemplo n.º 8
0
    def _delete_state(self,
                      activity,
                      agent,
                      state_id=None,
                      registration=None,
                      etag=None):
        """Private method to delete a specified state from the LRS

        :param activity: Activity object of state to be deleted
        :type activity: :class:`tincan.activity.Activity`
        :param agent: Agent object of state to be deleted
        :type agent: :class:`tincan.agent.Agent`
        :param state_id: UUID of state to be deleted
        :type state_id: str | unicode
        :param registration: registration UUID of state to be deleted
        :type registration: str | unicode
        :param etag: etag of state to be deleted
        :type etag: str | unicode
        :return: LRS Response object with deleted state 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="DELETE", resource="activities/state")

        if etag is not None:
            request.headers["If-Match"] = etag

        request.query_params = {
            "activityId": activity.id,
            "agent": agent.to_json(self.version)
        }
        if state_id is not None:
            request.query_params["stateId"] = state_id

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

        lrs_response = self._send_request(request)

        return lrs_response