|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from os import getenv |
| 4 | +from enum import IntEnum |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +CGAPI_BASE_URL = getenv('CGAPI_BASE_URL', 'https://codegra.de/api/v1') |
| 9 | + |
| 10 | + |
| 11 | +class APIRoutes(): |
| 12 | + LOGIN = CGAPI_BASE_URL + '/login' |
| 13 | + COURSES = CGAPI_BASE_URL + '/courses/?extended=true' |
| 14 | + SUBMISSIONS = CGAPI_BASE_URL + '/assignments/{assignment_id}/submissions/' |
| 15 | + FILES = CGAPI_BASE_URL + '/submissions/{submission_id}/files/?owner=auto' |
| 16 | + FILE = CGAPI_BASE_URL + '/submissions/{submission_id}/files/?path={path}&owner=auto' |
| 17 | + FILE_BUF = CGAPI_BASE_URL + '/code/{file_id}' |
| 18 | + FILE_RENAME = CGAPI_BASE_URL + '/code/{file_id}?operation=rename&new_path={new_path}' |
| 19 | + |
| 20 | + |
| 21 | +class APICodes(IntEnum): |
| 22 | + """Internal API codes that are used by :class:`APIException` objects. |
| 23 | + """ |
| 24 | + INCORRECT_PERMISSION = 0 |
| 25 | + NOT_LOGGED_IN = 1 |
| 26 | + OBJECT_ID_NOT_FOUND = 2 |
| 27 | + OBJECT_WRONG_TYPE = 3 |
| 28 | + MISSING_REQUIRED_PARAM = 4 |
| 29 | + INVALID_PARAM = 5 |
| 30 | + REQUEST_TOO_LARGE = 6 |
| 31 | + LOGIN_FAILURE = 7 |
| 32 | + INACTIVE_USER = 8 |
| 33 | + INVALID_URL = 9 |
| 34 | + OBJECT_NOT_FOUND = 10 |
| 35 | + BLOCKED_ASSIGNMENT = 11 |
| 36 | + INVALID_CREDENTIALS = 12 |
| 37 | + INVALID_STATE = 13 |
| 38 | + INVALID_OAUTH_REQUEST = 14 |
| 39 | + DISABLED_FEATURE = 15 |
| 40 | + |
| 41 | + |
| 42 | +class CGAPIException(Exception): |
| 43 | + def __init__(self, response): |
| 44 | + data = response.json() |
| 45 | + super(CGAPIException, self).__init__(data['message']) |
| 46 | + |
| 47 | + self.description = data['description'] |
| 48 | + self.message = data['message'] |
| 49 | + self.code = data['code'] |
| 50 | + |
| 51 | + |
| 52 | +class CGAPI(): |
| 53 | + def __init__(self, username, password): |
| 54 | + r = requests.post( |
| 55 | + APIRoutes.LOGIN, |
| 56 | + json={ |
| 57 | + 'username': username, |
| 58 | + 'password': password, |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + if r.status_code >= 400: |
| 63 | + raise CGAPIException(r) |
| 64 | + |
| 65 | + self.access_token = r.json()['access_token'] |
| 66 | + |
| 67 | + def get_default_headers(self): |
| 68 | + return { |
| 69 | + 'Authorization': 'Bearer ' + self.access_token, |
| 70 | + } |
| 71 | + |
| 72 | + def get_courses(self): |
| 73 | + r = requests.get(APIRoutes.COURSES, headers=self.get_default_headers()) |
| 74 | + |
| 75 | + if r.status_code >= 400: |
| 76 | + raise CGAPIException(r) |
| 77 | + |
| 78 | + return r.json() |
| 79 | + |
| 80 | + def get_submissions(self, assignment_id): |
| 81 | + url = APIRoutes.SUBMISSIONS.format(assignment_id=assignment_id) |
| 82 | + r = requests.get(url, headers=self.get_default_headers()) |
| 83 | + |
| 84 | + if r.status_code >= 400: |
| 85 | + raise CGAPIException(r) |
| 86 | + |
| 87 | + return r.json() |
| 88 | + |
| 89 | + def get_submission_files(self, submission_id): |
| 90 | + url = APIRoutes.FILES.format(submission_id=submission_id) |
| 91 | + r = requests.get(url, headers=self.get_default_headers()) |
| 92 | + |
| 93 | + if r.status_code >= 400: |
| 94 | + raise CGAPIException(r) |
| 95 | + |
| 96 | + return r.json() |
| 97 | + |
| 98 | + def get_file_meta(self, submission_id, path): |
| 99 | + url = APIRoutes.FILE.format(submission_id=submission_id, path=path) |
| 100 | + r = requests.get(url, headers=self.get_default_headers()) |
| 101 | + |
| 102 | + if r.status_code >= 400: |
| 103 | + raise CGAPIException(r) |
| 104 | + |
| 105 | + return r.json() |
| 106 | + |
| 107 | + def create_file(self, submission_id, path, buf=None): |
| 108 | + url = APIRoutes.FILE.format(submission_id=submission_id, path=path) |
| 109 | + r = requests.post(url, headers=self.get_default_headers(), data=buf) |
| 110 | + |
| 111 | + if r.status_code >= 400: |
| 112 | + raise CGAPIException(r) |
| 113 | + |
| 114 | + return r.json() |
| 115 | + |
| 116 | + def rename_file(self, file_id, new_path): |
| 117 | + url = APIRoutes.FILE_RENAME.format(file_id=file_id, new_path=new_path) |
| 118 | + r = requests.patch(url, headers=self.get_default_headers()) |
| 119 | + |
| 120 | + if r.status_code >= 400: |
| 121 | + raise CGAPIException(r) |
| 122 | + |
| 123 | + return r.json() |
| 124 | + |
| 125 | + def get_file(self, file_id): |
| 126 | + url = APIRoutes.FILE_BUF.format(file_id=file_id) |
| 127 | + r = requests.get(url, headers=self.get_default_headers()) |
| 128 | + |
| 129 | + if r.status_code >= 400: |
| 130 | + raise CGAPIException(r) |
| 131 | + |
| 132 | + return r.content |
| 133 | + |
| 134 | + def patch_file(self, file_id, buf): |
| 135 | + url = APIRoutes.FILE_BUF.format(file_id=file_id) |
| 136 | + r = requests.patch(url, headers=self.get_default_headers(), data=buf) |
| 137 | + |
| 138 | + if r.status_code >= 400: |
| 139 | + raise CGAPIException(r) |
| 140 | + |
| 141 | + return r.json() |
| 142 | + |
| 143 | + def delete_file(self, file_id): |
| 144 | + url = APIRoutes.FILE_BUF.format(file_id=file_id) |
| 145 | + r = requests.delete(url, headers=self.get_default_headers()) |
| 146 | + |
| 147 | + if r.status_code >= 400: |
| 148 | + raise CGAPIException(r) |
0 commit comments