def digest(self): """ This method retrieves information about all known items within this collection. A cache is then formed that provides an index by AOS unique ID, and another by user-defined item name. Returns: a list of all known items; each item is the dictionary of item data. """ got = requests.get(self.url, headers=self.api.headers) if not got.ok: raise SessionRqstError(resp=got) body = got.json() aos_1_0 = semantic_version.Version('1.0', partial=True) self._cache.clear() self._cache['list'] = list() self._cache['names'] = list() self._cache['by_%s' % self.DISPLAY_NAME] = dict() self._cache['by_%s' % self.UNIQUE_ID] = dict() items = body[ 'items'] if self.api.version['semantic'] > aos_1_0 else body for item in items: self._add_item(item) return self._cache['by_%s' % self.DISPLAY_NAME]
def digest(self): """ This method retrieves information about all known items within this collection. A cache is then formed that provides an index by AOS unique ID, and another by user-defined item name. Returns: a list of all known items; each item is the dictionary of item data. """ got = self.api.requests.get(self.url) if not got.ok: raise SessionRqstError(resp=got) body = got.json() self._cache.clear() self._cache['list'] = list() self._cache['names'] = list() self._cache['by_%s' % self.LABEL] = dict() self._cache['by_%s' % self.UNIQUE_ID] = dict() items = body['items'] for item in items: self._add_item(item) return self._cache['by_%s' % self.LABEL]
def put_updated(): got = self.api.requests.put(self.url, json=dict(display_name='Default Pool', devices=has_devices)) if not got.ok: raise SessionRqstError( message='unable to update approved list: %s' % got.text, resp=got)
def digest(self): got = self.api.requests.get(self.url) if not got.ok: raise SessionRqstError(resp=got, message="error fetching slots") get_name = itemgetter('name') body = got.json() self._cache['list'] = body['items'] self._cache['names'] = map(get_name, self._cache['list']) self._cache['by_name'] = {get_name(i): i for i in self._cache['list']}
def digest(self): got = requests.get("%s/slots" % self.blueprint.url, headers=self.api.headers) if not got.ok: raise SessionRqstError(resp=got, message="error fetching slots") get_name = itemgetter('name') body = got.json() aos_1_0 = semantic_version.Version('1.0', partial=True) self._cache['list'] = body['items'] if self.api.version['semantic'] > aos_1_0 else body self._cache['names'] = map(get_name, self._cache['list']) self._cache['by_name'] = {get_name(i): i for i in self._cache['list']}
def delete(self): """ Deletes the item from the AOS server Raises: SessionRqstError - when API error NoExistsError - when item does not actually exist """ got = self.api.requests.delete(self.url) if not got.ok: raise SessionRqstError(message='unable to delete item: %s' % got.reason, resp=got) self.collection -= self
def read(self): """ This method will retrieve the current parameter/slot value. Returns: The value, as a dict, of the parameter. """ got = self.api.requests.get(self.url) if not got.ok: raise SessionRqstError(resp=got, message='unable to get value on slot: %s' % self.name) self._param['value'] = copy(got.json()) return self._param['value']
def read(self): """ Retrieves the item value from the AOS-server. Raises: SessionRqstError: upon REST call error Returns: a copy of the item value, usually a :class:`dict`. """ got = self.api.requests.get(self.url) if not got.ok: raise SessionRqstError(resp=got, message='unable to get item name: %s' % self.name) self.datum = copy(got.json()) return self.datum
def write(self, value=None): """ Used to write the item value back to the AOS-server. Raises: SessionRqstError: upon HTTP request issue """ if not self.exists: return self.create(value=value) got = self.api.requests.put(self.url, json=value or self.datum) if not got.ok: raise SessionRqstError(message='unable to update: %s' % got.reason, resp=got) self.read()
def contents(self): """ Property accessor to blueprint contents. :getter: returns the current blueprint data :class:`dict` :deletter: removes the blueprint from AOS-server Raises: SessionRqstError: upon issue with HTTP requests """ got = self.api.requests.get(self.url) if not got.ok: raise SessionRqstError(message='unable to get blueprint contents', resp=got) return got.json()
def update(self, merge_value): """ This method will issue a PATCH to the slot value so that the caller can merge the provided `merge_value` with the existing value. Once the PATCH completes, this method with then invoke :meth:`read` to retrieve the fully updated value. Args: merge_value: data value to merge with existing slot value. Raises: SessionRqstError - if error with API request """ got = self.api.requests.patch(self.url, json=merge_value) if not got.ok: raise SessionRqstError(message='unable to patch slot: %s' % self.name, resp=got) self.read()
def write(self, replace_value): """ This method writes (PUT) the given parameter value. If you are looking to merge/update a value into the existing parameter, use the :meth:`update` instead. Args: replace_value (dict): the new parameter value; will replace anything that previously exists. Raises: SesssionRqstError - upon API request error """ got = self.api.requests.put(self.url, json=replace_value) if not got.ok: raise SessionRqstError(message='unable to write slot: %s' % self.name, resp=got) self._param['value'] = replace_value
def create(self, value=None, replace=False): """ Creates a new item using the `value` provided. Args: value (dict): item value dictionary. replace (bool): determine if this method should replace and existing item with the same name. Raises: - SessionError: upon any HTTP request issue. - DuplicateError: attempting to create an existing item Returns: the instance to the new collection item """ # check to see if this item currently exists, using the name/URI # when this instances was instantiated from the collection; *not* # from the `value` data. def throw_duplicate(name): raise DuplicateError( "'{}' already exists in collection: {}.".format( name, self.collection.URI)) if self.exists: if not replace: throw_duplicate(self.name) self.delete() # the caller can either pass the new data to this method, or they # could have already assigned it into the :prop:`datum`. This # latter approach should be discouraged. if value is not None: self.datum = copy(value) # now check to see if the new value/name exists. if the datum # does not include the lable value, we need to auto-set it from # the instance name value. new_name = self.datum.get(self.collection.LABEL) if not new_name: self.datum[self.collection.LABEL] = self.name if new_name in self.collection: throw_duplicate(new_name) # at this point we should be good to execute the POST and # create the new item in the server got = self.api.requests.post(self.collection.url, json=self.datum) if not got.ok: raise SessionRqstError(message='unable to create: %s' % got.reason, resp=got) body = got.json() self.datum[self.collection.UNIQUE_ID] = body[self.collection.UNIQUE_ID] # now add this item to the parent collection so it can be used by other # invocations self.collection += self return self
def user_config(self, value): got = self.api.requests.put(self.url, json=dict(user_config=value)) if not got.ok: raise SessionRqstError(message='unable to set user_config', resp=got)
def get(self): got = self.api.requests.get(self.url) if not got.ok: raise SessionRqstError(got) return got.json()