Example #1
0
 def get_files(self, course: Course) -> File:
     cache = self._check_cache(course)
     if cache:
         for file in cache.get_iter():
             yield file
     else:
         raise ResourceDoesNotExist("File tab is not supported.")
Example #2
0
    def yield_take_snapshot(self):
        course = self.course
        request_batcher = RequestBatcher(course)

        yield (0, '请稍候', '正在获取文件列表')
        files = request_batcher.get_files()
        if files is None:
            raise ResourceDoesNotExist("File tab is not supported.")

        folders = request_batcher.get_folders()

        for _, file in files.items():
            folder = normalize_path(folders[file.folder_id].full_name) + "/"
            if folder.startswith("course files/"):
                folder = folder[len("course files/"):]
            snapshot_file = from_canvas_file(file)
            filename = f'{folder}{normalize_path(snapshot_file.name, file_regex)}'
            self.add_to_snapshot(filename, snapshot_file)

        print(f'  {len(files)} files in total')
        yield (0.1, None, f'共 {len(files)} 个文件')

        if self.with_link:
            yield (None, '正在解析链接', None)
            pages = request_batcher.get_pages() or []
            for page in pages:
                key = f'pages/{normalize_path(page.title, file_regex)}.html'
                value = SnapshotLink(page.title, page.html_url, "Page")
                self.add_to_snapshot(key, value)
            print(f'  {len(pages)} pages in total')
            yield (0.2, '请稍候', f'共 {len(pages)} 个链接')
Example #3
0
    def request(self,
                method,
                endpoint=None,
                headers=None,
                use_auth=True,
                _url=None,
                **kwargs):
        """
        Make a request to the Canvas API and return the response.

        :param method: The HTTP method for the request.
        :type method: str
        :param endpoint: The endpoint to call.
        :type endpoint: str
        :param headers: Optional HTTP headers to be sent with the request.
        :type headers: dict
        :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.
        :type _url: str
        :rtype: str
        """
        full_url = _url if _url else "%s%s" % (self.base_url, endpoint)

        if not headers:
            headers = {}

        if use_auth:
            auth_header = {'Authorization': 'Bearer %s' % (self.access_token)}
            headers.update(auth_header)

        if method == 'GET':
            req_method = self._get_request
        elif method == 'POST':
            req_method = self._post_request
        elif method == 'DELETE':
            req_method = self._delete_request
        elif method == 'PUT':
            req_method = self._put_request

        response = req_method(full_url, headers, kwargs)

        if response.status_code == 400:
            raise BadRequest(response.json())
        elif response.status_code == 401:
            if 'WWW-Authenticate' in response.headers:
                raise InvalidAccessToken(response.json())
            else:
                raise Unauthorized(response.json())
        elif response.status_code == 404:
            raise ResourceDoesNotExist('Not Found')
        elif response.status_code == 500:
            raise CanvasException(
                "API encountered an error processing your request")

        return response
    def take_snapshot(self):
        """Take a snapshot

        Raises:
            ResourceDoesNotExist: this exception will be raised if file tab is not available

        Returns:
            dict: snapshot of Canvas in `SnapshotFile` or `SnapshotLink` type.
        """
        course = self.course
        request_batcher = RequestBatcher(course)

        files = request_batcher.get_files()
        if files is None:
            raise ResourceDoesNotExist("File tab is not supported.")

        folders = request_batcher.get_folders()

        for _, file in files.items():
            folder = folders[file.folder_id].full_name + "/"
            if folder.startswith("course files/"):
                folder = folder[len("course files/"):]
            snapshot_file = from_canvas_file(file)
            filename = f'{folder}{snapshot_file.name}'
            self.add_to_snapshot(filename, snapshot_file)

        print(f'  {len(files)} files in total')
        if self.with_link:
            pages = request_batcher.get_pages() or []
            for page in pages:
                key = f'pages/{page.title}.html'
                value = SnapshotLink(page.title, page.html_url, "Page")
                self.add_to_snapshot(key, value)
            print(f'  {len(pages)} pages in total')

        return self.snapshot
