-
Notifications
You must be signed in to change notification settings - Fork 19
Add challenge language endpoints #60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
e2d7868
Add structure for challenges
takos22 15a4763
Add piston language API client
takos22 ffea92f
Add piston language and version checker
takos22 0215802
Add challenge language models
takos22 08d07fe
Add piston service closing in app shutdown
takos22 930363b
Add challenge language routes
takos22 ec1949b
Update models submodule version
takos22 ebb549a
Fix piston service
takos22 08aa58d
Pin aiohttp to 3.x
takos22 304d216
Add some tests for challenges
takos22 2073024
Fix bug
takos22 a1fa157
Add tests for PATCH endpoint
takos22 5d429df
Merge branch 'staging' into challenges
takos22 db67b0e
Fix bug
takos22 29551c1
Update Pipfile.lock
takos22 0b6e100
Update services
takos22 7504b8c
Update routes
takos22 4da5b77
Update tests
takos22 72c0f40
Update PISTON_URL env var
takos22 1e3f683
Fix linting
takos22 41668ce
Merge branch 'staging' of https://github.com/Tech-With-Tim/API into c…
takos22 a6a65da
Update Pipfile.lock
takos22 5de6515
Update routes
takos22 38e328e
Add tests for delete endpoint
takos22 b1bee8a
Update UPDATE route
takos22 77b0b2c
Update challenge manager role fixture
takos22 064c6f7
Update query formatting
SylteA 8c95501
piston_url is no longer optional.
SylteA be4f676
Update PISTON_URL config
takos22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule models
updated
10 files
| +6 −1 | __init__.py | |
| +3 −1 | challenges/__init__.py | |
| +23 −13 | challenges/challenge.py | |
| +0 −36 | challenges/challenge_submission.py | |
| +32 −0 | challenges/language.py | |
| +38 −0 | challenges/submission.py | |
| +5 −3 | migrations/0001_init_schema.down.sql | |
| +23 −8 | migrations/0001_init_schema.up.sql | |
| +2 −0 | permissions/__init__.py | |
| +11 −1 | permissions/challenges.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import logging | ||
| from typing import Any, Dict, List | ||
|
|
||
| import config | ||
| from api.services import http | ||
|
|
||
|
|
||
| __all__ = ("get_runtimes", "get_runtimes_dict", "get_runtime", "Runtime") | ||
|
|
||
| log = logging.getLogger() | ||
|
|
||
| _base_url: str = ( | ||
| config.piston_url().rstrip("/") + "/" | ||
| ) # make sure there's a / at the end | ||
|
|
||
|
|
||
| async def _make_request(method: str, endpoint: str, data: Any = None) -> Any: | ||
| async with http.session.request( | ||
| method, | ||
| _base_url + endpoint, | ||
| json=data, | ||
| raise_for_status=True, | ||
| ) as response: | ||
| return await response.json() | ||
|
|
||
|
|
||
| async def get_runtimes() -> List["Runtime"]: | ||
| """Get a list of all available runtimes.""" | ||
| runtimes = await _make_request("GET", "runtimes") | ||
| return [Runtime(runtime) for runtime in runtimes] | ||
|
|
||
|
|
||
| async def get_runtimes_dict() -> Dict[str, List["Runtime"]]: | ||
| """Get a dictionary of language names and aliases mapped to a list of | ||
| all the runtimes with that name or alias. | ||
| """ | ||
|
|
||
| runtimes = await get_runtimes() | ||
| runtimes_dict = {} | ||
|
|
||
| for runtime in runtimes: | ||
| if runtime.language in runtimes_dict: | ||
| runtimes_dict[runtime.language].append(runtime) | ||
| else: | ||
| runtimes_dict[runtime.language] = [runtime] | ||
|
|
||
| for alias in runtime.aliases: | ||
| if alias in runtimes_dict: | ||
| runtimes_dict[alias].append(runtime) | ||
| else: | ||
| runtimes_dict[alias] = [runtime] | ||
|
|
||
| return runtimes_dict | ||
|
|
||
|
|
||
| async def get_runtime(language: str) -> List["Runtime"]: | ||
| """Get a runtime with a language or an alias.""" | ||
|
|
||
| runtimes_dict = await get_runtimes_dict() | ||
| return runtimes_dict.get(language, []) | ||
|
|
||
|
|
||
| class Runtime: | ||
| def __init__(self, data: dict): | ||
| self.language = data["language"] | ||
| self.version = data["version"] | ||
| self.aliases = data["aliases"] | ||
| self.runtime = data.get("runtime") | ||
takos22 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from . import languages | ||
| from .routes import router | ||
|
|
||
| router.include_router(languages.router) | ||
|
|
||
| __all__ = (router,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .routes import router | ||
|
|
||
| __all__ = (router,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from fastapi import HTTPException | ||
|
|
||
| from api.services import piston | ||
|
|
||
|
|
||
| async def check_piston_language_version(language: str, version: str): | ||
| """Checks if a language and its version are installed on the Piston service. | ||
|
|
||
| Raises an :class:`fastapi.HTTPException` otherwise with a 404 status code. | ||
| """ | ||
|
|
||
| runtimes = await piston.get_runtime(language) | ||
| if not runtimes: | ||
| raise HTTPException(404, "Piston language not found") | ||
|
|
||
| versions = [runtime.version for runtime in runtimes] | ||
| if version not in versions: | ||
| raise HTTPException(404, "Piston language version not found") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| from typing import Optional | ||
|
|
||
| from pydantic import BaseModel, Field, HttpUrl | ||
|
|
||
|
|
||
| class ChallengeLanguageResponse(BaseModel): | ||
| id: str | ||
| name: str | ||
| download_url: Optional[HttpUrl] | ||
| disabled: bool = False | ||
| piston_lang: str | ||
| piston_lang_ver: str | ||
|
|
||
|
|
||
| class NewChallengeLanguageBody(BaseModel): | ||
| name: str = Field(..., min_length=4, max_length=32) | ||
| download_url: Optional[HttpUrl] = None | ||
| disabled: bool = False | ||
| piston_lang: str | ||
| piston_lang_ver: str | ||
|
|
||
|
|
||
| class UpdateChallengeLanguageBody(BaseModel): | ||
| name: str = Field("", min_length=4, max_length=32) | ||
| download_url: Optional[HttpUrl] = None | ||
| disabled: bool = False | ||
| piston_lang: str = "" | ||
| piston_lang_ver: str = "" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.