def retrieve_agent_profile(self, agent, profile_id): """Retrieve agent profile with the specified parameters :param agent: Agent object of the desired agent profile :type agent: :class:`tincan.agent.Agent` :param profile_id: UUID of the desired agent profile :type profile_id: str | unicode :return: LRS Response object with an agent profile doc as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(agent, Agent): agent = Agent(agent) request = HTTPRequest(method="GET", resource="agents/profile", ignore404=True) request.query_params = { "profileId": profile_id, "agent": agent.to_json(self.version) } lrs_response = self._send_request(request) if lrs_response.success: doc = AgentProfileDocument(id=profile_id, content=lrs_response.data, agent=agent) 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 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
def retrieve_agent_profile_ids(self, agent, since=None): """Retrieve agent profile id(s) with the specified parameters :param agent: Agent object of desired agent profiles :type agent: :class:`tincan.agent.Agent` :param since: Retrieve agent profile id's since this time :type since: str | unicode :return: LRS Response object with list of retrieved agent profile id's as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(agent, Agent): agent = Agent(agent) request = HTTPRequest( method="GET", resource="agents/profile" ) request.query_params["agent"] = agent.to_json(self.version) 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
def retrieve_agent_profile_ids(self, agent, since=None): """Retrieve agent profile id(s) with the specified parameters :param agent: Agent object of desired agent profiles :type agent: :class:`tincan.agent.Agent` :param since: Retrieve agent profile id's since this time :type since: str | unicode :return: LRS Response object with list of retrieved agent profile id's as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(agent, Agent): agent = Agent(agent) request = HTTPRequest(method="GET", resource="agents/profile") request.query_params["agent"] = agent.to_json(self.version) 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
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 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
def actor(self, value): if value is not None and not isinstance(value, Agent) and not isinstance(value, Group): 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'] == 'Group': value = Group(value) else: value = Agent(value) else: value = Agent(value) self._actor = value
def instructor(self, value): if value is not None and not isinstance(value, Group) and not isinstance(value, Agent): try: value = Agent(value) except (TypeError, AttributeError): value = Group(value) self._instructor = value
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
def agent(self, value): if not isinstance(value, Agent) and value is not None: try: value = Agent(value) except: raise TypeError( f"Property 'agent' in 'tincan.{self.__class__.__name__}' must be set with a type " f"that can be constructed into a tincan.Agent object.") self._agent = value
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
def retrieve_agent_profile(self, agent, profile_id): """Retrieve agent profile with the specified parameters :param agent: Agent object of the desired agent profile :type agent: :class:`tincan.agent.Agent` :param profile_id: UUID of the desired agent profile :type profile_id: str | unicode :return: LRS Response object with an agent profile doc as content :rtype: :class:`tincan.lrs_response.LRSResponse` """ if not isinstance(agent, Agent): agent = Agent(agent) request = HTTPRequest( method="GET", resource="agents/profile", ignore404=True ) request.query_params = { "profileId": profile_id, "agent": agent.to_json(self.version) } lrs_response = self._send_request(request) if lrs_response.success: doc = AgentProfileDocument( id=profile_id, content=lrs_response.data, agent=agent ) 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 member(self, value): newmembers = AgentList() if value is not None: if isinstance(value, list): for k in value: if not isinstance(k, Agent): newmembers.append(Agent(k)) else: newmembers.append(k) else: newmembers = AgentList(value) self._member = newmembers
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
def addmember(self, value): """Adds a single member to this group's list of members. Tries to convert to :class:`tincan.Agent` :param value: The member to add to this group :type value: :class:`tincan.Agent` """ if value is not None and not isinstance(value, Agent): value = Agent(value) self._member.append(value)
def authority(self, value): if value is not None and not isinstance(value, Agent): value = Agent(value) self._authority = value