Example #5
0
    def request(self,
                method,
                endpoint=None,
                headers=None,
                use_auth=True,
                _url=None,
                _kwargs=None,
                json=False,
                **kwargs):
        """
        Make a request to the Canvas API and return the response.

        :param method: The HTTP method for the request.
        :type method: str
        :param endpoint: The endpoint to call.
        :type endpoint: str
        :param headers: Optional HTTP headers to be sent with the request.
        :type headers: dict
        :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.
        :type _url: str
        :param _kwargs: A list of 2-tuples representing processed
            keyword arguments to be sent to Canvas as params or data.
        :type _kwargs: `list`
        :param json: Whether or not to treat the data as json instead of form data.
            currently only the POST request of GraphQL is using this parameter.
            For all other methods it's just passed and ignored.
        :type json: `bool`
        :rtype: str
        """
        full_url = _url if _url else "{}{}".format(self.base_url, endpoint)

        if not headers:
            headers = {}

        if use_auth:
            auth_header = {
                "Authorization": "Bearer {}".format(self.access_token)
            }
            headers.update(auth_header)

        # Convert kwargs into list of 2-tuples and combine with _kwargs.
        _kwargs = _kwargs or []
        _kwargs.extend(kwargs.items())

        # Do any final argument processing before sending to request method.
        for i, kwarg in enumerate(_kwargs):
            kw, arg = kwarg

            # Convert boolean objects to a lowercase string.
            if isinstance(arg, bool):
                _kwargs[i] = (kw, str(arg).lower())

            # Convert any datetime objects into ISO 8601 formatted strings.
            elif isinstance(arg, datetime):
                _kwargs[i] = (kw, arg.isoformat())

        # Determine the appropriate request method.
        if method == "GET":
            req_method = self._get_request
        elif method == "POST":
            req_method = self._post_request
        elif method == "DELETE":
            req_method = self._delete_request
        elif method == "PUT":
            req_method = self._put_request
        elif method == "PATCH":
            req_method = self._patch_request

        # Call the request method
        logger.info("Request: {method} {url}".format(method=method,
                                                     url=full_url))
        logger.debug("Headers: {headers}".format(
            headers=pformat(clean_headers(headers))))

        if _kwargs:
            logger.debug("Data: {data}".format(data=pformat(_kwargs)))

        response = req_method(full_url, headers, _kwargs, json=json)
        logger.info("Response: {method} {url} {status}".format(
            method=method, url=full_url, status=response.status_code))
        logger.debug("Headers: {headers}".format(
            headers=pformat(clean_headers(response.headers))))

        try:
            logger.debug("Data: {data}".format(data=pformat(response.json())))
        except ValueError:
            logger.debug("Data: {data}".format(data=pformat(response.text)))

        # Add response to internal cache
        if len(self._cache) > 4:
            self._cache.pop()

        self._cache.insert(0, response)

        # Raise for status codes
        if response.status_code == 400:
            raise BadRequest(response.text)
        elif response.status_code == 401:
            if "WWW-Authenticate" in response.headers:
                raise InvalidAccessToken(response.json())
            else:
                raise Unauthorized(response.json())
        elif response.status_code == 403:
            raise Forbidden(response.text)
        elif response.status_code == 404:
            raise ResourceDoesNotExist("Not Found")
        elif response.status_code == 409:
            raise Conflict(response.text)
        elif response.status_code == 422:
            raise UnprocessableEntity(response.text)
        elif response.status_code > 400:
            # generic catch-all for error codes
            raise CanvasException(
                "Encountered an error: status code {}".format(
                    response.status_code))

        return response
Example #6
0
    def request(
            self, method, endpoint=None, headers=None, use_auth=True,
            _url=None, _kwargs=None, **kwargs):
        """
        Make a request to the Canvas API and return the response.

        :param method: The HTTP method for the request.
        :type method: str
        :param endpoint: The endpoint to call.
        :type endpoint: str
        :param headers: Optional HTTP headers to be sent with the request.
        :type headers: dict
        :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.
        :type _url: str
        :param _kwargs: A list of 2-tuples representing processed
            keyword arguments to be sent to Canvas as params or data.
        :type _kwargs: `list`
        :rtype: str
        """
        full_url = _url if _url else "{}{}".format(self.base_url, endpoint)

        if not headers:
            headers = {}

        if use_auth:
            auth_header = {'Authorization': 'Bearer {}'.format(self.access_token)}
            headers.update(auth_header)

        # Convert kwargs into list of 2-tuples and combine with _kwargs.
        _kwargs = _kwargs or []
        _kwargs.extend(kwargs.items())

        # Do any final argument processing before sending to request method.
        for i, kwarg in enumerate(_kwargs):
            kw, arg = kwarg

            # Convert boolean objects to a lowercase string.
            if isinstance(arg, bool):
                _kwargs[i] = (kw, str(arg).lower())

            # Convert any datetime objects into ISO 8601 formatted strings.
            elif isinstance(arg, datetime):
                _kwargs[i] = (kw, arg.isoformat())

        # Determine the appropriate request method.
        if method == 'GET':
            req_method = self._get_request
        elif method == 'POST':
            req_method = self._post_request
        elif method == 'DELETE':
            req_method = self._delete_request
        elif method == 'PUT':
            req_method = self._put_request

        # Call the request method
        response = req_method(full_url, headers, _kwargs)

        # Add response to internal cache
        if len(self._cache) > 4:
            self._cache.pop()

        self._cache.insert(0, response)

        # Raise for status codes
        if response.status_code == 400:
            raise BadRequest(response.text)
        elif response.status_code == 401:
            if 'WWW-Authenticate' in response.headers:
                raise InvalidAccessToken(response.json())
            else:
                raise Unauthorized(response.json())
        elif response.status_code == 403:
            raise Forbidden(response.text)
        elif response.status_code == 404:
            raise ResourceDoesNotExist('Not Found')
        elif response.status_code == 500:
            raise CanvasException("API encountered an error processing your request")

        return response