Skip to content

Commit aefbbc5

Browse files
committed
Auto-use of api-key through the decorator
1 parent 83acd17 commit aefbbc5

File tree

3 files changed

+136
-97
lines changed

3 files changed

+136
-97
lines changed

codePost_api/helpers.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ def get_available_courses(course_name=None, course_period=None, api_key=None):
6666
result = r.json()
6767

6868
except Exception as exc:
69-
7069
raise RuntimeError(
7170
"""
7271
get_available_courses: Unexpected exception while retrieving
7372
the list of available courses/terms; this could be related
7473
to the API key({:.5}...) being either unavailable, invalid,
7574
or stale:
7675
{}
77-
""".format(api_key, exc)
76+
""".format(_util._robust_str(api_key), exc)
7877
)
7978

8079
# Optionally filter according to the `course_name` parameter
@@ -90,6 +89,7 @@ def get_available_courses(course_name=None, course_period=None, api_key=None):
9089
return list(result)
9190

9291

92+
@_util.api_key_decorator()
9393
def get_course_roster_by_id(course_id, api_key=None):
9494
"""
9595
Returns the course roster, given the course's ID. The course ID
@@ -114,12 +114,14 @@ def get_course_roster_by_id(course_id, api_key=None):
114114
except Exception as exc:
115115
raise RuntimeError(
116116
"""
117-
get_course_roster_by_id: Unexpected exception while retrieving the
118-
course roster from the provided course id({: d}):
117+
get_course_roster_by_id: Unexpected exception while
118+
retrieving the course roster from the provided course
119+
id ({: d}):
119120
{}
120121
""".format(course_id, exc)
121122
)
122123

124+
@_util.api_key_decorator()
123125
def get_course_roster_by_name(course_name, course_period, api_key=None):
124126
"""
125127
Returns the course information and roster for a course, given its
@@ -138,7 +140,7 @@ def get_course_roster_by_name(course_name, course_period, api_key=None):
138140
("get_course_roster_by_name: No course '{course_name}_{course_name}' "
139141
"is available, or codePost API key '{api_key:.5}...' is invalid or "
140142
"does not have access to it.").format(
141-
api_key=api_key,
143+
api_key=_util._robust_str(api_key),
142144
course_name=course_name,
143145
course_period=course_period,
144146
)
@@ -168,6 +170,7 @@ def get_course_roster_by_name(course_name, course_period, api_key=None):
168170

169171
return course_info
170172

173+
@_util.api_key_decorator()
171174
def get_assignment_info_by_id(assignment_id, api_key=None):
172175
"""
173176
Returns the assignment information dictionary, given the assignment's ID.
@@ -190,11 +193,12 @@ def get_assignment_info_by_id(assignment_id, api_key=None):
190193
raise RuntimeError(
191194
"""
192195
get_assignment_info_by_id: Unexpected exception while retrieving the
193-
assignment info from the provided id({: d}):
196+
assignment info from the provided id ({: d}):
194197
{}
195198
""".format(assignment_id, exc)
196199
)
197200

201+
@_util.api_key_decorator()
198202
def get_assignment_info_by_name(course_name, course_period, assignment_name, api_key=None):
199203
"""
200204
Returns the assignment information dictionary, given a(course name, course period,
@@ -217,7 +221,8 @@ def get_assignment_info_by_name(course_name, course_period, assignment_name, api
217221
get_assignment_info_by_name: Either no course with the
218222
specified course({}) and period({}) exists, or the provided
219223
API key({:.5}...) does not have access to it.
220-
""".format(course_name, course_period, api_key)
224+
""".format(course_name, course_period,
225+
_util._robust_str(api_key))
221226
)
222227

223228
elif len(courses) > 1:
@@ -260,6 +265,7 @@ def get_assignment_info_by_name(course_name, course_period, assignment_name, api
260265
return selected_assignment
261266

262267

268+
@_util.api_key_decorator()
263269
def get_assignment_submissions(assignment_id, student=None, grader=None, api_key=None):
264270
"""
265271
Returns the list of submissions of an assignment, provided an assignment ID
@@ -321,6 +327,7 @@ def get_assignment_submissions(assignment_id, student=None, grader=None, api_key
321327

322328
return result
323329

330+
@_util.api_key_decorator()
324331
def get_submission_by_id(submission_id, api_key=None):
325332
"""
326333
Returns the submission information dictionary, given the submission's ID.
@@ -348,6 +355,7 @@ def get_submission_by_id(submission_id, api_key=None):
348355
""".format(submission_id, exc)
349356
)
350357

358+
@_util.api_key_decorator()
351359
def get_file(file_id, api_key=None):
352360
"""
353361
Returns the file given its file ID; the file IDs are provided within a
@@ -377,6 +385,7 @@ def get_file(file_id, api_key=None):
377385
)
378386

379387

388+
@_util.api_key_decorator()
380389
def set_submission_grader(submission_id, grader, api_key=None):
381390
"""
382391
Changes the grader claimed to a submission with a given submission ID.
@@ -415,6 +424,7 @@ def set_submission_grader(submission_id, grader, api_key=None):
415424
)
416425

