Refactoring

- split into separate endpoints
This commit is contained in:
Peter Šurda 2024-04-15 19:23:15 +08:00
parent 92287547aa
commit 7346fe600c
Signed by: PeterSurda
GPG Key ID: 3E47497CF67ABB95

View File

@ -15,32 +15,27 @@ GITEA_REPO_URL="https://git.bitmessage.org/api/v1"
combined = {}
def get_combined(token):
for q in ("review_requested", "assigned"):
def retrieve(token, relationship):
if relationship not in ("review_requested", "assigned"):
raise cherrypy.HTTPError(401, 'Unauthorized')
req = urllib.request.Request(GITEA_REPO_URL
+ f"/repos/issues/search?state=open&{q}=true")
+ f"/repos/issues/search?state=open&{relationship}=true")
req.add_header("Accept", "application/json")
req.add_header("Authorization", "token " + token)
timestamp=datetime.datetime.now()
print(f"{timestamp} Requesting and parsing {q}")
with urllib.request.urlopen(req) \
as response:
retval = {}
with urllib.request.urlopen(req) as response:
issues = json.load(response)
for issue in issues:
_id = issue['id']
if _id not in combined:
combined[_id] = issue
combined[_id]['categories'] = []
if q not in combined[_id]['categories']:
combined[_id]['categories'].append(q)
timestamp=datetime.datetime.now()
print(f"{timestamp} Done processing {q}")
return combined
retval[_id] = issue
retval[_id]['categories'] = [relationship]
return retval
def process_combined(combined):
def process(retrieved):
cal = Calendar()
for _id, issue in combined.items():
for _id, issue in retrieved.items():
todo = Todo()
todo['uid'] = _id
todo['dtstamp'] = issue['created_at']
@ -60,8 +55,7 @@ def get_token(input_token):
return token
class Root:
@cherrypy.expose
def todo(self):
def _authenticate(self):
cherrypy.response.headers['WWW-Authenticate'] = \
'Basic realm="ICS access"'
cherrypy.response.headers['Content-Type'] = \
@ -74,10 +68,23 @@ class Root:
token = get_token(authorization)
if not token:
raise cherrypy.HTTPError(401, 'Unauthorized')
combined = get_combined(token)
if not combined:
return token
@cherrypy.expose
def assigned(self):
token = self._authenticate()
retrieved = retrieve(token, "assigned")
if not retrieved:
raise cherrypy.HTTPError(401, 'Unauthorized')
return(process_combined(combined))
return(process(retrieved))
@cherrypy.expose
def review_requested(self):
token = self._authenticate()
retrieved = retrieve(token, "assigned")
if not retrieved:
raise cherrypy.HTTPError(401, 'Unauthorized')
return(process(retrieved))
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0',