Exemple #1
0
 def _verify(self, number, label):
     params = {
         "course__id": self.course.pk,
         "book__id": self.book.pk,
     }
     if number is not None:
         params["rank"] = number
     elif label is not None:
         params["label"] = label
     else:
         raise APIError("Chapter label or Chapter number must be provided.")
     response = self.client.get(CHAPTERS_API, params=params)
     result = singleton_or_none(response)
     if result is None:
         raise APIError("Input chapter not found.")
     self.pk = result["id"]
     self.number = result["rank"]
     self.label = result["label"]
Exemple #2
0
 def _verify(self):
     response = self.client.get(COURSE_API, params={"label": self.label})
     result = singleton_or_none(response)
     if result is None:
         raise APIError("The requested course label does not exist. "
                        "You might not be a member of the requested "
                        "course if it exists.")
     self.pk = result["id"]
     self.autograder_bucket = result["s3_autograder_bucket"]
     self.number = result["number"]
Exemple #3
0
def issue(mime):
    if mime == 'example':
        raise APIError(f'default API script execution failed')

    # issue warning
    warnings.warn(f'{mime}: API script execution failed', APIWarning, 2)

    ## remove API entry
    # del API_DICT[mime]
    return EXIT_FAILURE
Exemple #4
0
 def _verify(self):
     params = {
         "course__label": self.course.label,
         "label": self.label,
     }
     response = self.client.get(BOOK_API, params=params)
     result = singleton_or_none(response)
     if result is None:
         raise APIError("Input book not found.")
     self.pk = result["id"]
     self.is_booklet = bool(result["is_booklet"])
Exemple #5
0
 def _verify(self):
     params = {
         "name": self.name,
     }
     response = self.client.get(LAB_API.format(self.course.pk),
                                params=params)
     result = singleton_or_none(response)
     if result is None:
         raise APIError("Invalid homework name.")
     self.pk = result["id"]
     self.uuid = result["uuid"]
Exemple #6
0
 def _verify(self, number):
     # If we have a booklet, then don't look at number.
     if self.book.is_booklet:
         params = {"book__id": self.book.pk}
     else:
         params = {"book__id": self.book.pk, "rank": number}
     response = self.client.get(PARTS_API, params=params)
     result = singleton_or_none(response)
     if result is None:
         raise APIError("Input part not found.")
     self.pk = result["id"]
Exemple #7
0
def main():
    '''
    The main application. Purpose is to cycle around the functions used to
    interract with the API in a forever-loop. Exits loop and terminates
    application, when variables href, method and schema are set as None.

    No return value.

    Exceptions. All API calls are done over a requests session. If any
    exceptions specific to the session are caught, then the application
    restarts from entry point. Any API specific exceptions are caught within
    the method handling the app continues from a state before the API call,
    caused the exception.

    ConnectionError             In case of a connection error of the TCP/IP
                                stack.
    requests.Timeout            If a timeout occured with the HTTP request.
    requests.TooManyRedirects   In case the request experiences too many
                                redirects.

    APIError                    In case the API replies with any != 2xx status
                                code
    '''

    breakout = False
    while True and not breakout:
        with requests.Session() as s:
            SERVER_URL, href, method = api_entry(s)
            SERVER_URL = SERVER_URL.strip("/api/")
            # print("DEBUG MAIN:\t\tFull URL: ", SERVER_URL + href, "\r\n")
            try:
                body = None
                while True:
                    if body is not None:
                        # Print UI
                        print_line()
                        print("\r\nCurrent route: {}".format(href))
                        href, method, schema = process_body(body)
                        # print("DEBUG MAIN:\t\thref after process is: ", href)
                        # print("DEBUG MAIN:\t\tmethod after"
                        #       "process is: ", method)
                    if method == "get":
                        # print("DEBUG MAIN:\t\thref for GET is: ", href)
                        # print("DEBUG MAIN:\t\tGETting: ", SERVER_URL + href)
                        try:
                            # Resfresh UI
                            get_body = get_resource(s, SERVER_URL + href)
                        except APIError as err:
                            print("\n", err)
                            input("Press Enter to continue...")
                        except JSONDecodeError:
                            print("Server response was not a "
                                  "valid JSON document.")
                            input("Press Enter to continue...")
                        else:
                            body = get_body
                    elif method == "post":
                        # print("DEBUG MAIN:\t\thref for POST is: ", href)
                        # print("DEBUG MAIN:\t\tPOSTing: ", SERVER_URL + href)
                        try:
                            # Post new resource
                            resp = post_resource(s, SERVER_URL + href, schema)
                            if resp.status_code == 201:
                                print(
                                    "\r\nResource created: ",
                                    resp.headers["Location"].strip(
                                        SERVER_URL)  # noqa: E501
                                )
                                # Ask user to get data on newly created
                                # resource, or stay at current resource-state
                                while True:
                                    str_in = input("\r\nExamine newly created "
                                                   "resource? (y/n): ") \
                                                   .strip().lower()
                                    if str_in == "y":
                                        # Get newly created resource data
                                        # and return body
                                        body = get_resource(
                                            s, resp.headers["Location"]
                                        )  # noqa: E501
                                        break
                                    elif str_in == "n":
                                        # Resfresh UI
                                        body = get_resource(
                                            s, SERVER_URL + href)  # noqa: E501
                                        break
                                    else:
                                        print("Select \"y\" for yes or "
                                              "\"n\n for no, please.")
                            else:
                                raise APIError(resp.status_code, resp.content)
                        except APIError as err:
                            print("\n", err)
                            input("Press Enter to continue...")
                    elif method == "put":
                        # print("DEBUG MAIN:\t\thref for PUT is: ", href)
                        # print("DEBUG MAIN:\t\tPUTing: ", SERVER_URL + href)
                        try:
                            # Make changes
                            resp = put_resource(s, SERVER_URL + href, schema)
                            if resp.status_code == 204:
                                print("\r\nResource modified: ", href)
                                # Resfresh UI
                                print("Refreshing UI...")
                                body = get_resource(s, SERVER_URL + href)
                            else:
                                raise APIError(resp.status_code, resp.content)
                        except APIError as err:
                            print("\n", err)
                            input("Press Enter to continue...")
                    elif method == "delete":
                        # print("DEBUG MAIN:\t\thref for DELETE is: ", href)
                        # print("DEBUG MAIN:\t\tDELETing: ", SERVER_URL + href)
                        try:
                            # Delete resource
                            resp = delete_resource(s, SERVER_URL + href)
                            if resp.status_code == 204:
                                print("\r\nResource deleted: ", href)
                                # If successfull, go back one level in href
                                href = extract_prev_href(href)
                                print("Falling back to parent resource...")
                                body = get_resource(s, SERVER_URL + href)
                            else:
                                raise APIError(resp.status_code, resp.content)
                        except APIError as err:
                            print("\n", err)
                            input("Press Enter to continue...")
                    # Terminates program
                    elif href is None and method is None and schema is None:
                        breakout = True
                        break
            except ConnectionError:
                print(
                    "Get request to {} experienced a connection error.".format(
                        SERVER_URL + href))
                input("Press Enter to continue...")
            except requests.Timeout:
                print("Get request to {} timed out.".format(SERVER_URL + href))
                input("Press Enter to continue...")
            except requests.TooManyRedirects:
                print(
                    "Get request to {} experienced too many redirects.".format(
                        SERVER_URL + href))
                input("Press Enter to continue...")