Skip to content

Commit

Permalink
Use new_quizzes and graphql keywords (#653)
Browse files Browse the repository at this point in the history
* Use `new_quizzes` and `graphql` keywords

Based on #612 and as an issue in #619, this adds the `new_quizzes` and
`graphql` keyword endpoints in the requester module. Methods which
specify these locations will have the correct URL supplied by the
library in the `_url` parameter for the requester.

Calls from the `canvas`, `course`, and `new_quizzes` modules have been
updated to use they keywords rather than a formatted URL string.

All tests passing.

* Better variable checks

Rearrange to check for `_url` first, then assign the request url more
explicitely.

* fix silly typos

---------

Co-authored-by: Matthew Emond <[email protected]>
  • Loading branch information
bennettscience and Thetwam authored May 12, 2024
1 parent c1e8556 commit 4b4fbd8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion canvasapi/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ def graphql(self, query, variables=None, **kwargs):
_kwargs=combine_kwargs(**kwargs)
+ [("query", query), ("variables", variables)],
# Needs to call special endpoint without api/v1
_url=self.__requester.original_url + "/api/graphql",
_url="graphql",
json=True,
)

Expand Down
6 changes: 3 additions & 3 deletions canvasapi/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def create_new_quiz(self, **kwargs):
response = self._requester.request(
"POST",
endpoint,
_url=self._requester.original_url + "/api/quiz/v1/" + endpoint,
_url="new_quizzes",
_kwargs=combine_kwargs(**kwargs),
)
response_json = response.json()
Expand Down Expand Up @@ -1758,7 +1758,7 @@ def get_new_quiz(self, assignment, **kwargs):
response = self._requester.request(
"GET",
endpoint,
_url=self._requester.original_url + "/api/quiz/v1/" + endpoint,
_url="new_quizzes",
_kwargs=combine_kwargs(**kwargs),
)
response_json = response.json()
Expand All @@ -1783,7 +1783,7 @@ def get_new_quizzes(self, **kwargs):
self._requester,
"GET",
endpoint,
_url_override=self._requester.original_url + "/api/quiz/v1/" + endpoint,
_url_override="new_quizzes",
_kwargs=combine_kwargs(**kwargs),
)

Expand Down
4 changes: 2 additions & 2 deletions canvasapi/new_quiz.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def delete(self, **kwargs):
response = self._requester.request(
"DELETE",
endpoint,
_url=self._requester.original_url + "/api/quiz/v1/" + endpoint,
_url="new_quizzes",
_kwargs=combine_kwargs(**kwargs),
)
response_json = response.json()
Expand All @@ -44,7 +44,7 @@ def update(self, **kwargs):
response = self._requester.request(
"PATCH",
endpoint,
_url=self._requester.original_url + "/api/quiz/v1/" + endpoint,
_url="new_quizzes",
_kwargs=combine_kwargs(**kwargs),
)
response_json = response.json()
Expand Down
27 changes: 22 additions & 5 deletions canvasapi/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self, base_url, access_token):
# Preserve the original base url and add "/api/v1" to it
self.original_url = base_url
self.base_url = base_url + "/api/v1/"
self.new_quizzes_url = base_url + "/api/quiz/v1/"
self.graphql = base_url + "/api/graphql"
self.access_token = access_token
self._session = requests.Session()
self._cache = []
Expand Down Expand Up @@ -145,10 +147,16 @@ def request(
:param use_auth: Optional flag to remove the authentication
header from the request.
:type use_auth: bool
:param _url: Optional argument to send a request to a URL
outside of the Canvas API. If this is selected and an
endpoint is provided, the endpoint will be ignored and
only the _url argument will be used.
:param _url: Optional argument to specify a request type to Canvas
or to send a request to a URL outside of the Canvas API.
If set to "new_quizzes", the new quizzes endpoint will be used.
If set to "graphql", a graphql POST request will be sent.
If any string URL is provided, it will be used instead of the
base REST URL.
If omitted or set to None, the base_url for the instance REST
endpoint will be used.
If this is selected and an endpoint is provided, the endpoint
will be ignored and only the `_url` argument will be used..
:type _url: str
:param _kwargs: A list of 2-tuples representing processed
keyword arguments to be sent to Canvas as params or data.
Expand All @@ -159,7 +167,16 @@ def request(
:type json: `bool`
:rtype: :class:`requests.Response`
"""
full_url = _url if _url else "{}{}".format(self.base_url, endpoint)
# Check for specific URL endpoints available from Canvas. If not
# specified, pass the given URL and move on.
if not _url:
full_url = "{}{}".format(self.base_url, endpoint)
elif _url == "new_quizzes":
full_url = "{}{}".format(self.new_quizzes_url, endpoint)
elif _url == "graphql":
full_url = self.graphql
else:
full_url = _url

if not headers:
headers = {}
Expand Down

0 comments on commit 4b4fbd8

Please sign in to comment.