Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from UCBoulder/new-field-xml
Browse files Browse the repository at this point in the history
#DS-491 #Updated Add two new fields from Prefect flow to API interface
  • Loading branch information
bencroft authored Sep 28, 2022
2 parents a004c07 + 740e206 commit 4c13f25
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
7 changes: 5 additions & 2 deletions app/graphql/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def make_request(
low_cost_flag
no_cost_flag
oer_flag
cc_id
item_id
ClassBookRequirement {
code
}
Expand All @@ -88,8 +90,9 @@ def make_request(

if r.status_code == 200:
if r.json().get("data"):
return r.status_code, flatten_book_records(
r.json()["data"]["books_v0_2_Book"]
return (
r.status_code,
flatten_book_records(r.json()["data"]["books_v0_2_Book"]),
)
else:
return 422, {"Unable to parse results."}
Expand Down
18 changes: 15 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import datetime
import logging
import logging.config
import random
import secrets
import string
import time
Expand Down Expand Up @@ -38,14 +37,27 @@ async def log_requests(request: Request, call_next):
Tidy bit of logging assist to show how long each call takes.
Found here: https://philstories.medium.com/fastapi-logging-f6237b84ea64
"""
idem = "".join(random.choices(string.ascii_uppercase + string.digits, k=6))

# Create idem with secrets module instead of random module
# which is unsuitable for security/cryptographic purposes
string_source = string.ascii_uppercase + string.digits
idem = secrets.choice(string.ascii_uppercase)
idem += secrets.choice(string.digits)

for _ in range(4):
idem += secrets.choice(string_source)

char_list = list(idem)
secrets.SystemRandom().shuffle(char_list)
idem = "".join(char_list)

logger.info("rid=%s start request path=%s", idem, request.url.path)
start_time = time.time()

response = await call_next(request)

process_time = (time.time() - start_time) * 1000
formatted_process_time = "{0:.2f}".format(process_time)
formatted_process_time = f"{process_time:.2f}"
logger.info(
"rid=%s completed_in=%sms status_code=%s",
idem,
Expand Down
2 changes: 2 additions & 0 deletions app/utility/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def flatten_book_record(book):
result.session = book.Session.code
result.term = book.Session.Term.code
result.requirement = book.ClassBookRequirement.code
result.cc_id = book.cc_id
result.item_id = book.item_id

return result.to_dict()

Expand Down
4 changes: 3 additions & 1 deletion test/flattened_output1.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
"low_cost_flag": "N",
"no_cost_flag": "N",
"oer_flag": "N",
"requirement": "R"
"requirement": "R",
"cc_id": "null",
"item_id": "null"
}
]
}
2 changes: 2 additions & 0 deletions test/graphql_output1.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"low_cost_flag": "N",
"no_cost_flag": "N",
"oer_flag": "N",
"cc_id": "null",
"item_id": "null",
"ClassBookRequirement": {
"code": "R"
}
Expand Down

0 comments on commit 4c13f25

Please sign in to comment.