def DELETE(klass, path='', params=None): fullpath = klass.api_endpoint() + path logger.debug("about to DELETE path %s" % fullpath) if params is not None: logger.debug(" with params %s" % str(params)) response = DELETE(fullpath, params=params) check_status(response) return response.json()
def search(klass, search=None): if search is not None: for key in search.keys(): if not key in klass._valid_search_params: logger.warn("search key '%s' may be ignored" % key) response = klass.GET(params=search) logger.debug("raw search response:") logger.debug(json.dumps(response, sort_keys=True, indent=4)) return SearchResults(klass, response)
def save(self): resource_name = self.singular_resource_name() if hasattr(self, 'resource_id'): data = {resource_name: self._save_params()} new_resource_info = self.__class__.PUT(path=self.resource_id, data=data) self.resource_id = new_resource_info[resource_name]['id'] else: data = {resource_name: self._save_params()} logger.debug("data is %s" % str(data)) new_resource_info = self.__class__.POST(data=data) self.resource_id = new_resource_info[resource_name]['id'] return self # allow chaining
def __init__(self, klass, response): logger.debug("creating SearchResults object for %s:" % (klass.__name__)) logger.debug(json.dumps(response, sort_keys=True, indent=4)) self.total = int(response['meta']['total']) if 'page_input' in response['meta']: self.limit = response['meta']['page_input']['limit'] self.offset = response['meta']['page_input']['offset'] self.search_term = response['meta']['page_input']['search'] else: self.limit = self.total self.offset = 0 self.search_term = None self.raw_results = response[klass._resource_path] self.klass = klass
def find_or_create(klass, **params): results = klass.search(params) total = int(results.total) if total == 1: logger.debug("found unique record") return results[0] elif total == 0: logger.debug("no records found, creating new") obj = klass(**params) logger.debug("about to save obj %s" % obj) logger.debug(str(obj.__dict__)) obj.save() return obj else: logger.debug("total %s" % results.total) raise UnexpectedCondition("multiple objects found")
def PUT(klass, path='', data=None, params=None): fullpath = klass.api_endpoint() + path logger.debug("about to PUT to path %s" % fullpath) if params is not None: logger.debug(" with params %s" % str(params)) if data is not None: logger.debug(" with data %s" % str(data)) response = PUT(fullpath, params=params, data=json.dumps(data)) check_status(response) return response.json()
def DELETE(endpoint, **kwargs): logger.debug("DELETE to %s" % endpoint) return requests.delete(endpoint, headers=headers, **kwargs)
def PUT(endpoint, **kwargs): logger.debug("PUT to %s" % endpoint) return requests.put(endpoint, headers=headers, **kwargs)
def GET(endpoint, **kwargs): logger.debug("GET of %s" % endpoint) return requests.get(endpoint, headers=headers, **kwargs)
def post_init(self): if not hasattr(self, 'notes') and hasattr(self, 'resource_id'): notes_info = self.get_resource_info('notes') logger.debug("notes info:") logger.debug(str(notes_info)) self.notes = []
def add_note(self, text): note_info = self.post_resource_info('notes', data={'note': text}) logger.debug("returned note info is") logger.debug(str(note_info))