Skip to content

Commit

Permalink
Close #686
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeluigi committed Jan 3, 2025
1 parent 189d726 commit 7126995
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 11 deletions.
3 changes: 2 additions & 1 deletion backend/website/services/archidekt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .json_api import get
from common.abstractions import Deck, CardInDeck
from django.conf import settings
from django.core.exceptions import ValidationError

ARCHIDEKT_HOSTNAME = 'archidekt.com'

Expand Down Expand Up @@ -36,7 +37,7 @@ def archidekt(url: str) -> Deck | None:
]
return Deck(main=main, commanders=commanders)
except KeyError:
return None
raise ValidationError('Invalid decklist')


def archidekt_id_to_api(id: str) -> str:
Expand Down
9 changes: 7 additions & 2 deletions backend/website/services/csv_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import csv
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from django.core.exceptions import ValidationError
from .useragent import FAKE_USERAGENT


Expand All @@ -13,5 +14,9 @@ def get(url: str) -> list[dict[str, object]] | None:
data = list(csv.reader(res.read().decode('utf-8').splitlines()))
header = data[0]
return [dict(zip(header, row, strict=True)) for row in data[1:]]
except (HTTPError, csv.Error, ValueError, IndexError):
return None
except HTTPError as e:
if e.code == 404:
return None
raise ValidationError('Error response from the website')
except (csv.Error, ValueError, IndexError):
raise ValidationError('Invalid response from the website')
3 changes: 2 additions & 1 deletion backend/website/services/deckstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import urlparse
from .json_api import get
from common.abstractions import Deck, CardInDeck
from django.core.exceptions import ValidationError


DECKSTATS_HOSTNAME = 'deckstats.net'
Expand Down Expand Up @@ -42,7 +43,7 @@ def deckstats(url: str) -> Deck | None:
main.append(CardInDeck(card=name, quantity=quantity))
return Deck(main=main, commanders=commanders)
except KeyError:
return None
raise ValidationError('Invalid decklist')


def deckstats_to_api(owner: str, deck: str) -> str:
Expand Down
9 changes: 7 additions & 2 deletions backend/website/services/json_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
from urllib.request import Request, urlopen
from urllib.error import HTTPError
from django.core.exceptions import ValidationError
from .useragent import FAKE_USERAGENT


Expand All @@ -12,5 +13,9 @@ def get(url: str, user_agent: str | None = None) -> dict | None:
with urlopen(req) as res:
data = json.load(res)
return data
except (HTTPError, json.JSONDecodeError):
return None
except HTTPError as e:
if e.code == 404:
return None
raise ValidationError('Error response from the website')
except json.JSONDecodeError:
ValidationError('Invalid response from the website')
3 changes: 2 additions & 1 deletion backend/website/services/moxfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .json_api import get
from common.abstractions import Deck, CardInDeck
from django.conf import settings
from django.core.exceptions import ValidationError


MOXFIELD_HOSTNAME = 'moxfield.com'
Expand Down Expand Up @@ -31,7 +32,7 @@ def moxfield(url: str) -> Deck | None:
]
return Deck(main=main, commanders=commanders)
except (KeyError, ValueError, AttributeError):
return None
raise ValidationError('Invalid decklist')


def moxfield_id_to_api(id: str) -> str:
Expand Down
3 changes: 2 additions & 1 deletion backend/website/services/tappedout.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import urlparse
from .csv_api import get
from common.abstractions import Deck, CardInDeck
from django.core.exceptions import ValidationError


TAPPEDOUT_HOSTNAME = 'tappedout.net'
Expand Down Expand Up @@ -32,7 +33,7 @@ def tappedout(url: str) -> Deck | None:
main.append(CardInDeck(card=name, quantity=quantity))
return Deck(main=main, commanders=commanders)
except (KeyError, ValueError):
return None
raise ValidationError('Invalid decklist')


def tappedout_id_to_api(id: str) -> str:
Expand Down
21 changes: 18 additions & 3 deletions backend/website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ class UnsupportedWebsite(APIException):
default_code = 'unsupported_website'


class DecklistNotAvailable(APIException):
status_code = 400
default_detail = 'Decklist not available'
default_code = 'decklist_not_available'


class SomethingWentWrong(APIException):
status_code = 400
default_detail = 'Something went wrong'
default_code = 'something_went_wrong'


@extend_schema(
parameters=[OpenApiParameter(name='url', type=str)],
responses={
Expand All @@ -67,9 +79,12 @@ def card_list_from_url(request: Request) -> Response:
hostname = (parsed_url.hostname or '').lower().removeprefix('www.')
if hostname not in SUPPORTED_DECKBUILDING_WEBSITES:
raise UnsupportedWebsite()
deck = SUPPORTED_DECKBUILDING_WEBSITES[hostname](url)
if deck is None:
raise UnsupportedWebsite()
try:
deck = SUPPORTED_DECKBUILDING_WEBSITES[hostname](url)
if deck is None:
raise DecklistNotAvailable()
except ValidationError as e:
raise SomethingWentWrong(detail=str(e))
return Response(DeckSerializer(deck).data)


Expand Down

0 comments on commit 7126995

Please sign in to comment.