Skip to content

Commit

Permalink
Cache Responses for 24 Hours
Browse files Browse the repository at this point in the history
  • Loading branch information
theAkito committed Aug 23, 2023
1 parent 1892ce7 commit 1c83d13
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Testing API.
* ~~Make SSL/TLS work on Alpine inside Docker~~
* ~~Optimise Docker Compose YAML files~~
* ~~Improve Error Handling on Database Connection Defect~~
* Cache Zoom responses for 1, 12 or 24 hours
* ~~Cache Zoom responses for 1, 12 or 24 hours~~
* Save Contexts in Database
* Minimise Zoom API call amount

Expand Down
90 changes: 55 additions & 35 deletions src/zoominvitr.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,47 +78,67 @@ when isMainModule:
preMeetingsMatchedYes = collect:
for auth in ctx.zoom.authentication:
let
userMail = auth.mail
mailToID = {
userMail: auth.userID
}.toTable
account_id = auth.accountID
client_id = auth.clientID
client_secret = auth.clientSecret
base_bearer_token = encode(&"""{client_id}:{client_secret}""")
bearer_token = "Basic " & base_bearer_token
access_token = post(
&"https://{headerVal_host}/oauth/token?grant_type=account_credentials&account_id={account_id}",
@[
(headerKey_authorization, bearer_token),
(headerKey_contentType, headerVal_contentType),
(headerKey_host, headerVal_host)
].HttpHeaders
).body.parseJson{"access_token"}.getStr
bearer_access_token = &"Bearer {access_token}"
urlZoomMeetings = parseUrl(&"{root_url}users/{mailToID[userMail]}/meetings").edit:
it.query = @[
(key: "type", value: "upcoming_meetings"),
(key: "page_size", value: "100")
].QueryParams
meetingsBody = Request(
url: urlZoomMeetings,
headers: @[
(headerKey_authorization, bearer_access_token),
(headerKey_contentType, headerVal_contentType),
(headerKey_host, headerVal_host)
].HttpHeaders,
verb: "GET"
).fetch().body
meetings = block:
let jMeetingsBody = meetingsBody.parseJson
respondedLast = block:
auth.initZoomResponseIfNotExists
auth.loadZoomResponseTimestamp.toDateTime
dateOfHoursBeforeNow = respondedLast - initDuration(hours = 24 #[ TODO: Make configurable! ]#)
databaseZoomResponseMeetings = auth.loadZoomResponse.meetings
jMeetingsBody = if dateOfHoursBeforeNow < respondedLast and databaseZoomResponseMeetings != string.default:
logger.log(lvlInfo, &"""Loading Zoom response for "E-Mail: {auth.mail}; User ID: {auth.userID}" from database, because last response happened at "{respondedLast.formatWithTimezone(ctx.timeZone)}"!""")
databaseZoomResponseMeetings.parseJson
else:
logger.log(lvlInfo, &"""Loading Zoom response for ""E-Mail: {auth.mail}; User ID: {auth.userID}" from Zoom API, because last response happened at "{respondedLast.formatWithTimezone(ctx.timeZone)}"!""")
let
userMail = auth.mail
mailToID = {
userMail: auth.userID
}.toTable
account_id = auth.accountID
client_id = auth.clientID
client_secret = auth.clientSecret
base_bearer_token = encode(&"""{client_id}:{client_secret}""")
bearer_token = "Basic " & base_bearer_token
access_token = post(
&"https://{headerVal_host}/oauth/token?grant_type=account_credentials&account_id={account_id}",
@[
(headerKey_authorization, bearer_token),
(headerKey_contentType, headerVal_contentType),
(headerKey_host, headerVal_host)
].HttpHeaders
).body.parseJson{"access_token"}.getStr
bearer_access_token = &"Bearer {access_token}"
urlZoomMeetings = parseUrl(&"{root_url}users/{mailToID[userMail]}/meetings").edit:
it.query = @[
(key: "type", value: "upcoming_meetings"),
(key: "page_size", value: "100")
].QueryParams
meetingsBody = Request(
url: urlZoomMeetings,
headers: @[
(headerKey_authorization, bearer_access_token),
(headerKey_contentType, headerVal_contentType),
(headerKey_host, headerVal_host)
].HttpHeaders,
verb: "GET"
).fetch().body
jMeetingsBody = try:
meetingsBody.parseJson
except CatchableError:
logger.log(lvlError, "Failed to parse the following body:\p" & meetingsBody)
logger.log(lvlError, getCurrentException().getStackTrace)
logger.log(lvlError, getCurrentExceptionMsg())
continue
if jMeetingsBody{"code"}.getInt(0) == 124:
logger.log(lvlError, """Authentication to Zoom API failed! Check your configuration file! Did you configure this server via its configuration file, yet?""")
continue
else:
auth.saveZoomResponse(meetingsBody)
jMeetingsBody
meetings = block:
try:
jMeetingsBody.toZoomMeetings.toSeq
except CatchableError:
logger.log(lvlError, "Failed to parse the following body:\p" & meetingsBody)
logger.log(lvlError, "Failed to parse the following body:\p" & pretty jMeetingsBody)
logger.log(lvlError, getCurrentException().getStackTrace)
logger.log(lvlError, getCurrentExceptionMsg())
continue
Expand Down

0 comments on commit 1c83d13

Please sign in to comment.