417426

427+
@_util.api_key_decorator()
418428
def unclaim_submission(submission_id, api_key=None):
419429
"""
420430
Unclaims a submission, given the submission ID. This unsets the associated
@@ -427,6 +437,7 @@ def unclaim_submission(submission_id, api_key=None):
427437
)
428438

429439

440+
@_util.api_key_decorator()
430441
def remove_comments(submission_id=None, file_id=None, api_key=None):
431442
"""
432443
Removes all comments either from the submission with the given submission ID
@@ -491,6 +502,7 @@ def remove_comments(submission_id=None, file_id=None, api_key=None):
491502
return (total_comments == deleted_comments)
492503

493504

505+
@_util.api_key_decorator()
494506
def delete_submission(submission_id, api_key=None):
495507
"""
496508
Deletes the submission with the given submission ID; raises an exception
@@ -520,6 +532,7 @@ def delete_submission(submission_id, api_key=None):
520532
)
521533

522534

535+
@_util.api_key_decorator()
523536
def delete_file(file_id, api_key=None):
524537
"""
525538
Deletes the file with the given file ID; raises an exception
@@ -549,6 +562,7 @@ def delete_file(file_id, api_key=None):
549562
)
550563

551564

565+
@_util.api_key_decorator()
552566
def post_file(submission_id, filename, content, extension, api_key=None):
553567
"""
554568
Uploads a file to a submission.
@@ -589,6 +603,7 @@ def post_file(submission_id, filename, content, extension, api_key=None):
589603
)
590604

591605

606+
@_util.api_key_decorator()
592607
def post_submission(assignment_id, students, files, api_key=None):
593608
"""
594609
Uploads a submission, give the assignment's ID, and a dictionary containing
@@ -663,6 +678,7 @@ def post_submission(assignment_id, students, files, api_key=None):
663678

664679
return submission
665680

681+
@_util.api_key_decorator()
666682
def post_comment(file, text, pointDelta, startChar, endChar, startLine, endLine, rubricComment=None, api_key=None):
667683
"""
668684
Adds comment specified by (startChar, endChar, startLine, endLine) to file
@@ -710,6 +726,7 @@ def post_comment(file, text, pointDelta, startChar, endChar, startLine, endLine,
710726

711727
return comment
712728

729+
@_util.api_key_decorator()
713730
def set_submission_students(submission_id, students, api_key=None):
714731
"""
715732
Modifies the students associated with a submission.
@@ -741,6 +758,7 @@ def set_submission_students(submission_id, students, api_key=None):
741758
""".format(students, submission_id, exc)
742759
)
743760

761+
@_util.api_key_decorator()
744762
def remove_students_from_submission(submission_info, students_to_remove, api_key=None):
745763
"""
746764
Removes students from a submission, and possibly delete the submission if no
@@ -773,6 +791,7 @@ def _submission_list_is_unclaimed(submissions):
773791
return False
774792
return True
775793

794+
@_util.api_key_decorator()
776795
def get_course_grades(course_name, course_period, only_finalized=True, api_key=None):
777796
"""
778797
Returns a dictionary mapping every student in the specified course

codePost_api/util.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ def _is_stringable(obj):
184184
return False
185185
return True
186186

187+
def _robust_str(obj, default="N/A"):
188+
obj_str = default
189+
if _is_stringable(obj):
190+
obj_str = str(obj)
191+
return obj_str
192+
187193
def validate_api_key(api_key, log_outcome=False, caption="", refresh=False):
188194
# type: (str) -> bool
189195
"""
@@ -225,7 +231,7 @@ def invalid_api_key():
225231
if api_key in _checked_api_keys:
226232
if refresh:
227233
if log_outcome:
228-
_logger.info(
234+
_logger.debug(
229235
"API_KEY '{:.5}...' found in cache => PURGING".format(
230236
api_key_str,
231237
caption
@@ -235,7 +241,7 @@ def invalid_api_key():
235241

236242
else:
237243
if log_outcome:
238-
_logger.info(
244+
_logger.debug(
239245
"API_KEY '{:.5}...' {}found in cache.".format(
240246
api_key_str,
241247
caption
@@ -492,7 +498,8 @@ def __init__(self, api_key_override=True):
492498
self._api_key_override = api_key_override
493499

494500
def __call__(self, target_function):
495-
501+
502+
@_functools.wraps(target_function)
496503
def _wrapper(*args, **kwargs):
497504
global _checked_api_keys
498505

0 commit comments

Comments
 (0)