class Client: def __init__(self, base_url, username, password): self.api = Api(base_url, username, password) def list(self, table, **kparams): """ get a collection of records by table name. returns a collection of SnowRecord obj. """ records = self.api.list(table, **kparams) return records def get(self, table, sys_id): """ get a single record by table name and sys_id returns a SnowRecord obj. """ record = self.api.get(table, sys_id) return record def update(self, table, sys_id, **kparams): """ update a record via table api, kparams being the dict of PUT params to update. returns a SnowRecord obj. """ record = self.api.update(table, sys_id, **kparams) return record
def __init__(self, base_url, username, password): self.api = Api(base_url, username, password)
class Client: def __init__(self, base_url, username, password): self.api = Api(base_url, username, password) def list(self,table, **kparams): """ get a collection of records by table name. returns a collection of SnowRecord obj. """ result = self.api.list(table, **kparams) records = [] if self.is_error(result): msg = result["error"]["message"] detail = result["error"]["detail"] raise SnowError(msg, detail) for elem in result["result"]: records.append(SnowRecord(table, **elem)) return records def get(self,table, sys_id): """ get a single record by table name and sys_id returns a SnowRecord obj. """ result = self.api.get(table, sys_id) if self.is_error(result): msg = result["error"]["message"] detail = result["error"]["detail"] raise SnowError(msg, detail) return SnowRecord(table, **result["result"]) def resolve_links(self, snow_record): """ Finds any dict values that have 'link' in them, and assoc the new thing in as a replacement for the original <link> value. The replacement is a SnowRecord """ replace_dict = {} for key, value in snow_record.__dict__.items(): if isinstance(value, dict) and value['link']: link = value['link'] linked_response = self.api.req("get", link) replace_dict[key] = {"json": linked_response.json(), "tablename": self.tablename_from_link(link) } for key, value in replace_dict.items(): if replace_dict[key]: data = {} if "result" in replace_dict[key]["json"]: data = replace_dict[key]["json"]["result"] else: # FIXME better raise SnowError("Could not resolve links", [replace_dict, self]) setattr(snow_record, key, SnowRecord(replace_dict[key]["tablename"], **data)) return snow_record def resolve_link(self, snow_record, field_to_resolve): """ Assoc the new thing in as a replacement for the original <link> value in the field specified. The replacement is a SnowRecord. """ link = getattr(snow_record, field_to_resolve)['link'] linked_response = self.api.req("get", link) replacement_info = {"json": linked_response.json(), "tablename": self.tablename_from_link(link) } if "result" in replacement_info["json"]: data = replacement_info["json"]["result"] else: # FIXME better raise SnowError("Could not resolve link", [replacement_info, self]) setattr(snow_record, field_to_resolve, SnowRecord(replacement_info["tablename"], **data)) return snow_record def tablename_from_link(self, link): """ Helper method for URL's that look like /api/now/v1/table/FOO/sys_id etc. """ arr = link.split("/") i = arr.index("table") tn = arr[i+1] return tn def is_error(self, res): if "error" in res: return True elif "result" in res: return False else: raise "parsing the service now responses is hard." def is_not_error(self, res): return not self.is_error(res)