def _delete_ticket(self, _id, _rev): """ Delete a ticket from CouchDB. """ url = '''%(scheme)s%(host)s:%(port)s/%(db)s/%(docid)s''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db, 'docid': _id } url = url.encode('utf-8') params = {'rev': _rev} self.debug('[DEBUG][CouchDB] _delete_ticket(), url: %s' % url) self.debug('[DEBUG][CouchDB] _delete_ticket(), params: %s' % str(params)) httpClient = self.httpClientFactory(self.reactor) response = yield httpClient.delete( url, params=params, auth=(self._couch_user, self._couch_passwd), headers=Headers({'Accept': ['application/json']})) response = yield http_status_filter(response, [(200, 200)], CouchDBError) resp_text = yield treq.content(response) defer.returnValue(None)
def _fetch_ticket(self, ticket): """ Fetch a ticket representation from CouchDB. """ url = '''%(scheme)s%(host)s:%(port)s/%(db)s/_design/views/_view/get_ticket''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db } url = url.encode('utf-8') params = {'key': json.dumps(ticket.encode('utf-8'))} self.debug("[DEBUG][CouchDB] _fetch_ticket(), url: %s" % url) self.debug("[DEBUG][CouchDB] _fetch_ticket(), params: %s" % str(params)) httpClient = self.httpClientFactory(self.reactor) response = yield httpClient.get( url, params=params, headers=Headers({'Accept': ['application/json']}), auth=(self._couch_user, self._couch_passwd)) response = yield http_status_filter(response, [(200, 200)], CouchDBError) doc = yield treq.json_content(response) rows = doc[u'rows'] if len(rows) > 0: entry = rows[0][u'value'] entry[u'expires'] = parse_date(entry[u'expires']) if u'pgts' in entry: entry[u'pgts'] = set(entry[u'pgts']) defer.returnValue(entry) defer.returnValue(None)
def _update_ticket(self, _id, _rev, data): """ Update a ticket in CouchDB. """ data[u'expires'] = data[u'expires'].strftime('%Y-%m-%dT%H:%M:%S') if u'pgts' in data: data[u'pgts'] = list(data[u'pgts']) url = '''%(scheme)s%(host)s:%(port)s/%(db)s/%(docid)s''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db, 'docid': _id} url = url.encode('utf-8') data['_rev'] = _rev.encode('utf-8') try: doc = json.dumps(data) except Exception as ex: self.debug("[DEBUG][CouchDB] Failed to serialze doc:\n%s" % (str(data))) raise httpClient = self.httpClientFactory(self.reactor) self.debug('''[DEBUG][CouchDB] request_method="PUT" url="{0}"'''.format(url)) self.debug('''[DEBUG][CouchDB] document => {0}'''.format(data)) response = yield httpClient.put( url, data=doc, auth=(self._couch_user, self._couch_passwd), headers=Headers({ 'Accept': ['application/json'], 'Content-Type': ['application/json']})) response = yield http_status_filter(response, [(201,201)], CouchDBError) doc = yield treq.json_content(response) defer.returnValue(None)
def _fetch_ticket(self, ticket): """ Fetch a ticket representation from CouchDB. """ url = '''%(scheme)s%(host)s:%(port)s/%(db)s/_design/views/_view/get_ticket''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db} url = url.encode('utf-8') params = {'key': json.dumps(ticket.encode('utf-8'))} self.debug("[DEBUG][CouchDB] _fetch_ticket(), url: %s" % url) self.debug("[DEBUG][CouchDB] _fetch_ticket(), params: %s" % str(params)) httpClient = self.httpClientFactory(self.reactor) response = yield httpClient.get(url, params=params, headers=Headers({'Accept': ['application/json']}), auth=(self._couch_user, self._couch_passwd)) response = yield http_status_filter(response, [(200,200)], CouchDBError) doc = yield treq.json_content(response) rows = doc[u'rows'] if len(rows) > 0: entry = rows[0][u'value'] entry[u'expires'] = parse_date(entry[u'expires']) if u'pgts' in entry: entry[u'pgts'] = set(entry[u'pgts']) defer.returnValue(entry) defer.returnValue(None)
def _clean_expired(self): """ Clean up any expired tickets. """ try: url = '''%(scheme)s%(host)s:%(port)s/%(db)s/_design/views/_view/get_by_expires''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db } url = url.encode('utf-8') earliest = datetime.datetime.today() - datetime.timedelta( seconds=self._expired_margin) params = { 'descending': 'true', 'startkey': json.dumps(earliest.strftime("%Y-%m-%dT%H:%M:%S")), } self.debug("[DEBUG][CouchDB] _clean_expired(), url: %s" % url) self.debug("[DEBUG][CouchDB] _clean_expired(), params: %s" % str(params)) httpClient = self.httpClientFactory(self.reactor) response = yield httpClient.get( url, params=params, headers=Headers({'Accept': ['application/json']}), auth=(self._couch_user, self._couch_passwd)) response = yield http_status_filter(response, [(200, 200)], CouchDBError) doc = yield treq.json_content(response) rows = doc[u'rows'] if len(rows) > 0: del_docs = [] for row in rows: ticket_id = row[u'value'] try: yield self._expireTicket(ticket_id) except CouchDBError as ex: log.msg( "CouchDB error while attempting to delete expired tickets." ) log.err(ex) except Exception as ex: log.err(ex)
def _update_ticket(self, _id, _rev, data): """ Update a ticket in CouchDB. """ data[u'expires'] = data[u'expires'].strftime('%Y-%m-%dT%H:%M:%S') if u'pgts' in data: data[u'pgts'] = list(data[u'pgts']) url = '''%(scheme)s%(host)s:%(port)s/%(db)s/%(docid)s''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db, 'docid': _id } url = url.encode('utf-8') data['_rev'] = _rev.encode('utf-8') try: doc = json.dumps(data) except Exception as ex: self.debug("[DEBUG][CouchDB] Failed to serialze doc:\n%s" % (str(data))) raise httpClient = self.httpClientFactory(self.reactor) self.debug( '''[DEBUG][CouchDB] request_method="PUT" url="{0}"'''.format(url)) self.debug('''[DEBUG][CouchDB] document => {0}'''.format(data)) response = yield httpClient.put(url, data=doc, auth=(self._couch_user, self._couch_passwd), headers=Headers({ 'Accept': ['application/json'], 'Content-Type': ['application/json'] })) response = yield http_status_filter(response, [(201, 201)], CouchDBError) doc = yield treq.json_content(response) defer.returnValue(None)
def _delete_ticket(self, _id, _rev): """ Delete a ticket from CouchDB. """ url = '''%(scheme)s%(host)s:%(port)s/%(db)s/%(docid)s''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db, 'docid': _id} url = url.encode('utf-8') params = {'rev': _rev} self.debug('[DEBUG][CouchDB] _delete_ticket(), url: %s' % url) self.debug('[DEBUG][CouchDB] _delete_ticket(), params: %s' % str(params)) httpClient = self.httpClientFactory(self.reactor) response = yield httpClient.delete( url, params=params, auth=(self._couch_user, self._couch_passwd), headers=Headers({'Accept': ['application/json']})) response = yield http_status_filter(response, [(200,200)], CouchDBError) resp_text = yield treq.content(response) defer.returnValue(None)
def _clean_expired(self): """ Clean up any expired tickets. """ try: url = '''%(scheme)s%(host)s:%(port)s/%(db)s/_design/views/_view/get_by_expires''' % { 'scheme': self._scheme, 'host': self._couch_host, 'port': self._couch_port, 'db': self._couch_db} url = url.encode('utf-8') earliest = datetime.datetime.today() - datetime.timedelta(seconds=self._expired_margin) params = { 'descending': 'true', 'startkey': json.dumps(earliest.strftime("%Y-%m-%dT%H:%M:%S")), } self.debug("[DEBUG][CouchDB] _clean_expired(), url: %s" % url) self.debug("[DEBUG][CouchDB] _clean_expired(), params: %s" % str(params)) httpClient = self.httpClientFactory(self.reactor) response = yield httpClient.get(url, params=params, headers=Headers({'Accept': ['application/json']}), auth=(self._couch_user, self._couch_passwd)) response = yield http_status_filter(response, [(200,200)], CouchDBError) doc = yield treq.json_content(response) rows = doc[u'rows'] if len(rows) > 0: del_docs = [] for row in rows: ticket_id = row[u'value'] try: yield self._expireTicket(ticket_id) except CouchDBError as ex: log.msg("CouchDB error while attempting to delete expired tickets.") log.err(ex) except Exception as ex: log.err(ex)